0

I am trying to take picture from service using camera2 api. I am getting pictures too much dark.I tried setting CONTROL_AE_EXPOSURE_COMPENSATION and used different camera templates but it did not help. Can anyone please help me why I am getting dark pictures? here is my code:

private final ImageReader.OnImageAvailableListener onImageAvailableListener = (ImageReader imReader)->  {
    //get image and processes

};
private void takePicture() throws CameraAccessException {
    if (null == cameraDevice) {
        return;
    }
    final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
    Size[] jpegSizes = null;
    StreamConfigurationMap streamConfigurationMap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);//setting streamConfigurationMap
    if (streamConfigurationMap != null) {
        jpegSizes = streamConfigurationMap.getOutputSizes(ImageFormat.JPEG);
    }
    final boolean jpegSizesNotEmpty = jpegSizes != null && 0 < jpegSizes.length;
    int width = jpegSizesNotEmpty ? jpegSizes[0].getWidth() : 640;
    int height = jpegSizesNotEmpty ? jpegSizes[0].getHeight() : 480;
    final ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
    final List<Surface> outputSurfaces = new ArrayList<>();
    outputSurfaces.add(reader.getSurface());
    final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);//setting template
    captureBuilder.addTarget(reader.getSurface());
    captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);//setting CONTROL_MODE
    captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation());//setting mobile orientation
    captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);//setting ae mode
    captureBuilder.set(CaptureRequest.CONTROL_AWB_MODE, CaptureRequest.CONTROL_AWB_MODE_AUTO);//setting white balance
    captureBuilder.set(CaptureRequest.COLOR_CORRECTION_ABERRATION_MODE, CameraMetadata.COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY);//setting color correction
    captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);//setting auto focus
    captureBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START);//setting auto focus


    reader.setOnImageAvailableListener(onImageAvailableListener, null);
    cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(@NonNull CameraCaptureSession session) {
                    cameraCaptureSession = session;
                    try {//taking picture
                        HandlerThread handlerThread = new HandlerThread("MyHandlerThread");
                        handlerThread.start();
                        Looper looper = handlerThread.getLooper();
                        Handler handler = new Handler(looper);
                        cameraCaptureSession.capture(captureBuilder.build(), captureListener,handler);
                    } catch (final CameraAccessException e) {
                        Log.e("error capture" , e.getMessage());
                    }

                }

                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession session) {//

                }
            }
            , null);
}

Edit: My application does not have any UI, total program running in background only. I've tested with other (samsung)mobiles its working but with same android version(6.0.1) images are too much dark in Xiaomi mobile. I tried adding delay ,tried with TEMPLATE_PREVIEW and left 50 first images nothing worked for me.any help?

gss2408
  • 13
  • 5
  • 1
    possible duplicate: [Pictures with Camera2 API are really dark](https://stackoverflow.com/questions/31925769) – Alex Cohn Sep 03 '18 at 14:43
  • Tried it still not working – gss2408 Sep 04 '18 at 06:51
  • I know it's frustrating, and each device adds more challenges. You are actually "lucky" that your Samsung devices behave consistently. This is not what many developers experience these days. Android version 6.0.1 has very little to do with camera implementations. There are quite a few devices that were released with Android 7 but still only feature LEGACY Camera2 support. BTW, isn't your Xiaomi mobile one of such devices? – Alex Cohn Sep 04 '18 at 15:12
  • Thanks for help, I found that My Xiaomi mobile only features LEGACY Camera2 support. Should i go for other api? Do you know any other camera api that will let me take pictures in background service? I know that Android P does not let you use camera in background in ideal state but i still want to try in other android versions. – gss2408 Sep 05 '18 at 07:48
  • Definitely don't use **camera2** on LEGACY devices, it's all broken. Hopefully the *deprecated* [Camera API](https://developer.android.com/reference/android/hardware/Camera) is more flexible. At least, this approach removes an extra layer of indirection. This will not necessarily solve your problem, because as Eddy explains in the linked answer, the camera needs some time in preview regime to get exposure, focus, white balance, etc. converged. – Alex Cohn Sep 05 '18 at 14:12

0 Answers0