Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageIO.write bmp does not work

Tags:

java

bmp

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() gives me "jpg", "bmp", "jpeg" and some others..

Thanks in advance.

Jakob

like image 578
Jakob Mathiasen Avatar asked Sep 23 '13 10:09

Jakob Mathiasen


People also ask

How do I create a bitmap image in Java?

You can create a bitmap image in Java using the BufferedImage class and the setRGB() method. It provides us with a data buffer and various methods that we can use to manipulate the image data. To create a BufferedImage , we can use the BufferedImage() constructor.

What does ImageIO write do?

The ImageIO. write method calls the code that implements PNG writing a “PNG writer plug-in”. The term plug-in is used since Image I/O is extensible and can support a wide range of formats.

Can ImageIO read PNG?

imageio package. Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP. Image I/O is also extensible so that developers or administrators can "plug-in" support for additional formats.


3 Answers

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.

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)

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.

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

like image 51
bincob Avatar answered Oct 22 '22 15:10

bincob


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

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.

like image 29
Sefier Tang Avatar answered Oct 22 '22 16:10

Sefier Tang


Using BufferedImage.TYPE_INT_RGB encoding works for "gif","png","tif" as well as "jpg" and "bmp":

static void saveBufferedImageToFileTest(){

    String[] types = new String[] {"gif","png","tif","jpg","bmp"};

    //JPEG and BMP needs BufferedImage.TYPE_INT_RGB. See https://mkyong.com/java/convert-png-to-jpeg-image-file-in-java/
    //BufferedImage.TYPE_INT_RGB for all `types`
    int biType = BufferedImage.TYPE_INT_RGB;   // BufferedImage.TYPE_INT_ARGB does not work for "bmp" and "jpeg"
    BufferedImage bi = new BufferedImage(200 ,200, biType);
    Graphics g = bi.getGraphics();
    g.fillRect(50, 50, 100,  100);
    g.dispose();

    try {
        for(String type : types){
            boolean success = ImageIO.write(bi,type,new File("test_image."+type));
            System.out.println(type + (success ?  " file created" : " file NOT created") );
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
like image 30
c0der Avatar answered Oct 22 '22 16:10

c0der