我们知道自定义Camera需要以下几步
打开相机,即实例化Camera对象,Camera camera = Camera.open(); 设置Camera的相关参数,Camera.Parameters parameters = camera.getParameters(); 打开预览,camera.setPrevIEwdisplay(surfaceholder); camera.startPrevIEw(); 获取图片,这里只是从预览中获取因此使用,camera.setPrevIEwCallback(new Camera.PrevIEwCallback(){……..}); 停止预览,释放相机,camera.stopPrevIEw();camera.release();1、打开相机
public static Camera openFacingBackCamera() { Camera cam = null; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); for (int camIDx = 0,cameraCount = Camera.getNumberOfCameras(); camIDx < cameraCount; camIDx++) { Camera.getCameraInfo(camIDx,cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { try { //打开相应的摄像头,目前是前置摄像头 cam = Camera.open(camIDx); } catch (RuntimeException e) { e.printstacktrace(); if (cam!=null){ cam.stopPrevIEw(); cam.release(); } cam = null; } } } return cam; }
2、设置相关参数和预览
//定义SurfaceVIEwprivate SurfaceVIEw localSurface; // 为surfaceHolder添加一个回调监听器 localSurface.getHolder().addCallback(new SurfaceHolder.Callback() { @OverrIDe public voID surfaceCreated(SurfaceHolder holder) { } @OverrIDe public voID surfaceChanged(SurfaceHolder holder,int format,int wIDth,int height) { camera = CameraUtil.openFacingBackCamera(); //设置相机预览旋转角度 camera.setdisplayOrIEntation(180); if (camera != null) { Camera.Parameters parameters = camera.getParameters();// 获得相机参数 List<Camera.Size> sizes = parameters.getSupportedPrevIEwSizes(); List<Camera.Size> sizes_pic = parameters.getSupportedPictureSizes(); Camera.Size cs = sizes.get(0); int mWIDth = cs.wIDth; int mHeight = cs.height; parameters.setPrevIEwSize(mWIDth,mHeight); parameters.setPictureSize(sizes_pic.get(0).wIDth,sizes_pic.get(0).height); parameters.setJpegQuality(85);// parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_auto); parameters.setPictureFormat(ImageFormat.JPEG); camera.setParameters(parameters); try { camera.setPrevIEwdisplay(localSurface.getHolder()); camera.startPrevIEw(); // 开始预览 camera.autoFocus(null); // 自动对焦 } catch (IOException e) { e.printstacktrace(); camera.stopPrevIEw(); camera.release(); camera = null; } } } @OverrIDe public voID surfaceDestroyed(SurfaceHolder holder) { if (camera!=null){ camera.stopPrevIEw(); camera.release(); camera = null; } } } ); // 设置该SurfaceVIEw自己不维护缓冲 localSurface.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
3、获取图片
private voID getVIEwImage() { //设置监听 camera.setPrevIEwCallback(new Camera.PrevIEwCallback(){ @OverrIDe public voID onPrevIEwFrame(byte[] data,Camera camera) { Camera.Size size = camera.getParameters().getPrevIEwSize(); try{ YuvImage image = new YuvImage(data,ImageFormat.NV21,size.wIDth,size.height,null); if(image!=null){ ByteArrayOutputStream stream = new ByteArrayOutputStream(); image.compresstoJpeg(new Rect(0,size.height),80,stream); Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(),stream.size()); //因为图片会放生旋转,因此要对图片进行旋转到和手机在一个方向上 rotateBitmap(bmp); stream.close(); } }catch(Exception ex){ Log.e("Sys","Error:"+ex.getMessage()); } } }); }
旋转图片的方法
public voID rotateMyBitmap(Bitmap bmp){ Matrix matrix = new Matrix(); matrix.postscale(-1,1); // 镜像水平翻转(如果左右颠倒) matrix.postRotate(180); Bitmap nbmp = Bitmap.createBitmap(bmp,bmp.getWIDth(),bmp.getHeight(),matrix,true); imageVIEw.setimageBitmap(nbmp); };
自定义时间拍照
private Handler mHandler = new Handler() { public voID handleMessage(Message msg) { switch (msg.what) { case 101: getVIEwImage(); Log.e(TAG,"handleMessage: 拍照" ); mHandler.sendEmptyMessageDelayed(102,100); break; case 102: camera.setPrevIEwCallback(null); break ; } } };
然后点击按钮调用
mHandler.sendEmptyMessageDelayed(101,100);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android Camera实现毫秒级拍照实例全部内容,希望文章能够帮你解决Android Camera实现毫秒级拍照实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)