public static void setBestExposure(Camera.Parameters parameters, boolean lightOn) { int minExposure = parameters.getMinExposureCompensation(); int maxExposure = parameters.getMaxExposureCompensation(); float step = parameters.getExposureCompensationStep(); if ((minExposure != 0 || maxExposure != 0) && step > 0.0f) { // Set low when light is on float targetCompensation = lightOn ? MIN_EXPOSURE_COMPENSATION : MAX_EXPOSURE_COMPENSATION; int compensationSteps = Math.round(targetCompensation / step); float actualCompensation = step * compensationSteps; // Clamp value: compensationSteps = Math.max(Math.min(compensationSteps, maxExposure), minExposure); if (parameters.getExposureCompensation() == compensationSteps) { Log.i( TAG, "Exposure compensation already set to " + compensationSteps + " / " + actualCompensation); } else { Log.i( TAG, "Setting exposure compensation to " + compensationSteps + " / " + actualCompensation); parameters.setExposureCompensation(compensationSteps); } } else { Log.i(TAG, "Camera does not support exposure compensation"); } }