16

我正在尝试以 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()给我“jpg”、“bmp”、“jpeg”和其他一些..

提前致谢。

雅各布

4

5 回答 5

25

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.

于 2014-02-27T08:22:09.927 回答
7

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

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

然后你可以再写一次。

于 2016-06-07T07:14:56.240 回答
0

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

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

看看会发生什么。

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

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

encodingType 是否设置正确?

我认为您的 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());
}
于 2013-09-23T10:45:04.660 回答
0

老歌,但 BMP 偶尔仍然有用,最重要的答案是最好的解决方案:自己做。这样它适用于任何类型的位图。

static void writeBMP(BufferedImage image, File f) throws IOException {
    OutputStream out = new BufferedOutputStream(new FileOutputStream(f));
    int width = image.getWidth();
    int height = image.getHeight();
    int row = (width * 3 + 3) / 4 * 4;
    out.write('B');
    out.write('M');
    writeInt(out, 14 + 40 + row * height);  // file size
    writeInt(out, 0); 
    writeInt(out, 14 + 40);        // bitmap offset
    writeInt(out, 40);             // size
    writeInt(out, width);          // width
    writeInt(out, height);         // weight
    writeInt(out, (24<<16) | 1);   // planes, bpp
    writeInt(out, 0);              // compression
    writeInt(out, row * height);   // bitmap size
    writeInt(out, 0);              // resx
    writeInt(out, 0);              // resy
    writeInt(out, 0);              // used colors
    writeInt(out, 0);              // important colors
    for (int y=height-1;y>=0;y--) {
        for (int x=0;x<width;x++) {
            int rgba = image.getRGB(x, y); 
            out.write(rgba & 0xFF); // b
            out.write(rgba >> 8);   // g
            out.write(rgba >> 16);  // r
        }   
        for (int x=width*3;x%4!=0;x++) { // pad to 4 bytes
            out.write(0);
        }   
    }   
    out.close();
}   
private static void writeInt(OutputStream out, int v) throws IOException {
    out.write(v);
    out.write(v >> 8);
    out.write(v >> 16);
    out.write(v >> 24);
}   
于 2022-01-11T13:39:25.423 回答
0

BufferedImage.TYPE_INT_RGB对“gif”、“png”、“tif”以及“jpg”和“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();
    }
}
于 2021-01-14T06:43:04.733 回答