7

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.

Likith Ts
  • 296
  • 1
  • 7
  • 18
  • please see here: http://stackoverflow.com/questions/7646865/changing-screen-brightness-programmatically-as-with-the-power-widget –  Feb 10 '15 at 10:17
  • In my question I have cleared mentioned I'm handling the brightness. I can increase the brightness. But there is something different which is making the preview dark . Please have a look on the question once again. – Likith Ts Feb 10 '15 at 10:27
  • @LikithTs Please post your code that shows how you set the brightness. – iRuth Feb 10 '15 at 15:56
  • I have tried to increase the brightness to 255 which is maximum. But this only increases the screen brightness. I have also used light sensor. My preview will be completely dark in low light. Light Sensor wont increase the preview brightness at that time. – Likith Ts Feb 10 '15 at 17:57
  • iRuth Please help me on this. I dont think is related to brightness, bcoz I have increased the brightness and checked. I donno why this people are down voting. – Likith Ts Feb 10 '15 at 18:38
  • @LikithTs Have you tried [setting white balance](http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setWhiteBalance(java.lang.String)) or [setting the exposure compensation](http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setExposureCompensation(int))? – iRuth Feb 10 '15 at 20:30
  • iRuth I have set White balance to auto. I'm setting the exposure compensation like this. int index = parameters.getExposureCompensation(); parameters.setExposureCompensation(index); . Bcoz exposure various with each phone. – Likith Ts Feb 11 '15 at 01:38
  • If I open my google camera in low light. It will make the preview more brighter, so face will be completely visible. In the same way if I open my camera app. It wont make my preview brighter. – Likith Ts Feb 11 '15 at 01:41

5 Answers5

11

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

Community
  • 1
  • 1
Tiziano Munegato
  • 869
  • 1
  • 7
  • 21
7

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;
}
HimalayanCoder
  • 9,630
  • 6
  • 59
  • 60
vikoo
  • 2,451
  • 20
  • 27
5

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();
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Jaume
  • 61
  • 1
  • 1
0

From my experiments, scene-mode setting can change the preview (unlike ISO or exposure-compensation, which both work for captured pictures). Don't use auto. Try scene-mode-values=night or scene-mode=dusk-dawn.

The problem with scenes is that the supported values are not standardized across devices. But some kind of night is usually present.

King of Masses
  • 18,405
  • 4
  • 60
  • 77
  • I have already implemented SCENE_MODE_NIGHT. And I have implemented when starting the preview. If I'm wrong correct me with some example code. – Likith Ts Feb 10 '15 at 10:58
  • 2
    I solved it by giving variable fps to the Camera. First call Camera.getParameter.getSupportedPreviewFpsRange(); In my case I got (15000 to 15000),(24000 to 24000),(30000 to 30000) and (15000,30000). I tried setting range from 15000 to 30000. Make sure that you are passing to different number. – King of Masses Feb 10 '15 at 11:07
0

By reading the open source code of Android Camera app here:

https://android.googlesource.com/platform/packages/apps/Camera.git/+/lollipop-release

It initialize the camera this way:

// Reset preview frame rate to the maximum because it may be lowered by
// video camera application.
 List<Integer> frameRates = parameters.getSupportedPreviewFrameRates();
        if (frameRates != null) {
            Integer max = Collections.max(frameRates);
            parameters.setPreviewFrameRate(max);
        }

And it works well in Android Nexus 5, lollipop.

Tom
  • 4,612
  • 4
  • 32
  • 43