0

我正在尝试从具有透明而不是白色背景的 Java 显示中的图像创建一个 png。尝试在 new Color(0,0,0,0) 中将 alpha 设置为 0 - 背景仍然显示为白色?

我正在使用 BufferedImage

            WritableRaster 光栅 = onscreenImage.getRaster();
            WritableRaster newRaster;
            newRaster = raster.createWritableChild(0, 0, width, height, 0, 0, new int[] {0, 1, 2});
            DirectColorModel cm = (DirectColorModel) onscreenImage.getColorModel();
            DirectColorModel newCM = new DirectColorModel(cm.getPixelSize(),
                                                          cm.getRedMask(),
                                                          cm.getGreenMask(),
                                                          cm.getBlueMask());
            BufferedImage rgbBuffer = new BufferedImage(newCM, newRaster, false, null);
            尝试 { ImageIO.write(rgbBuffer, 后缀, 文件); }
            catch (IOException e) { e.printStackTrace(); }
4

1 回答 1

0

您没有在颜色模型中指定 alpha 分量。以这种方式尝试:

newRaster = raster.createWritableChild(0, 0, width, height, 0, 0, null);
DirectColorModel cm = (DirectColorModel) onscreenImage.getColorModel();
DirectColorModel newCM = new DirectColorModel(cm.getPixelSize(),
                                                      cm.getRedMask(),
                                                      cm.getGreenMask(),
                                                      cm.getBlueMask(),
                                                      cm.getAlphaMask());
于 2013-03-27T18:44:46.913 回答