如何在Android中使彩色图像变为黑白

如何在Android中使彩色图像变为黑白,第1张

概述我想知道当我在android中向用户显示彩色图像(我是从网上下载的)时将彩色图像转换成黑白图像的方法.   谁能在您的任何Android工作中找到此要求.请告诉我.谢谢拉克什曼解决方法:嗨,您可以使用对比使图像黑白色.参见代码.publicstaticBitmapcreateContrast(Bitmapsrc,do

我想知道当我在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;}

在Mathod调用中将double值设置为50.例如createContrast(Bitmap src,50)

总结

以上是内存溢出为你收集整理的如何在Android中使彩色图像变为黑白全部内容,希望文章能够帮你解决如何在Android中使彩色图像变为黑白所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1094954.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存