Search code examples
androidtorchsamsung-mobileandroid-camera2

Android Camera2 - FLASH_MODE_TORCH not working with CONTROL_MODE_OFF on some devices (e.g. Samsung Galaxy S8)


I am creating a continuous capture session with the Torch enabled:

  captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                captureBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, new Range(Framerate, Framerate));
                captureBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, characteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE));
                captureBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_OFF);
                captureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);

                captureBuilder.set(CaptureRequest.SENSOR_EXPOSURE_TIME, 100000L);
                captureBuilder.set(CaptureRequest.SENSOR_FRAME_DURATION, 66666666L);
                captureBuilder.set(CaptureRequest.SENSOR_SENSITIVITY, 1000);
                captureBuilder.addTarget(imageReader.getSurface());
                mCaptureSession.setRepeatingRequest(captureBuilder.build(), callback, null);

It works well on many devices but on some others (for example the Samsung Galaxy S8), FLASH_MODE_TORCH seems to trigger some internal code which then overrides my CONTROL_MODE_OFF.

I can test this by checking the sensor value of the resulting frames:

CameraCaptureSession.CaptureCallback callback = new CameraCaptureSession.CaptureCallback() {
        @Override
        public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
            RealSensorSentivity = result.get(CaptureResult.SENSOR_SENSITIVITY);
            super.onCaptureCompleted(session, request, result);
        }
    };

Instead of getting 1000, I always get 64.

If I remove the FLASH_MODE_TORCH parameter, everything (except the torch) works well.

I tried to use the Camera1 API together with this and that didn't work. Also tried using the newer:

manager.setTorchMode(camId, true);

But that fails as the camera is already in use (weird).

What else can I do?


Solution

  • Turns out the main issue was:

    mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

    Devices have wildy different settings for this.

    I fixed it by using:

    mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_MANUAL);