001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package com.nabhinc.util;
023:
024: import java.awt.Graphics2D;
025: import java.awt.Image;
026: import java.awt.geom.AffineTransform;
027: import java.awt.image.BufferedImage;
028: import java.io.FileOutputStream;
029: import java.io.IOException;
030: import java.io.OutputStream;
031:
032: import javax.swing.ImageIcon;
033:
034: import com.sun.image.codec.jpeg.JPEGCodec;
035: import com.sun.image.codec.jpeg.JPEGImageEncoder;
036:
037: /**
038: *
039: * @author Padmanabh Dabke
040: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
041: */
042: public class ImageUtil {
043:
044: /**
045: * Create a jpeg thumbnail image with the specified maximum size. If the image size is larger
046: * than the maximum allowed, the size (width and height) will be scaled to the actual
047: * ratio (width/height). The output image file name must have .jpg or jpeg extension.
048: *
049: * @param inputStream The original image input stream.
050: * @param imageFileName The output image file. The name must be ended with .jpg or jpeg
051: * @param maxWidth Image's maximum width
052: * @param maxHeight Image's maximum height
053: * @throws IOException Thrown if the output filename's extension is not jpg.
054: */
055: public static void createJPEGThumbnail(byte[] imgByte,
056: String imageFileName, int maxWidth, int maxHeight)
057: throws IOException {
058:
059: if (!imageFileName.toLowerCase().endsWith(".jpg")
060: && !imageFileName.toLowerCase().endsWith(".jpeg"))
061: throw new IOException(
062: "Unsupported image type. Only support image with extension '.jpg' or '.jpeg'.");
063:
064: OutputStream outputStream = null;
065: try {
066: ImageIcon imgIcon = new ImageIcon(imgByte);
067: Image srcImg = imgIcon.getImage();
068: int imgWidth = srcImg.getWidth(null);
069: int imgHeight = srcImg.getHeight(null);
070: if ((imgWidth <= 0) || (imgHeight <= 0)) {
071: throw new IOException("Invalid image size.");
072: }
073:
074: AffineTransform aTransform = new AffineTransform();
075: if ((imgWidth > maxWidth) || (imgHeight > maxHeight)) {
076: double scaleX = (double) maxWidth / imgWidth;
077: double scaleY = (double) maxHeight / imgHeight;
078: double scaleRatio = (scaleX < scaleY) ? scaleX : scaleY;
079: imgWidth = (int) (imgWidth * scaleRatio);
080: imgHeight = (int) (imgHeight * scaleRatio);
081: aTransform.scale(scaleRatio, scaleRatio);
082: }
083:
084: BufferedImage bufferedImg = new BufferedImage(imgWidth,
085: imgHeight, BufferedImage.TYPE_INT_RGB);
086:
087: Graphics2D g = bufferedImg.createGraphics();
088: g.drawImage(srcImg, aTransform, null);
089: g.dispose();
090:
091: outputStream = new FileOutputStream(imageFileName);
092: JPEGImageEncoder encoder = JPEGCodec
093: .createJPEGEncoder(outputStream);
094: encoder.encode(bufferedImg);
095:
096: } catch (IOException ie) {
097: throw ie;
098:
099: } finally {
100: if (outputStream != null)
101: outputStream.close();
102: }
103: }
104:
105: }
|