1

I need to save BufferedImage as png image. At next step,I will need to open the image in another program(Avisynth). This program is able to open images, which I have drawn in ms-paint, but images created by my java program it is not able to open. Images from my program and from ms-paint are type of png, and in windows seems good. It means I am able to open it, and image contains all what I have drawn. In external program it throws following error:

Avisynth open failure:
ImageReader error 'Could not open file' in DevIL library.
reading image"C:\Images\mask\mirror.png"
DevIL version 166.
(C:\User\admin\Documents\4555.avs) 

here is code. I tried it even with commented line. I found something on google. This message is typical for bad image format. But I do not know how to do the same image like ms-paint does.

    BufferedImage img = new BufferedImage(video.getWidth(), video.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = (Graphics2D) img.getGraphics();
    img.getGraphics().setColor(Color.white);
    c.paintAll(img.getGraphics());
    File f = new File(path + ".png");

    opencv_core.IplImage imgs = IplImage.createFrom(img);
    opencv_highgui.cvSaveImage(f.getPath(), imgs);
    //ImageIO.write(img, "png", f);

In code:

c is JComponent, whitch contains the image, whitch I want to save, and I used JavaCL library

Ján Яabčan
  • 743
  • 2
  • 10
  • 20

2 Answers2

2

I've looked at your attached images, and I they both seem perfectly valid PNG files, and open fine in any viewer/library that I have tried. They both use standard deflate compression, adaptive filtering and are non-interlaced.

However, there's one important difference, and that is the one written from Java has transparency (alpha channel), while the one from Paint does not. None of the images have transparent pixels, so you could try to just throw away the alpha channel, and see if that helps (only change the first line of your code):

int w = video.getWidth();
int h = video.getHeight();
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); 

TYPE_3BYTE_BGR is what the ImageIO PNGReader returns for an opaque PNG, so it's probably faster, but you could just as well use TYPE_INT_RGB I think.

This small change in code, should produce an image that is closer to what Paint creates. I'm quite confident that this change should produce a PNG that DevIL/Avisynth can read.

(There's also another small difference between the images, that is the Paint image explicitly contains an sRGB chunk (and some other auxiliary chunks), but that shouldn't matter).

Other than that, I see that DevIL is using LibPNG (which is probably the most standard and widely used PNG library out there) to read PNGs, so I do find it kind of strange that it cannot read this particular PNG. But, it could be something about this version, the way its built or something (I'm no C/C++ programmer, nor do I know the library in depth). You should probably talk to (file an issue with) the developers of the library/program about this.

Harald K
  • 26,314
  • 7
  • 65
  • 111
1

To read/write a BufferedImage, you can use javax.imageio.ImageIO. You can also add the TwelveMonkeys extension which improves the I/O operations with imageio.

An other solution is to use JAI, but it has a memory leak issues when reading images.

The png format is a standard, supported by almost all the libraries/softwares. If Avisynth cannot open an image created with your java software, it is certainly because you saved it into a rare format.

FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
  • The TwelveMonkey speeds-up the IO operations on png... just in case you need better performances. – FiReTiTi Sep 07 '15 at 00:34
  • @FiReTiTi I'm always happy when someone refers to the TwelveMonkeys library (I'm its main author). However, in this case, I don't think it will make a difference. There's no PNG plugin in the library (..yet. I plan on adding a faster `PNGImageWriter` one day). Thanks for the confidence, though! ;-) – Harald K Sep 07 '15 at 08:40
  • Sorry, I believed I read somewhere that the TwelveMonkeys core improved the png IO operations :-( – FiReTiTi Sep 08 '15 at 06:37