扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
本篇文章给大家分享的是有关Android中CameraManager类的作用是什么,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
为天桥等地区用户提供了全套网页设计制作服务,及天桥网站建设行业解决方案。主营业务为成都做网站、网站建设、天桥网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
具体内容如下
private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private CameraManager cameraManager; cameraManager = new CameraManager(this); mSurfaceView = (SurfaceView) findViewById(R.id.java_surface_view); mSurfaceHolder = mSurfaceView.getHolder(); // mSurfaceView 不需要自己的缓冲区 mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // mSurfaceView添加回调 mSurfaceHolder.addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { //SurfaceView创建 try { cameraManager.openDriver(mSurfaceHolder); cameraManager.startPreview(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { //SurfaceView销毁 holder.removeCallback(this); // Camera is being used after Camera.release() was called cameraManager.stopPreview(); cameraManager.closeDriver(); } });
自定义一个 CameraManager 类,在关闭当前页面,释放camera资源时偶尔报错
Camera is being used after Camera.release() was called
第一次,加了一句 holder.removeCallback(this); 运行发现还是时不时出现报错。
第二次在释放 camera前加 camera.setPreviewCallback(null); 问题解决。
/** * 关闭预览 */ public synchronized void stopPreview() { Log.e(TAG, "stopPreview"); if (autoFocusManager != null) { autoFocusManager.stop(); autoFocusManager = null; } if (camera != null && previewing) { camera.stopPreview(); camera.setPreviewCallback(null); // Camera is being used after Camera.release() was called previewing = false; } } /** * 关闭camera */ public synchronized void closeDriver() { Log.e(TAG, "closeDriver"); if (camera != null) { camera.release(); camera = null; } }
附:CameraManager类:
package com.lemoncome.facedetection; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.ImageFormat; import android.graphics.Matrix; import android.hardware.Camera; import android.util.Log; import android.view.Display; import android.view.Surface; import android.view.SurfaceHolder; import android.view.WindowManager; import java.io.IOException; import java.util.List; /** * User:lizhangqu(513163535@qq.com) * Date:2015-09-05 * Time: 10:56 */ public class CameraManager implements Camera.PreviewCallback { private static final String TAG = CameraManager.class.getName(); private Camera camera; private Camera.Parameters parameters; private AutoFocusManager autoFocusManager; private int requestedCameraId = -1; private Context mContext; private boolean initialized; private boolean previewing; /** * 打开摄像头 * * @param cameraId 摄像头id * @return Camera */ public Camera open(int cameraId) { int numCameras = Camera.getNumberOfCameras(); if (numCameras == 0) { Log.e(TAG, "No cameras!"); return null; } boolean explicitRequest = cameraId >= 0; if (!explicitRequest) { // Select a camera if no explicit camera requested int index = 0; while (index < numCameras) { Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); Camera.getCameraInfo(index, cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) { break; } index++; } cameraId = index; } Camera camera; if (cameraId < numCameras) { Log.e(TAG, "Opening camera #" + cameraId); camera = Camera.open(cameraId); } else { if (explicitRequest) { Log.e(TAG, "Requested camera does not exist: " + cameraId); camera = null; } else { Log.e(TAG, "No camera facing back; returning camera #0"); camera = Camera.open(0); } } int rotation = getDisplayOrientation(); camera.setDisplayOrientation(rotation); camera.setPreviewCallback(this); return camera; } public int getDisplayOrientation() { Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int rotation = display.getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; } Camera.CameraInfo camInfo = new Camera.CameraInfo(); Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, camInfo); int result = (camInfo.orientation - degrees + 360) % 360; return result; } public CameraManager(Context context) { mContext = context; } /** * 打开camera * * @param holder SurfaceHolder * @throws IOException IOException */ public synchronized void openDriver(SurfaceHolder holder) throws IOException { Log.e(TAG, "openDriver"); Camera theCamera = camera; if (theCamera == null) { theCamera = open(requestedCameraId); if (theCamera == null) { throw new IOException(); } camera = theCamera; } theCamera.setPreviewDisplay(holder); if (!initialized) { initialized = true; parameters = camera.getParameters(); ListpreviewSizes = parameters.getSupportedPreviewSizes(); int w = 800; int h = 600; for (Camera.Size size : previewSizes) { Log.e("TAG", "previewSizes width:" + size.width); Log.e("TAG", "previewSizes height:" + 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是否打开 * * @return camera是否打开 */ public synchronized boolean isOpen() { return camera != null; } /** * 关闭camera */ public synchronized void closeDriver() { Log.e(TAG, "closeDriver"); if (camera != null) { camera.release(); camera = null; } } /** * 开始预览 */ public synchronized void startPreview() { Log.e(TAG, "startPreview"); Camera theCamera = camera; if (theCamera != null && !previewing) { theCamera.startPreview(); previewing = true; autoFocusManager = new AutoFocusManager(camera); } } /** * 关闭预览 */ public synchronized void stopPreview() { Log.e(TAG, "stopPreview"); if (autoFocusManager != null) { autoFocusManager.stop(); autoFocusManager = null; } if (camera != null && previewing) { camera.stopPreview(); camera.setPreviewCallback(null); // Camera is being used after Camera.release() was called previewing = false; } } public void setPreviewCallback(Camera.PreviewCallback cb) { camera.setOneShotPreviewCallback(this); } /** * 打开闪光灯 */ public synchronized void openLight() { Log.e(TAG, "openLight"); if (camera != null) { parameters = camera.getParameters(); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); camera.setParameters(parameters); } } /** * 关闭闪光灯 */ public synchronized void offLight() { Log.e(TAG, "offLight"); if (camera != null) { parameters = camera.getParameters(); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); camera.setParameters(parameters); } } /** * 拍照 * * @param shutter ShutterCallback * @param raw PictureCallback * @param jpeg PictureCallback */ public synchronized void takePicture(final Camera.ShutterCallback shutter, final Camera.PictureCallback raw, final Camera.PictureCallback jpeg) { camera.takePicture(shutter, raw, jpeg); } @Override public void onPreviewFrame(byte[] bytes, Camera camera) { } private Bitmap reSize(byte[] data) { Log.i(TAG, "myJpegCallback:onPictureTaken..."); Bitmap cutMap = BitmapFactory.decodeByteArray(data, 0, data.length);//data是字节数据,将其解析成位图 //设置FOCUS_MODE_CONTINUOUS_VIDEO)之后,myParam.set("rotation", 90)失效。图片竟然不能旋转了,故这里要旋转下 Matrix matrix = new Matrix(); matrix.postRotate((float) 90.0); Bitmap rotaBitmap = Bitmap.createBitmap(cutMap, 0, 0, cutMap.getWidth(), cutMap.getHeight(), matrix, false); //旋转后rotaBitmap是960×1280.预览surfaview的大小是540×800 //将960×1280缩放到540×800 Bitmap sizeBitmap = Bitmap.createScaledBitmap(rotaBitmap, 540, 800, true); Bitmap rectBitmap = Bitmap.createBitmap(sizeBitmap, 100, 200, 300, 300);//截取 return rectBitmap; } }
以上就是Android中CameraManager类的作用是什么,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流