ImageIO:gif 到 jpeg 问题 ->图像变成粉红色

发布于 2024-10-13 02:52:19 字数 574 浏览 5 评论 0原文

我正在尝试使用 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();
}

I'm trying to convert a gif to a jpeg using imageIO but the resulting image is pink... Anyone can help?

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔小萌 2024-10-20 02:52:19

也许,粉红色被定义为 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();
}

看起来像 可能相关。

Perhaps, pink is defined as the transparency color for the gif image. If so, the following example might work. Basically, a new image is created and the "backgound color" is explicitly set to whatever is passed in.

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();
}

Looks like this might be related.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文