Android 照相机(闪光灯,切换摄像头)

1、开启关闭闪光灯

    /**
     * 通过设置Camera打开闪光灯
     */
    public synchronized  void turnLightOn() {
        if (camera == null) {
            return;
        }
        parameters = camera.getParameters();
        if (parameters == null) {
            return;
        }
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        camera.setParameters(parameters);
    }
    /**
     * 通过设置Camera关闭闪光灯
     */
    public synchronized void turnLightOff() {
        if (camera == null) {
            return;
        }
        parameters = camera.getParameters();
        if (parameters == null) {
            return;
        }
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        camera.setParameters(parameters);
    }

2、切换前后摄像头


 private int mCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
//现在是后置,变更为前置
 if (camera != null && mCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
      closeCamera();
      camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);//打开当前选中的摄像头
       try {
             amera.setPreviewDisplay(surfaceHolder);//通过surfaceview显示取景画面
             camera.autoFocus(CameraManager.get().getAutoFocusCallback());
             camera.setDisplayOrientation(getPreviewDegree());
             } catch (IOException e) {
                e.printStackTrace();
             }
         camera.startPreview();//开始预览
         mCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
         } else if (mCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT) {
           closeCamera();
           camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);//打开当前选中的摄像头
         try {
              camera.setPreviewDisplay(surfaceHolder);//通过surfaceview显示取景画面
              camera.autoFocus(CameraManager.get().getAutoFocusCallback());
              camera.setDisplayOrientation(getPreviewDegree());
          } catch (IOException e) {
               e.printStackTrace();
          }
           camera.startPreview();//开始预览
           mCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;

    }
  public void closeCamera() {
        if (camera != null) {
            camera.stopPreview();//停掉原来摄像头的预览
            camera.release();//释放资源
            camera = null;//取消原来摄像头
            CameraManager.get().getPreviewCallback().setHandler(null, 0);
            CameraManager.get().getAutoFocusCallback().setHandler(null, 0);
            CameraManager.get().setPreviewing(false);
        }

    }
 // 提供一个静态方法,用于根据手机方向获得相机预览画面旋转的角度
    private int getPreviewDegree() {
        // 获得手机的方向
        int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
        int degree = 0;
        // 根据手机的方向计算相机预览画面应该选择的角度
        switch (rotation) {
            case Surface.ROTATION_0:
                degree = 90;
                break;
            case Surface.ROTATION_90:
                degree = 0;
                break;
            case Surface.ROTATION_180:
                degree = 270;
                break;
            case Surface.ROTATION_270:
                degree = 180;
                break;
        }
        return degree;
    }

你可能感兴趣的:(Android 照相机(闪光灯,切换摄像头))