13

我正在使用新的 camera2 API 创建自定义相机捕捉视频。

我的代码受到 Google此处提供的代码的强烈启发。我的相机预览有一个按钮,可以从后置摄像头切换到前置摄像头,然后从前置摄像头切换到后置摄像头。默认情况下,使用后置摄像头启动“相机预览”活动。

出于某种原因,当我第一次单击“切换/交换摄像头”按钮,它应该会带到前置摄像头,但是每次我再次单击时,切换/交换不再起作用:预览(在前置摄像头上)会稍微变淡,就像发生了什么事情一样,但它仍保留在当前选定的(前置)摄像头上。

这是我的代码:

在一个RecordVideoFragment,在onViewCreated

//  Switch camera button
        switchCameraButton = (ImageButton) view.findViewById(R.id.button_switch_camera);
        // Listener for Switch cameras button
        switchCameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchCameras();
            }
        });

这是switchCameras()功能:

private void  switchCameras() {
        mCameraOpenCloseLock.release();
        mCameraDevice.close();

        CameraManager mCameraManager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE);
        try {
            String mCameraId = mCameraManager.getCameraIdList()[0];
            if (mCameraId.equals("0")) {   // If currently on FRONT camera (0 = CameraCharacteristics.LENS_FACING_FRONT)
                mCameraId = "1";           // switch to BACK camera (1 = CameraCharacteristics.LENS_FACING_BACK)
                switchCameraButton.setImageResource(R.drawable.ic_camera_front);
            } else {  // If currently on BACK camera
                mCameraId = "0"; // switch to front camera
                switchCameraButton.setImageResource(R.drawable.ic_camera_back);
            }
            try {
                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
                        == PackageManager.PERMISSION_GRANTED) {
                    mCameraManager.openCamera(mCameraId, mStateCallback, null);
                } else {
                    requestVideoPermissions();
                }
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        } catch (CameraAccessException e) {
            Toast.makeText(getActivity(), "Cannot access the camera.", Toast.LENGTH_SHORT).show();
            getActivity().finish();
        }
    }

如果您对正在发生的事情有任何想法,那将拯救我。这几天我一直在窃听。非常感谢

4

2 回答 2

23

您需要做的是引入新变量:

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

private String cameraId = CAMERA_BACK;

openCamera方法中删除cameraId 局部变量。

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);
    }
}
于 2016-08-21T13:50:03.047 回答
14

在查看了 MrOnyszko 的答案后,我采用了一种稍微不同的方法:

Camera2Basic教程中,镜头朝向用于设置正确的相机,所以我在关闭和重新打开相机之前更改了这个方向。

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;
            }
            ...
于 2018-02-08T08:32:41.637 回答