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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12040526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:22:47  来源:igfitidea点击:

Take monochrome picture (black and white) with Android

javaandroidandroid-cameraphotoimage

提问by Vince

I would like to take a picture in true black and white in my app. I searched for solutions (in this site too), but I always found solution to put a photo in gray scale (for example in this topic), but it's not what I am looking for ...

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

I also found a topic proposing this :

我还发现了一个提出这个的主题:

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;

    }

But the image quality is horrible ...

但是画质太差了。。。

Is anyone having an idea please?

有人有想法吗?

Thank you

谢谢

回答by zapl

If you like the image to be 1bit black/white you can use a simple (& slow) threshold algorithm

如果您喜欢图像为 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;
}

But depending on what that will not look good, for better results you need a dithering algorithm, see Algorithm overview- this one is the threshold method.

但是取决于看起来不太好的东西,为了获得更好的结果,您需要一种抖动算法,请参阅算法概述- 这是阈值方法。



For 256 levels of gray conversion:

对于 256 级灰度转换:

according to http://www.mathworks.de/help/toolbox/images/ref/rgb2gray.htmlyou calculate the gray value of each pixel as gray = 0.2989 * R + 0.5870 * G + 0.1140 * Bwhich would translate to

根据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;
}

But that is pretty slow since you have to do that for millions of pixels separately.

但这很慢,因为您必须分别为数百万个像素执行此操作。

https://stackoverflow.com/a/9377943/995891has a much nicer way of achieving the same.

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

回答by C2940680

G = Color.red(pixel);

G = Color.red(像素);

G = Color.green(pixel);

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

B = Color.red(pixel);

B = Color.red(像素);

B = Color.blue(pixel);

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

See if this changes (in bold) helps.

看看这个变化(粗体)是否有帮助。