Return-Path: Delivered-To: apmail-xml-cocoon-dev-archive@xml.apache.org Received: (qmail 24867 invoked by uid 500); 9 Jul 2002 16:13:22 -0000 Mailing-List: contact cocoon-dev-help@xml.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: list-post: Reply-To: cocoon-dev@xml.apache.org Delivered-To: mailing list cocoon-dev@xml.apache.org Received: (qmail 24856 invoked from network); 9 Jul 2002 16:13:21 -0000 From: "Per Kreipke" To: Subject: RE: ImageReader Date: Tue, 9 Jul 2002 12:16:11 -0400 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0) Importance: Normal In-Reply-To: <5.1.0.14.0.20020708184154.03cd3940@mail.visitronics.be> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300 X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N Not sure if this helps but here's another conversion program in Java which: - uses AWT but not the toolkit part - supports GIFs - is relatively smart about keep the x/y ratio - would need a little conversion to work in memory without writing to/from disk Per Kreipke P.s. Can't remember where I found portions of this code snippet so I can't give credit where credit is due, my apologies to the original author. package org.onclave.util; import java.awt.Image; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.io.FileOutputStream; import javax.swing.ImageIcon; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class Thumbnail { public static void main(String[] args) { System.out.print("starting...\n"); createThumbnail(args[0], args[1], Integer.parseInt(args[2])); System.out.print("finished.\n"); System.exit(0); } /** * Reads an image in a file and creates * a thumbnail in another file. * @param orig The name of image file. * @param thumb The name of thumbnail file. * Will be created if necessary. * @param maxDim The width and height of * the thumbnail must * be maxDim pixels or less. */ public static void createThumbnail(String orig, String thumb, int maxDim) { try { // Get the image from a file. Image inImage = new ImageIcon(orig).getImage(); // Determine the scale. double scale = (double)maxDim / (double) inImage.getHeight(null); if (inImage.getWidth(null) > inImage.getHeight(null)) { scale = (double)maxDim / (double) inImage.getWidth(null); } // Determine size of new image. // One of them should equal maxDim. int scaledW = (int)(scale * inImage.getWidth(null)); int scaledH = (int)(scale * inImage.getHeight(null)); // Create an image buffer in which to paint on. BufferedImage outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB); // Set the scale. AffineTransform tx = new AffineTransform(); // If the image is smaller than the desired image size, // don't bother scaling. if (scale < 1.0d) { tx.scale(scale, scale); } // Paint image. Graphics2D g2d = outImage.createGraphics(); g2d.drawImage(inImage, tx, null); g2d.dispose(); // JPEG-encode the image and write to file. OutputStream os = new FileOutputStream(thumb); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); encoder.encode(outImage); os.close(); } catch (IOException e) { e.printStackTrace(); } } } --------------------------------------------------------------------- To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org For additional commands, email: cocoon-dev-help@xml.apache.org