javaimagebufferedimagedevilavisynth

JAVA: How to create "png" image from BufferedImage. Image has to be openable in DevIL library


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


Solution

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