【问题标题】:Android Camera2 API set custom Brightness, Contrast, GammaAndroid Camera2 API 设置自定义亮度、对比度、伽玛
【发布时间】:2017-08-06 00:29:28
【问题描述】:

由于 this 没有明确的答案,并且 stackoverflow 没有关于 Camera 2 API Gamma 的问题/答案,我要求使用Android Camera 2 API 修改亮度、对比度和 Gamma 的解决方案。
我获取 rangestep 的代码:

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

我设置亮度百分比的方法

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

但是这种方法不能正常工作。图片变成green,就像here


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

【问题讨论】:

  • 你可以试试 Spectaculum,它适用于 Camera2 API:github.com/protyposis/…
  • @UmairAdil 感谢您的回复。但这不是我真正想要的。
  • @VladimirKulyk 我已经在 Nexus 5x 设备和 B+ 上安装了 apk,B- 工作正常。没有绿色?
  • @AnuragSingh ok :D 如果手动更改 B、G 或 C,我将 AutoWhiteBalance 设置为 false。它使我的预览变成绿色。
  • 对不起,我没明白。如果您需要更新apk,请这样做?

标签: android android-camera android-camera2


【解决方案1】:

我的错误是在修改亮度(B)、伽玛(G)或对比度(C)之前将Auto White Balance Mode(AWB)更改为CONTROL_AWB_MODE_OFF
不要为AWB 设置OFF 模式,使用AUTO 或其他可能的模式。


获取当前 B 的百分比

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

以百分比设置 B

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

以百分比设置 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;
    }
}


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

private void applySettings() {
    captureSession.setRepeatingRequest(previewRequestBuilder.build(), null, null);
}

G仍在进行中。


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

【讨论】:

  • 这里找不到getValue@getValue(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION);?
  • @ShylendraMadda private CaptureRequest.Builder previewRequestBuilder; Integer brightness = previewRequestBuilder.get(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION);
  • 如何获取通道数组进行对比
  • @SumitMarwha 看看//set def channels (used for contrast) 的评论和下面的代码。
  • TonemapCurve tc = previewRequestBuilder.get(CaptureRequest.TONEMAP_CURVE); --在resilt中获取null
猜你喜欢
  • 2013-03-02
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
  • 2016-05-28
相关资源
最近更新 更多