Java ImageIO.write bmp 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18956941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:54:02  来源:igfitidea点击:

ImageIO.write bmp does not work

javabmp

提问by Jakob Mathiasen

I'm trying to save an image in bmp format, but it doesn't create any file. If I use "png" instead, everything works fine. Any ideas?

我正在尝试以 bmp 格式保存图像,但它不会创建任何文件。如果我改用“png”,则一切正常。有任何想法吗?

//This works fine:
ImageIO.write(bi, "png", new File("D:\MyImage.png"));

//This does not work:
ImageIO.write(bi, "bmp", new File("D:\MyImage.bmp"));

ImageIO.getWriterFormatNames()gives me "jpg", "bmp", "jpeg" and some others..

ImageIO.getWriterFormatNames()给我“jpg”、“bmp”、“jpeg”和其他一些..

Thanks in advance.

提前致谢。

Jakob

雅各布

回答by Heaven42

didn't try but I think the format should be "BMP" and not "bmp" actually. Please try with

没有尝试,但我认为格式实际上应该是“BMP”而不是“bmp”。请尝试

ImageIO.write(bi, "BMP", new File("D:\MyImage.bmp"));

and see what happens.

看看会发生什么。

We can't see how your bi is build.

我们看不到你的 bi 是如何构建的。

BufferedImage bufferedImage = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);

Is the encodingType is set properly ?

encodingType 设置是否正确?

I think your bi is corrupted, that's work perfectly for me.

我认为您的 bi 已损坏,这对我来说非常适合。

BufferedImage bi = new BufferedImage(50,50,BufferedImage.TYPE_INT_RGB);
Graphics gd = bi.getGraphics();
gd.drawRect(0, 0, 10, 10);      
try {
    ImageIO.write(bi, "BMP", new File("C:\test.bmp"));
    ImageIO.write(bi, "PNG", new File("C:\test.png"));
} catch (IOException e) {
    System.out.println("error "+e.getMessage());
}

回答by bincob

I just finished debugging a similar problem and I thought I will present my reasoning here, although Jakob has gone ahead with the PNG format.

我刚刚调试完一个类似的问题,我想我会在这里展示我的推理,尽管 Jakob 已经使用了 PNG 格式。

First, always check the return value of ImageIO.write(...). It will return false if no appropriate writer can be found and that's what should have happened when Jakob tried writing it as a bitmap. This happens when the actual image format of the file does not match what is given in the 'format name' argument. No exception is thrown in this case. Check out the docs at http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#write(java.awt.image.RenderedImage, java.lang.String, java.io.File)

首先,始终检查ImageIO.write(...)的返回值。如果找不到合适的编写器,它将返回 false,这就是当 Jakob 尝试将其编写为位图时应该发生的情况。当文件的实际图像格式与“格式名称”参数中给出的格式不匹配时,就会发生这种情况。在这种情况下不会抛出异常。查看http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#write(java.awt.image.RenderedImage, java.lang.String, java.io 。文件)

Second, check the image type of the BufferedImageobject by using the BufferedImage#getType()method. Check out the possible return values at http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#getType(). For example, If you get the type as TYPE_INT_ARGB from your BufferedImageobject (which represents a PNG with a alpha component) you wont have success using ImageIO.write(bi, "BMP", new File("D:\\test.bmp"))and the method would return false, even though you can see BMP/bmp in the list of entries obtained using ImageIO.getWriterFormatNames(). You might have to work on the encoding and transform your image to the desired format.

其次,使用BufferedImage#getType()方法检查BufferedImage对象的图像类型。在http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#getType()查看可能的返回值。例如,如果您从BufferedImage对象(代表带有 alpha 分量的 PNG)中获得类型为 TYPE_INT_ARGB,您将无法成功使用并且该方法将返回false,即使您可以在获得的条目列表中看到 BMP/bmp使用. 您可能需要进行编码并将图像转换为所需的格式。ImageIO.write(bi, "BMP", new File("D:\\test.bmp"))ImageIO.getWriterFormatNames()

Third, when facing such problems which can be a pain sometimes, it always helps to use an image editor such as GIMPto check out your image properties in detail.

第三,当遇到这些有时会很痛苦的问题时,使用图像编辑器(例如GIMP)来详细检查图像属性总是有帮助的。

@Green arrow, a minor note... you can use either "bmp" or "BMP" as the image format value. The same applies for other formats as well. It does not matter.

@绿色箭头,一个小提示......您可以使用“bmp”或“BMP”作为图像格式值。这同样适用于其他格式。没关系。

回答by Sefier Tang

As @bincob says, if write returns false, you can redraw the source image like this

正如@bincob 所说,如果 write 返回 false,您可以像这样重绘源图像

BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), 
bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);

And then you can write again.

然后你就可以再写了。