Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera Preview is too dark in low light android

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.

like image 593
Likith Ts Avatar asked Feb 10 '15 10:02

Likith Ts


3 Answers

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()

like image 77
Tiziano Munegato Avatar answered Oct 17 '22 19:10

Tiziano Munegato


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;
}
like image 7
vikoo Avatar answered Oct 17 '22 20:10

vikoo


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();
like image 5
Jaume Avatar answered Oct 17 '22 21:10

Jaume