简体   繁体   中英

Decoding with Qt an Image Encoded with Java ImageIO class

I want to display an image using C++, more precisely using Qt Framework. The image has been encoded using ImageIO.write method in this way:

          BufferedImage originalImage = ImageIO.read(new File("c:\\image.jpg"));
          // convert BufferedImage to byte array
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          ImageIO.write(originalImage, "jpg", baos);
          baos.flush();
          imageInByte = baos.toByteArray();
          baos.close();

Then the content of the ByteArrayOutputStream is simply dumped in a file image.out and this is the file I need to convert back to an Image but using C++. I see that the size of the file image.jpg is greater than the size of the image.out file so the Java must make some compression or encoding that I don't know. So I can't just read the image as JPG and put it in a QImage or QPixmap object.

Does anyone know how the ImageIO class encodes an image ?

Does anyone know how the ImageIO class encodes an image ?

The best choice is probably to read the Java Image I/O API Guide from Oracle.

It describes the basics and the plugin mechanism used for writing. You want to read at least section 3 (and especially 3.4 about the ImageWriter ), and maybe 4.1 for an introduction to the plugin concept.

For details on how to control JPEG format specific write settings, see JPEGImageWriteParam API doc.

PS: Your code looks ok though, and should provide a valid JPEG, provided the ImageIO reader plugin can read your initial file (the com.sun.imageio.plugins.JPEGImageReader is very restrictive in regards to non-standard files, even if most other software happily reads them).

PPS: The standard ImageIO JPEG writer plugin ( com.sun.imageio.plugins.JPEGImageWriter ) uses the Independent JPEG Group (IJG) JPEG library to write its JPEG stream. I don't think you can get much more "standard" than that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM