您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關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(); List<Camera.Size> previewSizes = 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類的作用是什么,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。