JPEG 写入器在写入文件时ImageIO
不会ColorModel
考虑,因为 JPEG 没有 alpha 通道。Alpha 通道是指定每个像素的透明度值的A
部分。大多数图像客户端会假设 JPEG 没有 alpha 值,因为这是标准行为。ARGB
作者ImageIO
将写入一个颜色变化的文件,就像图片中可爱的鲑鱼阴影作为背景一样。这是因为编写者在真正应该在 RBG 中写入值时会错误地写下这些值。
解决方法是使用 RBG 作为 ColorModel 将图像绘制到 BufferedImage。这是一些示例代码:
// argbBuffer is the ARGB Image that should be written as JPEG:
WritableRaster raster = argbBuffer.getRaster();
WritableRaster newRaster = raster.createWritableChild(0, 0, width, height, 0, 0, new int[] {0, 1, 2});
// create a ColorModel that represents the one of the ARGB except the alpha channel:
DirectColorModel cm = (DirectColorModel)argbBuffer.getColorModel();
DirectColorModel newCM = new DirectColorModel(cm.getPixelSize(),
cm.getRedMask(), cm.getGreenMask(), cm.getBlueMask());
// now create the new buffer that is used ot write the image:
BufferedImage rgbBuffer = new BufferedImage(newCM, newRaster, false, null);