【发布时间】: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