Computer Research

Only available on StudyMode
  • Topic: JPEG, Discrete cosine transform, Compression ratio
  • Pages : 4 (724 words )
  • Download(s) : 74
  • Published : December 6, 2012
Open Document
Text Preview
COMPS412F Tutorial 8


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.

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);

catch (Exception 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();
   catch (Exception 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 ( file can be downloaded from...
tracking img