我正在尝试使用imageIO将gif转换为jpeg,但生成的图像是粉红色的......任何人都可以提供帮助吗?
public byte[] convert(byte[] bytes)
throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
BufferedImage bufferedImage = ImageIO.read(inputStream);
ByteArrayOutputStream osByteArray = new ByteArrayOutputStream();
ImageOutputStream outputStream = ImageIO.createImageOutputStream(osByteArray);
ImageIO.write(bufferedImage, "jpg", outputStream);
outputStream.flush();
outputStream.close();
return osByteArray.toByteArray();
}
答案 0 :(得分:2)
也许,粉红色被定义为gif图像的透明色。如果是这样,以下示例可能会起作用。基本上,创建一个新图像,并将“背景颜色”显式设置为传入的任何内容。
public static byte[] convert(byte[] bytes, Color backgroundColor) throws Exception
{
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
BufferedImage bufferedImage = ImageIO.read(inputStream);
BufferedImage newBi = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) newBi.getGraphics();
g2d.drawImage(bufferedImage, 0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), backgroundColor, null);
bufferedImage.getHeight(), null);
ByteArrayOutputStream osByteArray = new ByteArrayOutputStream();
ImageOutputStream outputStream = ImageIO.createImageOutputStream(osByteArray);
ImageIO.write(newBi, "jpg", outputStream);
outputStream.flush();
outputStream.close();
return osByteArray.toByteArray();
}
看起来this可能是相关的。