切换摄像头

 /**
     * 切换前后摄像头
     */
    @SuppressWarnings("deprecation")
    public void toggleCamera(){

        int cameraCount = 0;
        CameraInfo cameraInfo = new CameraInfo();
        cameraCount = Camera.getNumberOfCameras();// 得到摄像头的个数


        for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraType == BACK_CAMERA) {
                //切换成前置
                if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                    //释放资源
                    freeCameraResource();
                    try {
                        mCamera = Camera.open(camIdx);
                        cameraType = FRONT_CAMERA;
                         if (mCamera == null)
                                return;
                        setCameraParams();
                        return;
                    } catch (RuntimeException e) {
                        e.printStackTrace();
                        freeCameraResource();
                    }
                }
            }else{
                //切换成后置
                if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                    //释放资源
                    freeCameraResource();
                    try {
                        mCamera = Camera.open(camIdx);
                        cameraType = BACK_CAMERA;
                         if (mCamera == null)
                                return;
                        setCameraParams();
                        return;
                    } catch (RuntimeException e) {
                        e.printStackTrace();
                        freeCameraResource();
                    }
                }
            }
        }
    }

你可能感兴趣的:(切换摄像头)