Android Camera2 API设置了自定义亮度,对比度,伽玛 [英] Android Camera2 API set custom Brightness, Contrast, Gamma

查看:214
本文介绍了Android Camera2 API设置了自定义亮度,对比度,伽玛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 ,目前尚无明确答案,并且stackoverflow还没有问题/答案关于Camera 2 API Gamma,我要求使用 Android Camera 2 API .
我要获取 范围 的代码>和 步骤 :

Since this hasn't clear answer, and stackoverflow hasn't questions / answers about Camera 2 API Gamma, I ask for solution to modify Brightness, Contrast and Gamma using Android Camera 2 API.
My code to get range and step:

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

但是这种方法无法正常工作.图像变为绿色,例如

But this approach doesn't work correctly. Image becomes green, like here.

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

I described all what I found in documentation (demo .apk included).

推荐答案

我的错误在于更改 CONTROL_AWB_MODE_OFF ,然后再修改亮度(B),伽玛(G)或对比度(C).
不要为AWB设置OFF模式,不要使用AUTO或其他可能的模式.

My mistake was in changing Auto White Balance Mode(AWB) to CONTROL_AWB_MODE_OFF before modifying Brightness(B), Gamma(G) or Contrast(C).
Do not set OFF mode for AWB, use AUTO or other from possible modes.

获得当前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 仍在进行中.


G is still in progress.

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

这篇关于Android Camera2 API设置了自定义亮度,对比度,伽玛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆