Android Camera2手电筒的手电筒请求中断了记录视图

时间:2019-02-13 03:08:46

标签: java android android-camera2 flashlight

我正在尝试使用Android Camera2 API在基于本教程的功能齐全的相机应用程序之上实现闪光灯功能,该应用程序不包含闪光灯功能:https://github.com/mobapptuts/android_camera2_api_video_app/blob/master/app/src/main/java/mobapptut/com/camera2videoimage/Camera2VideoImageActivity.java

我的手电筒代码可以正常工作,并且可以按预期方式打开和关闭手电筒,但是拍照后只要打开手电筒,记录屏幕就会冻结。

这是我用来打开和关闭手电筒的代码:

public void turnOnFlashLight() {
    if (!isFlashlightOn) {
        isFlashlightOn = true;
        captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
        try {
            recordCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
}

public void turnOffFlashLight() {
    if (isFlashlightOn) {
        isFlashlightOn = false;
        captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
        try {
            recordCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
}

在拍照之前执行上面的代码是完全可以的,但是拍照之后,这两个功能都会触发ImageReader.OnImageAvailableListener反复不停,我认为这是录制屏幕冻结的原因。如何防止手电筒代码触发imageReader的侦听器?

仅供参考,以下是设置captureRequestBuilder和recordCaptureSession的代码。它们全部基于教程,并且在我实现手电筒功能之前可以正常工作。

private void startStillCaptureRequest() {
    try {
        if(isRecording()) {
            captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_VIDEO_SNAPSHOT);
        } else {
            captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        }
        captureRequestBuilder.addTarget(imageReader.getSurface());

        CameraCaptureSession.CaptureCallback stillCaptureCallback = new CameraCaptureSession.CaptureCallback() {};
        if(isRecording()) {
            recordCaptureSession.capture(captureRequestBuilder.build(), stillCaptureCallback, null);
        } else {
            previewCaptureSession.capture(captureRequestBuilder.build(), stillCaptureCallback, null);
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

private void startRecord () {
    try {
        setupMediaRecorder();
        SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
        surfaceTexture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
        Surface previewSurface = new Surface(surfaceTexture);
        Surface recordSurface = mediaRecorder.getSurface();
        captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
        captureRequestBuilder.addTarget(previewSurface);
        captureRequestBuilder.addTarget(recordSurface);

        cameraDevice.createCaptureSession(Arrays.asList(previewSurface, recordSurface, imageReader.getSurface()),
                new CameraCaptureSession.StateCallback() {
                    @Override
                    public void onConfigured(CameraCaptureSession session) {
                        recordCaptureSession = session;
                        try {
                            recordCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, null);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onConfigureFailed(CameraCaptureSession session) {
                        Log.d(LOGTAG, "onConfigureFailed: startRecord");
                    }
                }, null);
    } catch (IOException | CameraAccessException e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案