从图像中剥离Alpha通道

Rob*_*Rob 5 java bufferedimage alpha twelvemonkeys

我想从PNG中剥离Alpha通道(透明背景),然后将它们写为JPEG图像.更准确地说,我想将透明像素设为白色.我尝试了两种技术,两种技术都以不同的方式失败:

方法1:

BufferedImage rgbCopy = new BufferedImage(inputImage.getWidth(), inputImage.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = rgbCopy.createGraphics();
graphics.drawImage(inputImage, 0, 0, Color.WHITE, null);
graphics.dispose();
return rgbCopy;
Run Code Online (Sandbox Code Playgroud)

结果:图像具有粉红色背景.

方法2:

final WritableRaster raster = inputImage.getRaster();
final WritableRaster newRaster = raster.createWritableChild(0, 0, inputImage.getWidth(), inputImage.getHeight(), 0, 0, new int[]{0, 1, 2});
ColorModel newCM = new ComponentColorModel(inputImage.getColorModel().getColorSpace(), false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
return new BufferedImage(newCM, newRaster, false, null);
Run Code Online (Sandbox Code Playgroud)

结果:图像为黑色背景.

在这两种情况下,输入图像都是PNG,输出图像以JPEG格式写入,如下所示:ImageIO.write(bufferedImage, "jpg", buffer).如果它是相关的:这是在Java 8上,我使用12monkeys库来调整图像大小,然后再将其写为JPEG.

我已经尝试了上面代码的一些变体,没有运气.以前的一些问题提出了上述代码,但在这种情况下似乎并没有起作用.

har*_*ldK 6

感谢Rob的回答,我们现在知道为什么颜色混乱了.

问题是双重的:

  • 默认JPEGImageWriter是ImageIO使用写JPEG,不与方式等软件理解阿尔法写JPEG文件(这是一个已知的问题).
  • 当null作为目的地传递给ResampleOp.filter(src, dest)过滤器方法时FILTER_TRIANGLE,BufferedImage将创建一个带有alpha(实际上BufferedImage.TYPE_INT_ARGB)的new .

重新采样后剥离alpha将起作用.但是,还有另一种方法可能更快并节省一些内存.也就是说,不是传递null目的地,而是传递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.

        // Fill background  with white
        Graphics2D graphics = inputImage.createGraphics();
        try {
            graphics.setComposite(AlphaComposite.DstOver); // Set composite rules to paint "behind"
            graphics.setPaint(Color.WHITE);
            graphics.fillRect(0, 0, inputImage.getWidth(), inputImage.getHeight());
        }
        finally {
            graphics.dispose();
        }
    }

    // Resample to fixed size
    int width = 100;
    int height = 100;

    BufferedImageOp resampler = new ResampleOp(width, height, ResampleOp.FILTER_TRIANGLE);

    // Using explicit destination, resizedImg will be of TYPE_INT_RGB
    BufferedImage resizedImg = resampler.filter(inputImage, new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB));

    // Write output as JPEG
    ImageIO.write(resizedImg, "JPEG", new File(input.getParent(), input.getName().replace('.', '_') + ".jpg"));
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这就像一个魅力.对于其他任何查看此解决方案的人来说,如果您只使用代码的一部分来填充Alpha通道(即不调整大小),则需要将图像复制到TYPE_INT_RGB的新图像,否则ImageIO会错误地写入. (3认同)