60

我有一个 BufferedImage 我正在尝试写入 jpeg 文件,但我的 Java 程序抛出异常。我能够成功地将相同的缓冲区保存为 gif 和 png。我曾尝试在 Google 上四处寻找解决方案,但无济于事。

代码:

   File outputfile = new File("tiles/" + row + ":" + col + ".jpg");
   try {
       ImageIO.write(mapBufferTiles[row][col], "jpg", outputfile);
   } catch (IOException e) {
        outputfile.delete();
        throw new RuntimeException(e);
   }

例外:

 Exception in thread "main" java.lang.RuntimeException: javax.imageio.IIOException: Invalid argument to native writeImage
 at MapServer.initMapBuffer(MapServer.java:90)
 at MapServer.<init>(MapServer.java:24)
 at MapServer.main(MapServer.java:118)
 Caused by: javax.imageio.IIOException: Invalid argument to native writeImage
 at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
 at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1055)
 at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:357)
 at javax.imageio.ImageWriter.write(ImageWriter.java:615)
 at javax.imageio.ImageIO.doWrite(ImageIO.java:1602)
 at javax.imageio.ImageIO.write(ImageIO.java:1526)
 at MapServer.initMapBuffer(MapServer.java:87)
 ... 2 more
4

5 回答 5

46

OpenJDK 没有原生 JPEG 编码器,尝试使用 Sun 的 JDK,或者使用库(如JAI

AFAIK,关于“粉红色”,Java 将 JPEG 保存为 ARGB(仍然带有透明度信息)。大多数观众在打开时假设四个通道必须对应于 CMYK(而不是 ARGB),因此是红色调。

但是,如果将图像导入回 Java,透明度仍然存在。

于 2010-08-07T23:57:33.993 回答
36

我在 OpenJDK 7 中遇到了同样的问题,我设法通过使用imageTypeofTYPE_3BYTE_BGR而不是TYPE_4BYTE_ABGR使用相同的 OpenJDK 来解决这个异常。

于 2013-07-24T22:12:05.003 回答
23

2019 答案:确保您的 BufferedImage 没有 alpha 透明度。JPEG 不支持 alpha,因此如果您的图像具有 alpha,则 ImageIO 无法将其写入 JPEG。

使用以下代码确保您的图像没有 alpha 透明度:

static BufferedImage ensureOpaque(BufferedImage bi) {
    if (bi.getTransparency() == BufferedImage.OPAQUE)
        return bi;
    int w = bi.getWidth();
    int h = bi.getHeight();
    int[] pixels = new int[w * h];
    bi.getRGB(0, 0, w, h, pixels, 0, w);
    BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    bi2.setRGB(0, 0, w, h, pixels, 0, w);
    return bi2;
}
于 2019-08-23T13:02:31.560 回答
5

这是一些代码来说明@Thunder 将图像类型更改为 TYPE_3BYTE_BGR 的想法

try {
  BufferedImage input = ImageIO.read(new File("input.png"));
  System.out.println("input image type=" + input.getType());
  int width = input.getWidth();
  int height = input.getHeight();
  BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
  int px[] = new int[width * height];
  input.getRGB(0, 0, width, height, px, 0, width);
  output.setRGB(0, 0, width, height, px, 0, width);
  ImageIO.write(output, "jpg", new File("output.jpg"));
} catch (Exception e) {
  e.printStackTrace();
}
于 2019-09-16T17:03:51.017 回答
1

你得到同样的错误

Caused by: javax.imageio.IIOException: Invalid argument to native writeImage
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1055)

如果您使用的是不受支持的色彩空间(在我的情况下为 CYMK)。请参阅如何在 Java 中正确地将 CMYK 转换为 RGB?如何解决这个问题。

于 2017-01-26T09:15:26.437 回答