Java codepublic static void scale(String srcFile, int destWidth, int destHeight, String destFile) throws IOException {
BufferedImage src = ImageIO.read(new File(srcFile));
BufferedImage dest = new BufferedImage(destWidth,destHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = dest.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance(
(double)destWidth/src.getWidth(),
(double)destHeight/src.getHeight());
g.drawRenderedImage(src,at);
ImageIO.write(dest,"JPG",new File(destFile));
}
public static File scale(File srcFile, int destWidth) throws IOException {
BufferedImage src = ImageIO.read(srcFile);
if (src.getWidth() <= destWidth) {
return srcFile;
}
int destHeight = (int)(src.getHeight() * ((double)destWidth/src.getWidth()));
BufferedImage dest = new BufferedImage(destWidth,destHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = dest.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance(
(double)destWidth/src.getWidth(),
(double)destHeight/src.getHeight());
g.drawRenderedImage(src,at);
String fileSufix = srcFile.getName().substring(srcFile.getName().lastIndexOf(".") + 1);
File destFile = new File(srcFile.getParent(), UUID.randomUUID().toString() + "." + fileSufix);
ImageIO.write(dest,fileSufix,destFile);
return destFile;
}
public static void main(String[] args) {
// if (args.length == 2) {
// try {
// scale(new File(args[0]),Integer.parseInt(args[1]));
// } catch (Exception e) {
// System.out.println(e);
// }
// } else
// System.out.println("paramater error!");
try {
scale(new File("e:/temp/1.gif"), 100);
scale(new File("e:/temp/2.png"), 100);
scale(new File("e:/temp/3.jpg"), 100);
scale(new File("e:/temp/4.jpeg"), 100);
} catch (Exception ex) {
System.out.println(ex);
}
}