Bufferedimage 位掩码操作 - 使用另一个图像作为掩码将颜色应用于图像
我有两个 BufferedImage 对象,src
和 dest
。两者都是灰度,src
是 1bpc(基本上是黑白),dest
实际上可以是任何类型的色彩空间/bpc/等。
我需要能够使用 src
作为位掩码在 dest
上绘制一些颜色。基本上,如果 src 中的像素是黑色,那么 dest 应该更改为绘制颜色。但如果src
中的像素是白色,则dest
应该保持不变。
如果重要的话,我还在绘制操作期间应用仿射变换。
Graphics2D g = dest.createGraphics();
// do something here???
g.drawImage(src, transform, null);
g.dispose();
在纯黑白世界中,这将涉及到像素值的简单 |
- 但似乎可能有一种使用图像操作来完成此操作的正确方法。
直觉告诉我,这是设置 Composite 和某种 alpha 的问题 - 但我完全不知道要使用什么值。我对 2d 图形的更高级方面几乎没有经验 - 任何指针将不胜感激。
I have two BufferedImage objects, src
and dest
. Both are grayscale, src
is 1bpc (B&W basically), and dest
could really be any sort of color space/bpc/etc.
I need to be able to draw some color onto dest
using src
as a bitmask. Basically, if the pixel in src
is black, then dest should be changed to the draw color. But if the pixel in src
is white, the dest
should be left alone.
If it matters, I am also applying an affine transform during the draw operation.
Graphics2D g = dest.createGraphics();
// do something here???
g.drawImage(src, transform, null);
g.dispose();
In a pure B&W world this would involve a simple |
of the pixel values together - but it seems like there's probably a correct way to do this using image operations.
Gut instinct says that this is a matter of setting the Composite and some sort of alpha - but I'm at a total loss as to what values to use. I have very little experience with the more advanced aspects of graphics 2d - any pointers would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为我已经使用 这篇文章想出了一个有效的解决方案< /a>
这当然是有效的,尽管我不确定是否遵循最佳实践:
I think that I've come up with an effective solution using this article
This is certainly efficient, although I'm not sure if follows best practices or not: