001 // Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 002 // The Molly framework is freely distributable under the terms of an 003 // MIT-style license. For details, see the molly pages web site at: 004 // http://www.mollypages.org/. Use, modify, have fun ! 005 006 package fc.util; 007 008 import java.io.*; 009 import java.util.*; 010 import fc.io.*; 011 import fc.util.*; 012 013 import java.awt.*; 014 import java.awt.geom.*; 015 import java.awt.image.*; 016 import javax.imageio.*; 017 018 /* Misc. image related utils */ 019 public final class ImageUtil 020 { 021 022 /** 023 Resizes the specified image file to the new width and height and writes it to 024 the targetFile (creating the targetFile if it does not already exist). 025 <p> 026 The format string is the output format of the file, like "jpg", "gif" etc. This 027 can be different than the format of the source file. The understood formats 028 are those understood {@link javax.io.ImageWriter} (the usual suspects like gif, 029 jpg, png, etc, all seem to work). 030 */ 031 public static void resize(File sourceFile, File targetFile, String format, int newWidth, int newHeight) 032 throws IOException 033 { 034 BufferedImage resized = resize(sourceFile, newWidth, newHeight); 035 ImageIO.write(resized, format, targetFile); 036 } 037 038 039 /** 040 Resizes the specified image file to the new width and height and writes it to 041 the targetFile (creating the targetFile if it does not already exist). 042 <p> 043 The output format is the same as the input format and is intuited from the file 044 name extension of the specified source file (therefore, source files specified 045 to this method, should be like <code>foo.gif</code>, <code>foo.jpg</code>, etc). 046 */ 047 public static void resize(File sourceFile, File targetFile, int newWidth, int newHeight) 048 throws IOException 049 { 050 String name = sourceFile.getName(); 051 int pos = name.lastIndexOf("."); 052 if (pos == -1) { 053 throw new IOException("The specified source file: [" + sourceFile.getAbsolutePath() + "] has no extension, and this method needs an extension (like .gif, .jpg etc)"); 054 } 055 String extension = name.substring(pos+1, name.length()); 056 //System.out.println(extension); 057 resize(sourceFile, targetFile, extension, newWidth, newHeight); 058 } 059 060 /** 061 Resizes the specified image file to the new width and height and returns the 062 new image. 063 */ 064 public static BufferedImage resize(File sourceFile, int newWidth, int newHeight) 065 throws IOException 066 { 067 BufferedImage sourceImage = ImageIO.read(sourceFile); 068 //WritableRaster raster = image.getRaster(); //for pixel manipulation, not needed 069 070 int srcWidth = sourceImage.getWidth(); 071 int srcHeight = sourceImage.getHeight(); 072 073 BufferedImage targetImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); 074 Graphics2D g2 = targetImage.createGraphics(); 075 AffineTransform transform = AffineTransform.getScaleInstance((double)newWidth/srcWidth, (double)newHeight/srcHeight); 076 077 g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 078 RenderingHints.VALUE_INTERPOLATION_BICUBIC); 079 080 g2.drawImage(sourceImage, transform, null); 081 g2.dispose(); 082 083 return targetImage; 084 } 085 086 public static void main (String args[]) throws IOException 087 { 088 Args myargs = new Args(args); 089 myargs.setUsage(myargs.getMainClassName() + " -source path-to-source-image -target path-to-write-target-img -width new-width -height new-height"); 090 091 File source = new File(myargs.getRequired("source")); 092 File target = new File(myargs.getRequired("target")); 093 int width = myargs.getRequiredInt("width"); 094 int height = myargs.getRequiredInt("height"); 095 096 if (! source.exists()) { 097 System.out.println("Cannot read source file: " + source.getAbsolutePath()); 098 System.exit(1); 099 } 100 101 resize(source, target, width, height); 102 } 103 }