gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:59:02 25 4
gpt4 key购买 nike

我想知道在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com