My camera app preview is too dark in low light. If I open my google camera it will increase brightness inside the preview so that our face is visible to take photos. But my preview is completely dark. I have handled the brightness and lightsensor. My Lightsensor works when is some light. I need to make preview is visible. Let me what should I have to handle?
public void initialBrightness() {
try {
brightnessMode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
brightnessModeAuto = true;
}
Settings.System.putInt(this.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 95);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100;
getWindow().setAttributes(lp);
}
I'm calling this method in onCreate method before camera preview class is called.
To solve the problem you have to unlock autoExposureCompensation and set to the MAX value:
Camera.Parameters params = mCamera.getParameters();
params.setExposureCompensation(params.getMaxExposureCompensation());
if(params.isAutoExposureLockSupported()) {
params.setAutoExposureLock(false);
}
mCamera.setParameters(params);
According to Android Develop > Reference > Camera.Parameters
Changing the white balance mode with setWhiteBalance(String) will release the auto-white balance lock if it is set.
Exposure compensation, AE lock, and AWB lock can be used to capture an exposure-bracketed burst of images, for example. Auto-white balance state, including the lock state, will not be maintained after camera release() is called.
Locking auto-white balance after open() but before the first call to startPreview() will not allow the auto-white balance routine to run at all, and may result in severely incorrect color in captured images.
So be careful and use the mentioned code after the first call to startPreview()
Try the below code. It worked for me.
previewRequestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, getRange());//This line of code is used for adjusting the fps range and fixing the dark preview
previewRequestBuilder.set(CaptureRequest.CONTROL_AE_LOCK, false);
previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
getRange() function as belw:
private Range<Integer> getRange() {
CameraManager mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics chars = null;
try {
chars = mCameraManager.getCameraCharacteristics(cameraId);
} catch (CameraAccessException e) {
e.printStackTrace();
}
Range<Integer>[] ranges = chars.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
Range<Integer> result = null;
for (Range<Integer> range : ranges) {
int upper = range.getUpper();
// 10 - min range upper for my needs
if (upper >= 10) {
if (result == null || upper < result.getUpper().intValue()) {
result = range;
}
}
}
return result;
}
What worked for me, after hours of testing with different settings was to reduce Frames per Second.
By doing so I guess the Automatic Exposure correction has enough time to compute the best settings for the available light.
So, reducing from 30 fps to 10 fps made it.
mCameraSource = new CameraSource.Builder(context, detector)
.setRequestedPreviewSize(640, 480)
.setFacing(CameraSource.CAMERA_FACING_FRONT)
.setRequestedFps(10.0f)
.build();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With