(no commit message)
[utils] / gps / src / org / wamblee / utils / JpegUtils.java
1 /*
2  * Copyright 2006 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16
17 package org.wamblee.utils;
18
19 import java.awt.Container;
20 import java.awt.Graphics2D;
21 import java.awt.Image;
22 import java.awt.MediaTracker;
23 import java.awt.RenderingHints;
24 import java.awt.image.BufferedImage;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28
29 import com.sun.image.codec.jpeg.JPEGCodec;
30 import com.sun.image.codec.jpeg.JPEGEncodeParam;
31 import com.sun.image.codec.jpeg.JPEGImageDecoder;
32 import com.sun.image.codec.jpeg.JPEGImageEncoder;
33
34 /**
35  * Utility functions for processing JPEG images. 
36  */
37 public class JpegUtils {
38     /**
39      * Scales an image preserving the aspect ratio. 
40      * 
41      * @param aMaxWidth Maximum width. 
42      * @param aMaxHeight Maximum height. 
43      * @param aImage Image to scale. 
44      * @return Scaled image. 
45      */
46     public static BufferedImage scaleImage(int aMaxWidth, int aMaxHeight, Image aImage) {
47         double thumbRatio = (double) aMaxWidth / (double) aMaxHeight;
48         int imageWidth = aImage.getWidth(null);
49         int imageHeight = aImage.getHeight(null);
50         double imageRatio = (double) imageWidth / (double) imageHeight;
51         if (thumbRatio < imageRatio) {
52             aMaxHeight = (int) (aMaxWidth / imageRatio);
53         } else {
54             aMaxWidth = (int) (aMaxHeight * imageRatio);
55         }
56         BufferedImage thumbImage = new BufferedImage(aMaxWidth, aMaxHeight,
57                 BufferedImage.TYPE_INT_RGB);
58         Graphics2D graphics2D = thumbImage.createGraphics();
59         graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
60                 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
61         graphics2D.drawImage(aImage, 0, 0, aMaxWidth, aMaxHeight, null);
62         return thumbImage;
63     }
64
65     /**
66      * Loads a jpeg image from an input stream.
67      *  
68      * @param aInput Input stream. 
69      * @return JPEG image. 
70      * @throws IOException In case of IO problems.  
71      * @throws InterruptedException When execution is interrupted. 
72      */
73     public static BufferedImage loadJpegImage(InputStream aInput) throws IOException, InterruptedException {
74         JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(aInput);
75         BufferedImage image = decoder.decodeAsBufferedImage();
76         MediaTracker mediaTracker = new MediaTracker(new Container());
77         mediaTracker.addImage(image, 0);
78         mediaTracker.waitForID(0);
79         return image;
80     }
81
82     /**
83      * Writes a JPEG image. 
84      * 
85      * @param aOutput Output stream to write to. 
86      * @param aQuality Quality of the JPEG image in the range 0..100
87      * @param aThumbImage
88      * @throws IOException
89      */
90     public static void writeJpegImage(OutputStream aOutput, int aQuality, BufferedImage aThumbImage) throws IOException {
91         // save thumbnail image to OUTFILE
92         
93         if ( aQuality < 0 || aQuality > 100 ) {
94             throw new IllegalArgumentException("Argument quality must be in range 0.100: " + aQuality); 
95         }
96             
97         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(aOutput);
98         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(aThumbImage);
99         aQuality = Math.max(0, Math.min(aQuality, 100));
100         param.setQuality((float) aQuality / 100.0f, false);
101         encoder.setJPEGEncodeParam(param);
102         encoder.encode(aThumbImage);
103     }
104
105 }
106