【问题标题】:Detect taken images brightness/darkness level - using Android CameraX检测拍摄的图像亮度/暗度级别 - 使用 Android CameraX
【发布时间】:2021-01-03 09:26:55
【问题描述】:

我正在使用 CameraX 在 Android 上捕获图像。 我想实现可以分析捕获的图像亮度/暗度级别的功能 - 如果图像太暗/太亮。

有什么优雅的方法可以做到这一点吗?也许是为此而设计的一些强大的灯光库?

目前的方法是在 Stackoverflow 上某处找到的代码片段:

public static boolean isDark(Bitmap bitmap){
    boolean dark=false;

    float darkThreshold = bitmap.getWidth()*bitmap.getHeight()*0.45f;
    int darkPixels=0;

    int[] pixels = new int[bitmap.getWidth()*bitmap.getHeight()];
    bitmap.getPixels(pixels,0,bitmap.getWidth(),0,0,bitmap.getWidth(),bitmap.getHeight());

    for(int pixel : pixels){
        int color = pixels[i];
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        double luminance = (0.299*r+0.0f + 0.587*g+0.0f + 0.114*b+0.0f);
        if (luminance<150) {
            darkPixels++;
        }
    }

    if (darkPixels >= darkThreshold) {
        dark = true;
    }
    long duration = System.currentTimeMillis()-s;
    return dark;
}

第二种方法是使用 SensorManager TYPE_LIGHT。还有其他想法/方法吗?

【问题讨论】:

  • Renderscript 可能会运行得更快,因为它将是 GPU 加速的,请查看 stackoverflow.com/questions/38908491/…,这与您的要求非常接近(尽管它只查看 RED 通道,很容易修改为测量亮度

标签: android image android-camerax


【解决方案1】:

更有效的方法是在不将输出转换为位图的情况下计算亮度。

private final ImageAnalysis.Analyzer mAnalyzer = image -> {
    byte[] bytes = new byte[image.getPlanes()[0].getBuffer().remaining()];
    image.getPlanes()[0].getBuffer().get(bytes);
    int total = 0;
    for (byte value : bytes) {
        total += value & 0xFF;
    }
    if (bytes.length != 0) {
        final int luminance = total / bytes.length;
        // luminance is the value you need.
    }
    image.close();
};

Source: CameraX test app source code

【讨论】:

  • 亮度的正确曝光值应该是多少?
猜你喜欢
  • 2020-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-13
  • 1970-01-01
  • 2011-10-18
相关资源
最近更新 更多