Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JPEG quality loss

Reply
Thread Tools

JPEG quality loss

 
 
k0m0r
Guest
Posts: n/a
 
      07-17-2006
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

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      07-17-2006
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.IIOM etadata,%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.

 
Reply With Quote
 
 
 
 
k0m0r
Guest
Posts: n/a
 
      07-17-2006
<http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageWriter.html#write(javax.imageio.metadata.IIOM etadata,%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?

 
Reply With Quote
 
pranshu
Guest
Posts: n/a
 
      07-17-2006
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

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      07-17-2006

k0m0r wrote:
> <http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageWriter.html#write(javax.imageio.metadata.IIOM etadata,%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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JPEG image quality loss when rendering in Swing Components Biswa Prakash Java 2 10-17-2006 06:18 PM
JPEG and quality loss... Sniper Digital Photography 2 05-12-2006 05:09 PM
how to stop JPEG loss drs@canby.com Digital Photography 53 11-25-2004 08:54 AM
Quality loss on saving John Lee Digital Photography 12 05-29-2004 09:57 PM
JPEG Questions: Loss In Quality When "Saving" Xtx99 Digital Photography 14 04-13-2004 03:09 PM



Advertisments