jpeg has poor quality when written back to disk

C

code_wrong

Hi,
I have written an app using netbeans that loads a jpeg image to a panel.
I can then do some processing on it. However if I do no
processing other than loading the file and saving it as a jpeg,
the file has very bad quality. (compressed further?)

How can I save an image file (jpeg) without losing quality?
 
C

code_wrong

Roedy Green said:
Jpeg is a "lossy" compressed format with varying degrees of
compression. I suggest you look for methods that let you find out how
much you had to start, and that let you control how much to use on
save. Then you can use the same.

See http://mindprod.com/jgloss/jpegencoder.html

Thanks . after a fair bit of googling I found this:
http://javaalmanac.com/egs/javax.imageio/JpegWrite.html
e699. Compressing a JPEG File
This example implements a method for writing a JPEG file with a specific
compression quality. In J2SE 1.4, the compression capability is not
functioning properly. This example demonstrates a workaround.

// Reads the jpeg image in infile, compresses the image,
// and writes it back out to outfile.
// compressionQuality ranges between 0 and 1,
// 0-lowest, 1-highest.
public void compressJpegFile(File infile, File outfile, float
compressionQuality) {
try {
// Retrieve jpg image to be compressed
RenderedImage rendImage = ImageIO.read(infile);

// Find a jpeg writer
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
if (iter.hasNext()) {
writer = (ImageWriter)iter.next();
}

// Prepare output file
ImageOutputStream ios = ImageIO.createImageOutputStream(outfile);
writer.setOutput(ios);

// Set the compression quality
ImageWriteParam iwparam = new MyImageWriteParam();
iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
iwparam.setCompressionQuality(compressionQuality);

// Write the image
writer.write(null, new IIOImage(rendImage, null, null), iwparam);

// Cleanup
ios.flush();
writer.dispose();
ios.close();
} catch (IOException e) {
}
}

// This class overrides the setCompressionQuality() method to workaround
// a problem in compressing JPEG images using the javax.imageio package.
public class MyImageWriteParam extends JPEGImageWriteParam {
public MyImageWriteParam() {
super(Locale.getDefault());
}

// This method accepts quality levels between 0 (lowest) and 1 (highest)
//and simply converts
// it to a range between 0 and 256; this is not a correct conversion
algorithm.
// However, a proper alternative is a lot more complicated.
// This should do until the bug is fixed.
public void setCompressionQuality(float quality) {
if (quality < 0.0F || quality > 1.0F) {
throw new IllegalArgumentException("Quality out-of-bounds!");
}
this.compressionQuality = 256 - (quality * 256);
}
}
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top