Android Camera2 API切换后置摄像头

时间:2016-08-18 16:04:05

标签: android camera camera2 android-camera2

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

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

出于某种原因,当我点击"开关/交换相机"按钮第一次,它会带来前摄像头,但是每次我再次点击,开关/交换都不再工作:预览(在前置摄像头上)稍稍淡化,就像发生了什么事情一样,但它仍然保留在当前选定的(前置)相机上。

这是我的代码:

ViewPager viewPager = (ViewPager) view.findViewById(R.id.view_pager); cleanBackStack(viewPager.getId()); 中的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()

如果您对发生的事情有任何想法可以拯救我。我好几天都在讨厌这个问题。非常感谢你

2 个答案:

答案 0 :(得分:16)

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

func mapView(MapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == annotationView.rightCalloutAccessoryView {
        let segueID: String = "abc" // Use the appropriate segue ID here
        performSegueWithIdentifier(segueID, sender: self)
    }
}

openCamera 方法中删除 cameraId local variable

imer timer= new Timer();
     timer.schedule(
                    new TimerTask() {
                        @Override
                        public void run() {
                            runOnUiThread(
                                    new Runnable() {
                                        @Override
                                        public void run() {

                                    **thise code run every 100ms**

                                }
                            }
                    );
                }
            }, 0, 100);

答案 1 :(得分:10)

在看了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;
            }
            ...