简体   繁体   English

如何在Java中使透明图像不透明?

[英]How can I make a transparent image opaque in Java?

For a Java program ("regular" program, not Applet and/or Android etc.), I have PNGs with transparency which are to be loaded and drawn atop other pictures, which is working perfectly fine. 对于Java程序(“常规”程序,而不是Applet和/或Android等),我有透明的PNG,可以将其加载并绘制在其他图片上,效果很好。 Now, for the case that there is no other picture below them (the determination of whether that is the case is not an issue, the respective code is already workingly implemented), I want them made opaque, but not in the sense of simply setting a background color; 现在,对于下面没有其他图片的情况(确定是不是问题,相应的代码已经有效执行),我希望它们变得不透明,但不是简单地设置背景色; since the png source files concerned are fully colored, just with reduced opacity (alpha value) on them, it would, I guess, be no problem to simply increase the opacity of all the colors to 100%, I just don't know the commands to do it. 因为相关的png源文件是全彩色的,只是它们的不透明度(alpha值)降低了,所以我想简单地将所有颜色的不透明度提高到100%没问题,我只是不知道命令来做到这一点。 Preferred and so far targeted way of doing it is by having them drawn in a BufferedImage, then modified respectively, but as said: How? 到目前为止,首选的且有针对性的方式是将它们绘制在BufferedImage中,然后分别进行修改,但是要说:如何?

TLDR: images from .png with <100% opacity (same value on whole pic), how to make opaque in Java, preferrably with BufferedImage or even simpler TLDR:.png中具有<100%不透明度(整个图片的值相同)的图像,如何在Java中使其不透明,最好使用BufferedImage或更简单

thanks in advance for any answers 预先感谢您的任何答案

You can try RescaleOp to scale the alpha component all the way to opaque: 您可以尝试RescaleOp将Alpha分量一直缩放到不透明:

float[] scales = { 1f, 1f, 1f, 0f };  // R, G, B, A
float[] offsets = {0f, 0f, 0f, 255f};   // R, G, B, A
RescaleOp rescaler = new RescaleOp(scales, offsets, null);
BufferedImage opaque = rescaler.filter(original, null);

See this Java 2D tutorial for more info. 有关更多信息,请参见此Java 2D教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM