android - 如何在Android中将彩色图像变成黑白

标签 android colors

我想知道在 android 中将彩色图像(我从网上下载)转换为黑白图像的方法。 任何人都可以在您的任何 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 < 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;
}

在调用方法时将 double 值设置为 50。例如 createContrast(Bitmap src, 50)

关于android - 如何在Android中将彩色图像变成黑白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10185443/

相关文章:

android - 使用 Google IAB 显示产品的设备特定定价

java - 如何在recyclerview中放置横幅

android - 用 canvas 画画 Android

android - 从设置 Intent 返回应用程序

flutter - 改变 Flutter 的 FlatButton onPressed 的颜色

java - 如何更改新 Material 主题中后退箭头的颜色?

android - 如何使用新的 gradle experimental 插件防止自动生成 Android.mk

Python OpenCV 颜色跟踪

android - 更改微调器的背景和项目颜色

c++ - 将 UINT32 颜色格式从 AaBbGgRr 转换为 AaRrGgBb