google 在Android 5.0推出 Camera2 這個類,用於替換 Camera,但是Camera2要求android sdk 最低版本為 minSdkVersion = 21 (5.0系統),所以Camera2 還不能完全替換 Camera,在相容低版本的時候,還是需要兩者一起協同開發。 ...
google 在Android 5.0推出 Camera2 這個類,用於替換 Camera,但是Camera2要求android sdk 最低版本為 minSdkVersion = 21 (5.0系統),所以Camera2 還不能完全替換 Camera,在相容低版本的時候,還是需要兩者一起協同開發。下麵我來說一下 Camera 的拍攝例子: 首先需要在xml 上佈局一個 SurfaceView 設置全屏
<SurfaceView android:id="@+id/surfaceView" android:layout_width="match_parent" android:layout_height="match_parent"/>
同時也把狀態欄和titleBar隱藏了:
requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉標題欄 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 設置全屏
然後獲取SurfacView 實例以及其持有者SurfaceHolder,接入SurfaceHolder.Callback回調,
mSurfaceView = (SurfaceView)findViewById(R.id.surfaceView); mSurfaceHolder = mSurfaceView.getHolder();// 取得holder mSurfaceHolder.addCallback(this); // holder加入回調介面 mSurfaceHolder.setKeepScreenOn(true);
SurfaceHolder.Callback會在頁面Actvity 初始化完畢後調用,則在回調的surfaceChanged初始化Camera,也就是打開預覽頁面:
@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (mCamera != null) { freeCameraResource(); } try { mCamera = Camera.open(); if (mCamera == null) return; mCamera.setDisplayOrientation(90); mCamera.setPreviewDisplay(mSurfaceHolder); parameters = mCamera.getParameters();// 獲得相機參數 List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes(); List<Camera.Size> mSupportedVideoSizes = parameters.getSupportedVideoSizes(); optimalSize = CameraHelper.getOptimalVideoSize(mSupportedVideoSizes, mSupportedPreviewSizes, height, width); parameters.setPreviewSize(optimalSize.width, optimalSize.height); // 設置預覽圖像大小 parameters.set("orientation", "portrait"); List<String> focusModes = parameters.getSupportedFocusModes(); if (focusModes.contains("continuous-video")) { parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); } mFpsRange = parameters.getSupportedPreviewFpsRange(); mCamera.setParameters(parameters);// 設置相機參數 mCamera.startPreview();// 開始預覽 }catch (Exception io){ io.printStackTrace(); } }
該方法返回了SurfaceView的寬與高,根據給出的尺寸與寬高比例,獲取一個最適配的預覽尺寸,你先看下麵有兩個參數:
List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
List<Camera.Size> mSupportedVideoSizes = parameters.getSupportedVideoSizes();
這兩個隊列分別是 該相機支持的 預覽大小(一般就是拍照時照片的大小),另外一個就是支持適配的大小,因為都是隊列,說明相機支持很多組尺寸,而且,照片的尺寸與視頻的尺寸是不一樣的。我debug看了幾款手機,通常攝像支持的尺寸少一點,照片會多一些。這樣,我們就要通過剛剛方法給出的寬高,獲取一個最佳匹配的預覽尺寸:
public static Camera.Size getOptimalVideoSize(List<Camera.Size> supportedVideoSizes, List<Camera.Size> previewSizes, int w, int h) { // Use a very small tolerance because we want an exact match. final double ASPECT_TOLERANCE = 0.1; double targetRatio = (double) w / h; // Supported video sizes list might be null, it means that we are allowed to use the preview // sizes List<Camera.Size> videoSizes; if (supportedVideoSizes != null) { videoSizes = supportedVideoSizes; } else { videoSizes = previewSizes; } Camera.Size optimalSize = null; // Start with max value and refine as we iterate over available video sizes. This is the // minimum difference between view and camera height. double minDiff = Double.MAX_VALUE; // Target view height int targetHeight = h; // Try to find a video size that matches aspect ratio and the target view size. // Iterate over all available sizes and pick the largest size that can fit in the view and // still maintain the aspect ratio. for (Camera.Size size : videoSizes) { double ratio = (double) size.width / size.height; if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; if (Math.abs(size.height - targetHeight) < minDiff && previewSizes.contains(size)) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } // Cannot find video size that matches the aspect ratio, ignore the requirement if (optimalSize == null) { minDiff = Double.MAX_VALUE; for (Camera.Size size : videoSizes) { if (Math.abs(size.height - targetHeight) < minDiff && previewSizes.contains(size)) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } } return optimalSize; }該方法是獲取最佳的預覽與攝像尺寸。然後設置預覽圖像大小:
parameters.setPreviewSize(optimalSize.width, optimalSize.height);可以通過獲取相機的參數實例,設置裡面各種效果,包括剛剛的預覽圖,前置攝像頭,閃光燈等。
parameters = mCamera.getParameters();// 獲得相機參數
設置好預覽已經相機參數,則打開:
mCamera.setParameters(parameters);// 設置相機參數 mCamera.startPreview();// 開始預覽
那麼就進入一個預覽的拍攝頁面了,該頁面其實也可以用來做拍照。要想做拍攝,還要實例化MediaRecorder,然後傳入camera並初始化相應的參數:
mMediaRecorder.setCamera(mCamera); mMediaRecorder.setOnErrorListener(this); mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT ); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);// 視頻源 // Use the same size for recording profile. CamcorderProfile mProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); mProfile.videoFrameWidth = optimalSize.width; mProfile.videoFrameHeight = optimalSize.height; mMediaRecorder.setProfile(mProfile); //該設置是為了抽取視頻的某些幀,真正錄視頻的時候,不要設置該參數 // mMediaRecorder.setCaptureRate(mFpsRange.get(0)[0]);//獲取最小的每一秒錄製的幀數 mMediaRecorder.setOutputFile(mVecordFile.getAbsolutePath()); mMediaRecorder.prepare(); mMediaRecorder.start();錄完的時候停止,需要重置才能再次使用。
try { mMediaRecorder.stop(); mMediaRecorder.reset(); } catch (Exception e) { e.printStackTrace(); }
當頁面destroy的時候,要記得釋放它們:
private void releaseRecord() { if (mMediaRecorder != null) { mMediaRecorder.setPreviewDisplay(null); mMediaRecorder.setOnErrorListener(null); try { mMediaRecorder.release(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } mMediaRecorder = null; }
閃光燈關閉與開啟:
private void flashLightToggle(){ try { if(isFlashLightOn){ parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); mCamera.setParameters(parameters); isFlashLightOn = false; }else { parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); mCamera.setParameters(parameters); isFlashLightOn = true; } } catch (Exception e) { e.printStackTrace(); } }
前後攝像頭切換,就要重新初始化 camera實例:
private void switchCamera(){ Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); int cameraCount = Camera.getNumberOfCameras();//得到攝像頭的個數 for(int i = 0; i < cameraCount; i++ ) { Camera.getCameraInfo(i, cameraInfo);//得到每一個攝像頭的信息 if(cameraPosition == 1) { //現在是後置,變更為前置 if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {//代表攝像頭的方位,CAMERA_FACING_FRONT前置 CAMERA_FACING_BACK後置 mCamera.stopPreview();//停掉原來攝像頭的預覽 mCamera.release();//釋放資源 mCamera = null;//取消原來攝像頭 mCamera = Camera.open(i);//打開當前選中的攝像頭 try { mCamera.setDisplayOrientation(90); mCamera.setPreviewDisplay(mSurfaceHolder);//通過surfaceview顯示取景畫面 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mCamera.setParameters(parameters);// 設置相機參數 mCamera.startPreview();//開始預覽 cameraPosition = 0; break; } } else { //現在是前置, 變更為後置 if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {//代表攝像頭的方位,CAMERA_FACING_FRONT前置 CAMERA_FACING_BACK後置 mCamera.stopPreview();//停掉原來攝像頭的預覽 mCamera.release();//釋放資源 mCamera = null;//取消原來攝像頭 mCamera = Camera.open(i);//打開當前選中的攝像頭 try { mCamera.setDisplayOrientation(90); mCamera.setPreviewDisplay(mSurfaceHolder);//通過surfaceview顯示取景畫面 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mCamera.setParameters(parameters);// 設置相機參數 mCamera.startPreview();//開始預覽 cameraPosition = 1; break; } } } }
錄製時的頁面: 這就是使用 camera 來攝像的主要步驟,demo:https://github.com/xiaoxiaoqingyi/android-CameraVideo 如果你想瞭解 Camera2,你也可以看看google 的 Camera2 官方例子: https://github.com/googlesamples/android-Camera2Basic