本文介绍了Android Camera2 API 切换后置 - 前置摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

My code is strongly inspired from the code provided by Google here. My camera preview has a button to switch from back to front camera and then from front to back camera.The "camera preview" activity is launched with the back camera by default.

由于某种原因,当我第一次点击切换/交换摄像头"按钮时,它应该会出现在前置摄像头上,但是每次我再次点击时,切换/swap 不再起作用:预览(在前置摄像头上)有点淡化,就像发生了什么事情一样,但它仍保留在当前选择的(前置)摄像头上.

For some reason, when I click on the "switch/swap camera" button for the first time, it brings be to the front camera as it should, BUT everytime I click again, the switch/swap doesn't work anymore: the preview (on the front camera) fades a little bit, like if something is happening, but it remains on the currently selected (front) camera.

这是我的代码:

在一个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();
        }
    }

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

If you have any idea on what's happening that would save me. I have been bugging on this for days. Thank you very much

推荐答案

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

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;

删除 cameraId 局部变量来自 openCamera 方法.

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);
    }
}

这篇关于Android Camera2 API 切换后置 - 前置摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 06:09