感谢Rob的回答,我们现在知道为什么颜色混乱了.
问题是双重的:
> ImageIO用于编写JPEG的默认JPEGImageWriter,不会以其他软件理解的方式编写带有alpha的JPEG(这是一个已知问题).
>当将null作为目标传递给ResampleOp.filter(src,dest)并且过滤器方法是FILTER_TRIANGLE时,将创建一个新的BufferedImage,其中包含alpha(实际上是BufferedImage.TYPE_INT_ARGB).
重新采样后剥离alpha将起作用.但是,还有另一种方法可能更快并节省一些内存.也就是说,不是传递空目标,而是传递适当大小和类型的BufferedImage:
public static void main(String[] args) throws IOException {
// Read input
File input = new File(args[0]);
BufferedImage inputImage = ImageIO.read(input);
// Make any transparent parts white
if (inputImage.getTransparency() == Transparency.TRANSLUCENT) {
// NOTE: For BITMASK images, the color model is likely IndexColorModel,
// and this model will contain the "real" color of the transparent parts
// which is likely a better fit than unconditionally setting it to white