Preview

Computer Research

Satisfactory Essays
Open Document
Open Document
724 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Computer Research
COMPS412F Tutorial 8

Activities

Objective:
The objective of this activity is to make use of JPEGImageEncoder and JPEGImageDecoder from com.sun.image.codec.jpeg.* to perform JPEG compression and to study how the compressed image quality is related to the amount of compression.

Description:
[pic]
The image on the left is the original Lenna image. The scrollbar controls the quality factor of JPEGImageEncoder. The image on the right is the compressed image. The compressed image suffers a various degree of loss depending on the scrollbar position.
When the application is first invoked, it reads in the original lenna.bmp image and displays it on the left. When the Compress button is clicked, it encodes the image and outputs it as a JPEG file. This step is performed by the following code segment: try {
File file = new File("test.jpg"); FileOutputStream out = new FileOutputStream(file); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(im.image); param.setQuality((float)(jSlider1.getValue()/100.0), false); encoder.setJPEGEncodeParam(param); encoder.encode(im.image);
}
catch (Exception ex) { System.out.println(ex);
}
In the above code segment, im.image refers to the BufferedImage object used to hold the image.
After that, the newly created JPEG file is read in and displayed on the right using the following code segment: try { bm = new ImageData(256, 256); File file = new File("test.jpg"); FileInputStream in = new FileInputStream(file); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in); bm.image = decoder.decodeAsBufferedImage(); imagePanel2.showImage(bm);
}
catch (Exception ex) { System.out.println(ex);
}
By changing the quality factor, you can see how the compressed image quality is affected. You should also check the size of the JPEG file created.
Results:
The entire zip (jpegviewer.zip) file can be downloaded

You May Also Find These Documents Helpful