This discussion is archived
0 Replies Latest reply: Mar 21, 2013 4:17 AM by Rafal Z RSS

Problem saving BufferedImage to JPEG2000 format

Rafal Z Newbie
Currently Being Moderated
Hallo,

I want to save a BufferedImage to JPEG2000 file, but I get corrupted file. I use following code:
 boolean myfunction(BufferedImage img,OutputStream out,float quality){            
        Iterator i = ImageIO.getImageWritersBySuffix(suffix);
        if (i.hasNext()) {
            writer = (ImageWriter) i.next();
        }
        if (writer == null) {
            return false;
        }
        try {
                ImageOutputStream ios = ImageIO.createImageOutputStream(out);
 
                pars=new J2KImageWriteParam(Locale.getDefault());
                String[] list=pars.getCompressionTypes();
                pars.setTilingMode(J2KImageWriteParam.MODE_DISABLED);
                pars.setCompressionMode(J2KImageWriteParam.MODE_EXPLICIT);
                pars.setCompressionType(list[0]);//"JPEG2000"
                pars.setCompressionQuality(quality);
                ((J2KImageWriteParam)(pars)).setWriteCodeStreamOnly(false);
                BufferedImage gif=new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
                gif.getGraphics().drawImage(img, 0, 0, null);
                img=gif;
                writer.setOutput(ios);
                writer.write(null, new IIOImage(img, null, null), pars);
                ios.flush();
                writer.dispose();
                ios.close();
        } catch (IOException ex) {
            Logger.getLogger(RzJUtils.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
I try to save a 16 bit monochrome image so I convert it to TYPE_BYTE_INDEXED, and its functionning for gif images, but the resulting file for jpeg2000 is big ca. 3 MB. Without the conversion I get a file of 800 kB. I never used this format and it is difficult to diagnose the problem.

with regards

Rafal Ziolkowski

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points