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?