使用Android拍摄单色照片(黑白)

我想在我的应用程序中拍摄真黑白照片。 我搜索了解决方案(也在这个网站上),但我总是找到解决方案将照片放在灰度(例如在本主题中 ),但它不是我要找的…

我还找到了一个提出这个主题的话题:

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  255) { R = 255; } G = Color.red(pixel); G = (int) (((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0); if (G  255) { G = 255; } B = Color.red(pixel); B = (int) (((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0); 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位黑/白,则可以使用简单(慢速)阈值算法

 public static Bitmap createBlackAndWhite(Bitmap src) { 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; // 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); R = Color.red(pixel); G = Color.green(pixel); B = Color.blue(pixel); int gray = (int) (0.2989 * R + 0.5870 * G + 0.1140 * B); // use 128 as threshold, above -> white, below -> black if (gray > 128) gray = 255; else gray = 0; // set new pixel color to output bitmap bmOut.setPixel(x, y, Color.argb(A, gray, gray, gray)); } } return bmOut; } 

但是根据看起来不太好的结果,为了获得更好的结果,您需要一个抖动算法,请参阅算法概述 – 这是一个阈值方法。


对于256级灰度转换:

根据http://www.mathworks.de/help/toolbox/images/ref/rgb2gray.html你计算每个像素的灰度值为gray = 0.2989 * R + 0.5870 * G + 0.1140 * B这将转换为

 public static Bitmap createGrayscale(Bitmap src) { 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; // 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); R = Color.red(pixel); G = Color.green(pixel); B = Color.blue(pixel); int gray = (int) (0.2989 * R + 0.5870 * G + 0.1140 * B); // set new pixel color to output bitmap bmOut.setPixel(x, y, Color.argb(A, gray, gray, gray)); } } return bmOut; } 

但这很慢,因为你必须分别为数百万像素做这件事。

https://stackoverflow.com/a/9377943/995891有更好的方法来实现同样的目标。

 // code from that answer put into method from above public static Bitmap createGrayscale(Bitmap src) { int width = src.getWidth(); int height = src.getHeight(); Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmOut); ColorMatrix ma = new ColorMatrix(); ma.setSaturation(0); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(ma)); canvas.drawBitmap(src, 0, 0, paint); return bmOut; } 

G = Color.red(像素);

G =颜色。 绿色 (像素);

B = Color.red(像素);

B =颜色。 蓝色 (像素);

看看这种变化(以粗体显示)是否有帮助。