(no commit message)
[utils] / gps / src / main / java / 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  * @author Erik Brakkee
38  */
39 public class JpegUtils {
40     /**
41      * Scales an image preserving the aspect ratio. 
42      * 
43      * @param aMaxWidth Maximum width. 
44      * @param aMaxHeight Maximum height. 
45      * @param aImage Image to scale. 
46      * @return Scaled image. 
47      */
48     public static BufferedImage scaleImage(int aMaxWidth, int aMaxHeight, Image aImage) {
49         double thumbRatio = (double) aMaxWidth / (double) aMaxHeight;
50         int imageWidth = aImage.getWidth(null);
51         int imageHeight = aImage.getHeight(null);
52         double imageRatio = (double) imageWidth / (double) imageHeight;
53         if (thumbRatio < imageRatio) {
54             aMaxHeight = (int) (aMaxWidth / imageRatio);
55         } else {
56             aMaxWidth = (int) (aMaxHeight * imageRatio);
57         }
58         BufferedImage thumbImage = new BufferedImage(aMaxWidth, aMaxHeight,
59                 BufferedImage.TYPE_INT_RGB);
60         Graphics2D graphics2D = thumbImage.createGraphics();
61         graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
62                 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
63         graphics2D.drawImage(aImage, 0, 0, aMaxWidth, aMaxHeight, null);
64         return thumbImage;
65     }
66
67     /**
68      * Loads a jpeg image from an input stream.
69      *  
70      * @param aInput Input stream. 
71      * @return JPEG image. 
72      * @throws IOException In case of IO problems.  
73      * @throws InterruptedException When execution is interrupted. 
74      */
75     public static BufferedImage loadJpegImage(InputStream aInput) throws IOException, InterruptedException {
76         JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(aInput);
77         BufferedImage image = decoder.decodeAsBufferedImage();
78         MediaTracker mediaTracker = new MediaTracker(new Container());
79         mediaTracker.addImage(image, 0);
80         mediaTracker.waitForID(0);
81         return image;
82     }
83
84     /**
85      * Writes a JPEG image. 
86      * 
87      * @param aOutput Output stream to write to. 
88      * @param aQuality Quality of the JPEG image in the range 0..100
89      * @param aThumbImage
90      * @throws IOException
91      */
92     public static void writeJpegImage(OutputStream aOutput, int aQuality, BufferedImage aThumbImage) throws IOException {
93         // save thumbnail image to OUTFILE
94         
95         if ( aQuality < 0 || aQuality > 100 ) {
96             throw new IllegalArgumentException("Argument quality must be in range 0.100: " + aQuality); 
97         }
98             
99         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(aOutput);
100         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(aThumbImage);
101         aQuality = Math.max(0, Math.min(aQuality, 100));
102         param.setQuality((float) aQuality / 100.0f, false);
103         encoder.setJPEGEncodeParam(param);
104         encoder.encode(aThumbImage);
105     }
106
107 }
108