ImageIO.write bmp不起作用 [英] ImageIO.write bmp does not work

查看:179
本文介绍了ImageIO.write bmp不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

//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()给我 jpg,bmp,jpeg和其他一些..

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

提前致谢。

雅各布

推荐答案

我刚刚完成调试类似的问题,我想我会在这里提出我的推理,尽管雅各布已经推出了PNG格式。

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.

首先,请务必检查 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.File)

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)

其次,检查图像类型通过使用 BufferedImage#getType()方法获取 BufferedImage 对象。查看上的可能返回值http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#getType()。例如,如果从 BufferedImage 对象(表示带有alpha组件的PNG)获取TYPE_INT_ARGB类型,则无法使用 ImageIO.write(bi,BMP)获得成功,新文件(D:\\test.bmp))并且该方法将返回 false ,即使您可以在列表中看到BMP / bmp使用 ImageIO.getWriterFormatNames()获得的条目。您可能必须处理编码并将图像转换为所需的格式。

Second, check the image type of the BufferedImage object 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 BufferedImage object (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.

第三,面对这些可能有时会出现问题的问题时,使用它总是有帮助的图像编辑器(如 GIMP )可详细查看图像属性。

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

@Green arrow,次要注释......您可以使用bmp或BMP作为图像格式值。这同样适用于其他格式。没关系。

@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.

这篇关于ImageIO.write bmp不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆