简体   繁体   English

BufferedImage:将所有像素设置为白色,其中alpha = 0

[英]BufferedImage: Set all pixels white where alpha = 0

I have a BufferedImage and would like to set all pixels that are fully transparent to be fully-transparent white (rather than transparent blank, or whatever may be in the source file). 我有一个BufferedImage,并希望将完全透明的所有像素设置为完全透明的白色(而不是透明的空白,或源文件中的任何像素)。 I can of course loop through the entire image using getRGB and setRGB, but is there some other way that would be much faster? 我当然可以使用getRGB和setRGB遍历整个图像,但是还有其他方法会更快吗?

You can set the pixels like this: 您可以这样设置像素:

public void setRGB(int startX,
               int startY,
               int w,
               int h,
               int[] rgbArray,
               int offset,
               int scansize)

This method sets an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB color space, into a portion of the image data. 此方法将默认RGB颜色模型(TYPE_INT_ARGB)和默认sRGB颜色空间中的整数像素数组设置为图像数据的一部分。 Color conversion takes place if the default model does not match the image ColorModel. 如果默认模型与图像ColorModel不匹配,则会进行颜色转换。 There are only 8-bits of precision for each color component in the returned data when using this method. 使用此方法时,返回数据中每个颜色分量的精度只有8位。 With a specified coordinate (x, y) in the this image, the ARGB pixel can be accessed in this way: 在此图像中使用指定的坐标(x,y),可以通过以下方式访问ARGB像素:

 pixel   = rgbArray[offset + (y-startY)*scansize + (x-startX)];

I can't say for sure if it is faster, but take a look at the ColorConvertOp class. 我不能肯定地说是否更快,但是请看一下ColorConvertOp类。

I haven't used it personally, but it might be what you are looking for. 我没有亲自使用过它,但是它可能正是您想要的。

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

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