时间:2021-05-21
最近得到了一个需求,在后台拍照并保存
public void onTakePhotoClicked() { final SurfaceView preview = new SurfaceView(this); SurfaceHolder holder = preview.getHolder(); // deprecated setting, but required on Android versions prior to 3.0holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); holder.addCallback(new SurfaceHolder.Callback() { @Override //The preview must happen at or after this point or takePicture fails public void surfaceCreated(SurfaceHolder holder) { Log.d(TAG, "Surface created"); camera = null; try { camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK); Log.d(TAG, "Opened camera"); try { camera.setPreviewDisplay(holder); } catch (IOException e) { throw new RuntimeException(e); } camera.startPreview(); Log.d(TAG, "Started preview"); //延时拍照 ThreadUtils.postOnUiThreadDelayed(new Runnable() { @Override public void run() { Log.e("zgj","开始拍照"); camera.takePicture(null, null, CameraService.this); } },5000); } catch (Exception e) { if (camera != null) camera.release(); throw new RuntimeException(e); } } @Override public void surfaceDestroyed(SurfaceHolder holder) {} @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {} }); WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); WindowManager.LayoutParams params = new WindowManager.LayoutParams( 1, 1, //Must be at least 1x1 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, //Don't know if this is a safe default PixelFormat.UNKNOWN); //Don't set the preview visibility to GONE or INVISIBLE wm.addView(preview, params); } @Override public void onPictureTaken(byte[] bytes, Camera camera) { Log.e("zgj", "拍照结束"); File pictureDir = Environment.getExternalStorageDirectory(); if (pictureDir == null) { Log.d("zgj", "Error creating media file, check storage permissions!"); return; } try { String pictureName = "ssss.png"; File file = new File(pictureDir + "/pic/"); if (!file.exists()) { file.mkdir(); } file = new File(pictureDir + "/pic/" + pictureName); FileOutputStream fos = new FileOutputStream(file); fos.write(bytes); fos.close(); } catch (FileNotFoundException e) { Log.d("zgj", "File not found: " + e.getMessage()); } catch (IOException e) { Log.d("zgj", "Error accessing file: " + e.getMessage()); } }总结
以上所述是小编给大家介绍的Android 无预览拍照功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了Android实现无预览拍照功能的具体代码,供大家参考,具体内容如下实现思路:把预览的SurfaceView的宽高设置为肉眼看不出的值,例如
本文实例为大家分享了Android实现摄像头切换,拍照及保存到相册,预览等功能,解决android7拍照之后不能连续预览的问题、参数设置相关问题以及前后摄像头语
这篇博客为大家介绍Android自定义相机,并且实现倒计时拍照功能。首先自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件:activ
本文介绍了Android编辑头像功能的简单实例,可以实现拍照,图片选取,裁剪。拍照publicstaticvoidstartCamera(Fragmentfra
ImageSelector简介Android自定义相册,实现了拍照、图片选择(单选/多选)、ImageLoader无绑定任由开发者选择https://githu