Android CameraManager类详解
本文实例为大家分享了AndroidCameraManager类的具体代码,供大家参考,具体内容如下
先看代码:
privateSurfaceViewmSurfaceView; privateSurfaceHoldermSurfaceHolder; privateCameraManagercameraManager; cameraManager=newCameraManager(this); mSurfaceView=(SurfaceView)findViewById(R.id.java_surface_view); mSurfaceHolder=mSurfaceView.getHolder(); //mSurfaceView不需要自己的缓冲区 mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //mSurfaceView添加回调 mSurfaceHolder.addCallback(newSurfaceHolder.Callback(){ @Override publicvoidsurfaceCreated(SurfaceHolderholder){//SurfaceView创建 try{ cameraManager.openDriver(mSurfaceHolder); cameraManager.startPreview(); }catch(IOExceptione){ e.printStackTrace(); } } @Override publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,intheight){ } @Override publicvoidsurfaceDestroyed(SurfaceHolderholder){//SurfaceView销毁 holder.removeCallback(this);//CameraisbeingusedafterCamera.release()wascalled cameraManager.stopPreview(); cameraManager.closeDriver(); } });
自定义一个CameraManager类,在关闭当前页面,释放camera资源时偶尔报错
CameraisbeingusedafterCamera.release()wascalled
第一次,加了一句holder.removeCallback(this);运行发现还是时不时出现报错。
第二次在释放camera前加 camera.setPreviewCallback(null);问题解决。
/** *关闭预览 */ publicsynchronizedvoidstopPreview(){ Log.e(TAG,"stopPreview"); if(autoFocusManager!=null){ autoFocusManager.stop(); autoFocusManager=null; } if(camera!=null&&previewing){ camera.stopPreview(); camera.setPreviewCallback(null);//CameraisbeingusedafterCamera.release()wascalled previewing=false; } } /** *关闭camera */ publicsynchronizedvoidcloseDriver(){ Log.e(TAG,"closeDriver"); if(camera!=null){ camera.release(); camera=null; } }
附:CameraManager类:
packagecom.lemoncome.facedetection; importandroid.content.Context; importandroid.graphics.Bitmap; importandroid.graphics.BitmapFactory; importandroid.graphics.ImageFormat; importandroid.graphics.Matrix; importandroid.hardware.Camera; importandroid.util.Log; importandroid.view.Display; importandroid.view.Surface; importandroid.view.SurfaceHolder; importandroid.view.WindowManager; importjava.io.IOException; importjava.util.List; /** *User:lizhangqu(513163535@qq.com) *Date:2015-09-05 *Time:10:56 */ publicclassCameraManagerimplementsCamera.PreviewCallback{ privatestaticfinalStringTAG=CameraManager.class.getName(); privateCameracamera; privateCamera.Parametersparameters; privateAutoFocusManagerautoFocusManager; privateintrequestedCameraId=-1; privateContextmContext; privatebooleaninitialized; privatebooleanpreviewing; /** *打开摄像头 * *@paramcameraId摄像头id *@returnCamera */ publicCameraopen(intcameraId){ intnumCameras=Camera.getNumberOfCameras(); if(numCameras==0){ Log.e(TAG,"Nocameras!"); returnnull; } booleanexplicitRequest=cameraId>=0; if(!explicitRequest){ //Selectacameraifnoexplicitcamerarequested intindex=0; while(indexpreviewSizes=parameters.getSupportedPreviewSizes(); intw=800; inth=600; for(Camera.Sizesize:previewSizes){ Log.e("TAG","previewSizeswidth:"+size.width); Log.e("TAG","previewSizesheight:"+size.height); if(size.width-w<=100&size.width>=w){ w=size.width; h=size.height; break; } } parameters.setPreviewSize(w,h); parameters.setPictureFormat(ImageFormat.JPEG); parameters.setJpegQuality(100); parameters.setPictureSize(800,600); theCamera.setParameters(parameters); } } /** *camera是否打开 * *@returncamera是否打开 */ publicsynchronizedbooleanisOpen(){ returncamera!=null; } /** *关闭camera */ publicsynchronizedvoidcloseDriver(){ Log.e(TAG,"closeDriver"); if(camera!=null){ camera.release(); camera=null; } } /** *开始预览 */ publicsynchronizedvoidstartPreview(){ Log.e(TAG,"startPreview"); CameratheCamera=camera; if(theCamera!=null&&!previewing){ theCamera.startPreview(); previewing=true; autoFocusManager=newAutoFocusManager(camera); } } /** *关闭预览 */ publicsynchronizedvoidstopPreview(){ Log.e(TAG,"stopPreview"); if(autoFocusManager!=null){ autoFocusManager.stop(); autoFocusManager=null; } if(camera!=null&&previewing){ camera.stopPreview(); camera.setPreviewCallback(null);//CameraisbeingusedafterCamera.release()wascalled previewing=false; } } publicvoidsetPreviewCallback(Camera.PreviewCallbackcb){ camera.setOneShotPreviewCallback(this); } /** *打开闪光灯 */ publicsynchronizedvoidopenLight(){ Log.e(TAG,"openLight"); if(camera!=null){ parameters=camera.getParameters(); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); camera.setParameters(parameters); } } /** *关闭闪光灯 */ publicsynchronizedvoidoffLight(){ Log.e(TAG,"offLight"); if(camera!=null){ parameters=camera.getParameters(); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); camera.setParameters(parameters); } } /** *拍照 * *@paramshutterShutterCallback *@paramrawPictureCallback *@paramjpegPictureCallback */ publicsynchronizedvoidtakePicture(finalCamera.ShutterCallbackshutter,finalCamera.PictureCallbackraw, finalCamera.PictureCallbackjpeg){ camera.takePicture(shutter,raw,jpeg); } @Override publicvoidonPreviewFrame(byte[]bytes,Cameracamera){ } privateBitmapreSize(byte[]data){ Log.i(TAG,"myJpegCallback:onPictureTaken..."); BitmapcutMap=BitmapFactory.decodeByteArray(data,0,data.length);//data是字节数据,将其解析成位图 //设置FOCUS_MODE_CONTINUOUS_VIDEO)之后,myParam.set("rotation",90)失效。图片竟然不能旋转了,故这里要旋转下 Matrixmatrix=newMatrix(); matrix.postRotate((float)90.0); BitmaprotaBitmap=Bitmap.createBitmap(cutMap,0,0,cutMap.getWidth(),cutMap.getHeight(),matrix,false); //旋转后rotaBitmap是960×1280.预览surfaview的大小是540×800 //将960×1280缩放到540×800 BitmapsizeBitmap=Bitmap.createScaledBitmap(rotaBitmap,540,800,true); BitmaprectBitmap=Bitmap.createBitmap(sizeBitmap,100,200,300,300);//截取 returnrectBitmap; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。