Android Camera2 API设置了自定义亮度,对比度,伽玛

Vol*_*lyk 4 android android-camera android-camera2

由于还没有明确的答案,并且stackoverflow还没有关于Camera 2 API Gamma的问题/答案,因此我寻求使用Android Camera 2 API修改亮度,对比度和Gamma的解决方案。
我的代码来获取范围步骤

Rational controlAECompensationStep = characteristics.get(CameraCharacteristics.CONTROL_AE_COMPENSATION_STEP);
if (controlAECompensationStep != null) {
    compensationStep = controlAECompensationStep.doubleValue();
}

Range<Integer> controlAECompensationRange = characteristics.get(CameraCharacteristics.CONTROL_AE_COMPENSATION_RANGE);
if (controlAECompensationRange != null) {
    minCompensationRange = controlAECompensationRange.getLower();
    maxCompensationRange = controlAECompensationRange.getUpper();
}
Run Code Online (Sandbox Code Playgroud)

我的方法以百分比设置亮度

public void setBrightness(int value) {
    int brightness = (int) (minCompensationRange + (maxCompensationRange - minCompensationRange) * (value / 100f));
    previewRequestBuilder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, brightness);
    applySettings();
}
private void applySettings() {
    try {
        captureSession.setRepeatingRequest(previewRequestBuilder.build(), null, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是这种方法无法正常工作。像这里一样,图像变成绿色


我描述了我在文档中找到的所有内容(包括演示.apk)。

Vol*_*lyk 6

我的错误是在修改亮度(B),伽玛(G)或对比度(C)之前将自动白平衡模式(AWB)更改为CONTROL_AWB_MODE_OFF
不要OFFAWB,使用AUTO或其他可能的模式设置模式。


获得当前B的百分比

public int getBrightnessValue() {
    int absBRange = maxCompensationRange - minCompensationRange;
    int value = getValue(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION);
    return 100 * (value - minCompensationRange) / absBRange;
}
Run Code Online (Sandbox Code Playgroud)

将B设置为百分比

public void setBrightness(int value) {
    int brightness = (int) (minCompensationRange + (maxCompensationRange - minCompensationRange) * (value / 100f));
    previewRequestBuilder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, brightness);
    applySettings();
 }
Run Code Online (Sandbox Code Playgroud)

将C设置为百分比

//set def channels (used for contrast)
TonemapCurve tc = previewRequestBuilder.get(CaptureRequest.TONEMAP_CURVE);
if (tc != null) {
    channels = new float[3][];
    for (int chanel = TonemapCurve.CHANNEL_RED; chanel <= TonemapCurve.CHANNEL_BLUE; chanel++) {
        float[] array = new float[tc.getPointCount(chanel) * 2];
        tc.copyColorCurve(chanel, array, 0);
        channels[chanel] = array;
    }
}
Run Code Online (Sandbox Code Playgroud)


public void setContrast(int value) {
    final int minContrast = 0;
    final int maxContrast = 1;

    if (channels == null || value > 100 || value < 0) {
        return;
    }

    float contrast = minContrast + (maxContrast - minContrast) * (value / 100f);

    float[][] newValues = new float[3][];
    for (int chanel = TonemapCurve.CHANNEL_RED; chanel <= TonemapCurve.CHANNEL_BLUE; chanel++) {
        float[] array = new float [channels[chanel].length];
        System.arraycopy(channels[chanel], 0, array, 0, array.length);
        for (int i = 0; i < array.length; i++) {
            array[i] *= contrast;
        }
        newValues[chanel] = array;
    }
    TonemapCurve tc = new TonemapCurve(newValues[TonemapCurve.CHANNEL_RED], newValues[TonemapCurve.CHANNEL_GREEN], newValues[TonemapCurve.CHANNEL_BLUE]);
    previewRequestBuilder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE);
    previewRequestBuilder.set(CaptureRequest.TONEMAP_CURVE, tc);
    applySettings();
}
Run Code Online (Sandbox Code Playgroud)
private void applySettings() {
    captureSession.setRepeatingRequest(previewRequestBuilder.build(), null, null);
}
Run Code Online (Sandbox Code Playgroud)

G仍在进行中。


上面的代码示例可能不是100%正确,如果您有更好的解决方案或发现了错误,请告诉我 ;)