java 黑白照片_使用Android拍摄黑白照片(黑白) - java

我想在我的应用中以真实的黑白照片。我搜索了解决方案(也在此站点中),但是我总是找到将照片放成灰度级的解决方案(例如this topic),但这不是我想要的...

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

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;

}

但是图像质量太差了...

请问有人有主意吗?

谢谢

参考方案

如果您希望图像为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;

}

但是,根据看起来不太好的东西,要获得更好的结果,您需要使用抖动算法,请参阅Algorithm overview-这是阈值方法。

对于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;

}

Java-将int更改为ascii - java

java有没有办法将int转换为ascii符号? 参考方案 您是否要将int转换为char?:int yourInt = 33; char ch = (char) yourInt; System.out.println(yourInt); System.out.println(ch); // Output: // 33 // ! 还是要将int转换为Stri…Java:正则表达式模式匹配器是否有大小限制? - java

我的模式类似于OR:“word1 | word2 | word3”我大约有800个字。可能有问题吗? 参考方案 您仅受记忆和理智的限制。 :)Java:线程池如何将线程映射到可运行对象 - java

试图绕过Java并发问题,并且很难理解线程池,线程以及它们正在执行的可运行“任务”之间的关系。如果我创建一个有10个线程的线程池,那么我是否必须将相同的任务传递给池中的每个线程,或者池化的线程实际上只是与任务无关的“工人无人机”可用于执行任何任务?无论哪种方式,Executor / ExecutorService如何将正确的任务分配给正确的线程? 参考方案 …JAVA:字节码和二进制有什么区别? - java

java字节代码(已编译的语言,也称为目标代码)与机器代码(当前计算机的本机代码)之间有什么区别?我读过一些书,他们将字节码称为二进制指令,但我不知道为什么。 参考方案 字节码是独立于平台的,在Windows中运行的编译器编译的字节码仍将在linux / unix / mac中运行。机器代码是特定于平台的,如果在Windows x86中编译,则它将仅在Win…java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值