Android Camera2 API switch back - front cameras

后端 未结 2 427
生来不讨喜
生来不讨喜 2020-12-30 09:09

I\'m creating a custom camera capturing videos with the new camera2 API.

My code is strongly inspired from the code provided by Google here. My camera preview has a

相关标签:
2条回答
  • 2020-12-30 09:38

    After looking at MrOnyszko answer i followed a slightly different approach:

    In the Camera2Basic Tutorial a lens facing direction is used to set up the right camera, so i changed this direction before closing and reopening the camera.

    private void switchCamera() {
        if (mCameraLensFacingDirection == CameraCharacteristics.LENS_FACING_BACK) {
            mCameraLensFacingDirection = CameraCharacteristics.LENS_FACING_FRONT;
            closeCamera();
            reopenCamera();
    
        } else if (mCameraLensFacingDirection == CameraCharacteristics.LENS_FACING_FRONT) {
            mCameraLensFacingDirection = CameraCharacteristics.LENS_FACING_BACK;
            closeCamera();
            reopenCamera();
        }
    }
    
    private void reopenCamera() {
        if (mTextureView.isAvailable()) {
            openCamera(mTextureView.getWidth(), mTextureView.getHeight());
        } else {
            mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
        }
    }
    
    
    private void setUpCameraOutputs(int width, int height) {
        Activity activity = getActivity();
        CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
        try {
            for (String cameraId : manager.getCameraIdList()) {
                CameraCharacteristics characteristics
                        = manager.getCameraCharacteristics(cameraId);
    
                Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
                if (facing != null && facing != mCameraLensFacingDirection) {
                    continue;
                }
                ...
    
    0 讨论(0)
  • 2020-12-30 09:51

    What you need to do is introduce new variables:

    public static final String CAMERA_FRONT = "1";
    public static final String CAMERA_BACK = "0";
    
    private String cameraId = CAMERA_BACK;
    

    remove cameraId local variable from openCamera method.

    public void switchCamera() {
        if (cameraId.equals(CAMERA_FRONT)) {
            cameraId = CAMERA_BACK;
            closeCamera();
            reopenCamera();
            switchCameraButton.setImageResource(R.drawable.ic_camera_front);
    
        } else if (cameraId.equals(CAMERA_BACK)) {
            cameraId = CAMERA_FRONT;
            closeCamera();
            reopenCamera();
            switchCameraButton.setImageResource(R.drawable.ic_camera_back);
        }
    }
    
    public void reopenCamera() {
        if (mTextureView.isAvailable()) {
            openCamera(mTextureView.getWidth(), mTextureView.getHeight());
        } else {
            mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
        }
    }
    
    0 讨论(0)
提交回复
热议问题