JPEG quality loss

K

k0m0r

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
 
A

Andrew Thompson

k0m0r said:
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...geio.IIOImage, javax.imageio.ImageWriteParam)>
...combined with..
<http://java.sun.com/j2se/1.5.0/docs...eWriteParam.html#setCompressionQuality(float)>
...should do what you want.

Andrew T.
 
P

pranshu

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
 
A

Andrew Thompson

k0m0r said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top