2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.photos.utils;
18 import java.awt.Container;
19 import java.awt.Graphics2D;
20 import java.awt.Image;
21 import java.awt.MediaTracker;
22 import java.awt.RenderingHints;
23 import java.awt.image.BufferedImage;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
28 import sun.awt.image.codec.JPEGImageDecoderImpl;
30 import com.sun.image.codec.jpeg.JPEGCodec;
31 import com.sun.image.codec.jpeg.JPEGEncodeParam;
32 import com.sun.image.codec.jpeg.JPEGImageDecoder;
33 import com.sun.image.codec.jpeg.JPEGImageEncoder;
36 * Utility functions for processing JPEG images.
38 public class JpegUtils {
40 * Scales an image preserving the aspect ratio.
42 * @param aMaxWidth Maximum width.
43 * @param aMaxHeight Maximum height.
44 * @param aImage Image to scale.
45 * @return Scaled image.
47 public static BufferedImage scaleImage(int aMaxWidth, int aMaxHeight, Image aImage) {
48 double thumbRatio = (double) aMaxWidth / (double) aMaxHeight;
49 int imageWidth = aImage.getWidth(null);
50 int imageHeight = aImage.getHeight(null);
51 double imageRatio = (double) imageWidth / (double) imageHeight;
52 if (thumbRatio < imageRatio) {
53 aMaxHeight = (int) (aMaxWidth / imageRatio);
55 aMaxWidth = (int) (aMaxHeight * imageRatio);
57 BufferedImage thumbImage = new BufferedImage(aMaxWidth, aMaxHeight,
58 BufferedImage.TYPE_INT_RGB);
59 Graphics2D graphics2D = thumbImage.createGraphics();
60 graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
61 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
62 graphics2D.drawImage(aImage, 0, 0, aMaxWidth, aMaxHeight, null);
67 * Loads a jpeg image from an input stream.
69 * @param aInput Input stream.
71 * @throws IOException In case of IO problems.
72 * @throws InterruptedException When execution is interrupted.
74 public static BufferedImage loadJpegImage(InputStream aInput) throws IOException, InterruptedException {
75 JPEGImageDecoder decoder = new JPEGImageDecoderImpl(aInput);
76 BufferedImage image = decoder.decodeAsBufferedImage();
77 MediaTracker mediaTracker = new MediaTracker(new Container());
78 mediaTracker.addImage(image, 0);
79 mediaTracker.waitForID(0);
84 * Writes a JPEG image.
86 * @param aOutput Output stream to write to.
87 * @param aQuality Quality of the JPEG image in the range 0..100
91 public static void writeJpegImage(OutputStream aOutput, int aQuality, BufferedImage aThumbImage) throws IOException {
92 // save thumbnail image to OUTFILE
94 if ( aQuality < 0 || aQuality > 100 ) {
95 throw new IllegalArgumentException("Argument quality must be in range 0.100: " + aQuality);
98 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(aOutput);
99 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(aThumbImage);
100 aQuality = Math.max(0, Math.min(aQuality, 100));
101 param.setQuality((float) aQuality / 100.0f, false);
102 encoder.setJPEGEncodeParam(param);
103 encoder.encode(aThumbImage);