java使用Android拍摄黑白照片
我想在我的应用程序中拍摄一张真正的黑白照片。我搜索了解决方案(也在这个网站上),但我总是找到将照片放在灰度级的解决方案(例如在this topic),但这不是我要找的
我还发现一个主题提出了以下建议:
public static Bitmap createContrast(Bitmap src, double value) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// get contrast value
double contrast = Math.pow((100 + value) / 100, 2);
// scan through all pixels
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
// apply filter contrast for every channel R, G, B
R = Color.red(pixel);
R = (int) (((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if (R < 0) {
R = 0;
} else if (R > 255) {
R = 255;
}
G = Color.red(pixel);
G = (int) (((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if (G < 0) {
G = 0;
} else if (G > 255) {
G = 255;
}
B = Color.red(pixel);
B = (int) (((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if (B < 0) {
B = 0;
} else if (B > 255) {
B = 255;
}
// set new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}
但是图像质量很糟糕
请问有人有什么想法吗
多谢各位
# 1 楼答案
如果你喜欢1比特的黑白图像,你可以使用一个简单的(慢)阈值算法
但是,要想获得更好的结果,你需要一个抖动算法,请参见Algorithm overview——这是一个阈值方法
对于256级灰度转换:
根据http://www.mathworks.de/help/toolbox/images/ref/rgb2gray.html计算每个像素的灰度值为
gray = 0.2989 * R + 0.5870 * G + 0.1140 * B
,这将转换为但这是相当缓慢的,因为你必须分别对数百万像素进行这项工作
https://stackoverflow.com/a/9377943/995891有一个更好的方法实现同样的目标
# 2 楼答案
G=颜色。红色(像素)
G=颜色绿色(像素)
B=颜色。红色(像素)
B=颜色蓝色(像素)
看看这些变化(粗体)是否有帮助