Camera相機類相關的幾個流程方法 Camera.open(cameraId) 打開相機 camera.setDisplayOrientation(0) 設置相機水平方向 mCamera.setPreviewDisplay(holder) 在SurfaceHolder上顯示畫面 mCamera.s ...
Camera相機類相關的幾個流程方法
Camera.open(cameraId) 打開相機
camera.setDisplayOrientation(0) 設置相機水平方向
mCamera.setPreviewDisplay(holder) 在SurfaceHolder上顯示畫面
mCamera.setPreviewCallback(callback) 設置回調,將Yuv數據傳遞到onPreviewFrame方法
previewRate mParams = mCamera.getParameters() 獲取相機相關參數
mCamera.stopPreview() 停止預覽
mCamera.release() 釋放相機
實現Camera.PreviewCallback介面獲取Yuv視頻流
@Override public void onPreviewFrame(byte[] bytes, Camera camera) { //獲取實時的Yuv數據流 }
實現Camera.PictureCallback介面實現拍照
@Override public void onPictureTaken(byte[] bytes, Camera camera) { try { FileOutputStream fileOutputStream=new FileOutputStream("/mnt/sdcard/DCIM/cameras/"+System.currentTimeMillis()+".png"); fos.write(bytes); fos.close(); //重新打開預覽 camera.stopPreview(); camera.startPreview(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
尋找相機
private int findCamera(boolean isfront) { int cameraCount = 0; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); cameraCount = Camera.getNumberOfCameras(); for (int camIdx = 0; camIdx < cameraCount; camIdx++) { Camera.getCameraInfo(camIdx, cameraInfo); if (isfront) { // CAMERA_FACING_FRONT前置 if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { return camIdx; } } else { // CAMERA_FACING_BACK後置 if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) { return camIdx; } } } return -1; }
Camera.Parameters相機參數類
Camera.Parameters parameters = camera.getParameters(); //獲取相機參數
List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes(); //獲取攝像頭支持的各種解析度
parameters.setPictureFormat(PixelFormat.JPEG);//設置照片格式
parameters.setPreviewSize(witdh,height); //設置預覽尺寸
parameters.setPictureSize(witdh,height);
camera.setParameters(parameters);//提交相機參數
設置相機解析度
private void initCamera(Camera mCamera){ if(mCamera != null){ try{ mParams = mCamera.getParameters(); Size previewSize = CamParaUtil.getInstance().getPropPreviewSize(mParams.getSupportedPreviewSizes(),1.333f, 640); Log.d("log", String.valueOf(previewSize.width) + "-" + previewSize.height); pWidth = previewSize.width; pHeight = previewSize.height; mParams.setPreviewSize(pWidth, pHeight); List<String> focusModes = mParams.getSupportedFocusModes(); if(focusModes.contains("continuous-video")){ mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); } mCamera.setParameters(mParams); mCamera.startPreview(); isPreviewing = true; mPreviwRate = previewRate; }catch (Exception ex){ ex.printStackTrace(); } } } public Size getPropPreviewSize(List<Size> list, float th, int minWidth){ Collections.sort(list, sizeComparator); int i = 0; for(Size s:list){ if((s.width >= minWidth) && equalRate(s, th)){ break; } i++; } if(i == list.size()){ } return list.get(i); }