JPEG quality loss

Discussion in 'Java' started by k0m0r, Jul 17, 2006.

  1. k0m0r

    k0m0r Guest

    Hi.
    I've been trying to solve it myself, but I give up.
    I need to convert BufferedImage to byte[]. I'm using
    com.sun.imageio.plugins.jpeg.JPEGImageWriter and everything works
    perfect, expect the writer keeps compressing all images, and the
    quality of output is really poor :(
    My code is:


    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ImageOutputStream imgOut = ImageIO.createImageOutputStream(out);

    Iterator writerIter = ImageIO.getImageWritersByFormatName("jpg");
    ImageWriter writer = (ImageWriter) writerIter.next();
    // now the writer is com.sun.imageio.plugins.jpeg.JPEGImageWriter

    writer.setOutput(imgOut);
    writer.prepareWriteSequence(null);

    for(...) {
    BufferedImage image = (...)
    IIOImage iioImage = new IIOImage(image, null, null);

    writer.write(iioImage);
    out.reset();
    (...) // saving the byte array somewhere
    }

    writer.endWriteSequence();
    imgOut.flush();
    writer.dispose();
    imgOut.close();


    Please help - how can I set the JPEG compression value to the highest
    quality?
    Or maybe 95-97% of the original quality?

    Thanx a lot in advance,

    k0m0r
     
    k0m0r, Jul 17, 2006
    #1
    1. Advertising

  2. k0m0r wrote:
    > Hi.
    > I've been trying to solve it myself, but I give up.


    Have you given up prior to consulting the documentation?

    > I need to convert BufferedImage to byte[]. I'm using
    > com.sun.imageio.plugins.jpeg.JPEGImageWriter and everything works
    > perfect, expect the writer keeps compressing all images, and the
    > quality of output is really poor :(

    ....
    > writer.write(iioImage);


    A quick perusal of that documentation suggests..
    <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageWriter.html#write(javax.imageio.metadata.IIOMetadata,%20javax.imageio.IIOImage,%20javax.imageio.ImageWriteParam)>
    ...combined with..
    <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageWriteParam.html#setCompressionQuality(float)>
    ...should do what you want.

    Andrew T.
     
    Andrew Thompson, Jul 17, 2006
    #2
    1. Advertising

  3. k0m0r

    k0m0r Guest

    <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageWriter.html#write(javax.imageio.metadata.IIOMetadata,%20javax.imageio.IIOImage,%20javax.imageio.ImageWriteParam)>
    > ..combined with..
    > <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageWriteParam.html#setCompressionQuality(float)>
    > ..should do what you want.


    java.lang.UnsupportedOperationException: Compression not supported

    How come com.sun.imageio.plugins.jpeg.JPEGImageWriter does not support
    compression, while I can CLEARLY see that the quality is being lost?!
    Have you encountered this problem?
     
    k0m0r, Jul 17, 2006
    #3
  4. k0m0r

    pranshu Guest

    I had a slightly similar problem - which was loss of quality while
    resizing the image.
    In the end I was forced to use ImageMagick to do the resizing
    http://www.imagemagick.org/script/index.php
    http://www.yeo.id.au/jmagick/

    Maybe you can experiment with writing out a BMP - see if the quality of
    that is acceptable and then use a specialist imaging tool like
    imagemagick to do the compression / conversion.

    BTW are you writing a drawing application? Otherwise you should have
    the source image - and it might be easier to manipulate the source
    image file directly.

    Regards
    Pranshu
     
    pranshu, Jul 17, 2006
    #4
  5. k0m0r wrote:
    > <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageWriter.html#write(javax.imageio.metadata.IIOMetadata,%20javax.imageio.IIOImage,%20javax.imageio.ImageWriteParam)>
    > > ..combined with..
    > > <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageWriteParam.html#setCompressionQuality(float)>
    > > ..should do what you want.

    >
    > java.lang.UnsupportedOperationException: Compression not supported


    Which brings me back to the question, "did you RTFM?".

    <sscce>
    import javax.imageio.ImageWriteParam;
    import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
    import java.util.Locale;

    public class WriteImage {
    public static void main(String[] args) {
    Locale locale = new Locale("en");
    ImageWriteParam iwp1 = new ImageWriteParam(locale);
    System.out.println(iwp1.canWriteCompressed());
    JPEGImageWriteParam iwp2 = new JPEGImageWriteParam(locale);
    System.out.println(iwp2.canWriteCompressed());
    }
    }
    </sscce>

    Andrew T.
     
    Andrew Thompson, Jul 17, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MCP
    Replies:
    1
    Views:
    2,546
    Marco Schmidt
    Jul 3, 2003
  2. code_wrong
    Replies:
    2
    Views:
    590
    code_wrong
    Apr 21, 2004
  3. Karsten Wutzke

    JPEG compression and quality level

    Karsten Wutzke, May 31, 2005, in forum: Java
    Replies:
    2
    Views:
    5,255
  4. Biswa Prakash
    Replies:
    2
    Views:
    574
    Biswa Prakash
    Oct 17, 2006
  5. OtisUsenet
    Replies:
    1
    Views:
    929
    Daniel Pitts
    Sep 3, 2007
Loading...

Share This Page