2 * Copyright 2006 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.
17 package org.wamblee.utils;
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;
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;
35 * Utility functions for processing JPEG images.
37 * @author Erik Brakkee
39 public class JpegUtils {
41 * Scales an image preserving the aspect ratio.
43 * @param aMaxWidth Maximum width.
44 * @param aMaxHeight Maximum height.
45 * @param aImage Image to scale.
46 * @return Scaled image.
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);
56 aMaxWidth = (int) (aMaxHeight * imageRatio);
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);
68 * Loads a jpeg image from an input stream.
70 * @param aInput Input stream.
72 * @throws IOException In case of IO problems.
73 * @throws InterruptedException When execution is interrupted.
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);
85 * Writes a JPEG image.
87 * @param aOutput Output stream to write to.
88 * @param aQuality Quality of the JPEG image in the range 0..100
92 public static void writeJpegImage(OutputStream aOutput, int aQuality, BufferedImage aThumbImage) throws IOException {
93 // save thumbnail image to OUTFILE
95 if ( aQuality < 0 || aQuality > 100 ) {
96 throw new IllegalArgumentException("Argument quality must be in range 0.100: " + aQuality);
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);