经常在网上找处理图片的方法,总是感觉很乱,以下代码的方法不错,总结分享下:
/** * @param fromFileStr 源图片路径 * @param saveToFileStr 目标图片目录 * @param sysimgfile 目标图片名(不带图片后缀) * @param suffix 目标图片后缀 * @param width 目标图片宽 * @param height 目标图片高 * @throws Exception * @author 李敏 */ public static void createThumbnail(String fromFileStr, String saveToFileStr, String sysimgfile, String suffix, int width, int height) throws Exception { double Ratio = 0.0; File F = new File(fromFileStr); if (!F.isFile()) throw new Exception(F + " is not image file error in CreateThumbnail!"); File ThF = new File(saveToFileStr, sysimgfile + "." + suffix); BufferedImage Bi = ImageIO.read(F); Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH); if ((Bi.getHeight() > width) || (Bi.getWidth() > height)) { if (Bi.getHeight() > Bi.getWidth()) Ratio = (double) width / Bi.getHeight(); else Ratio = (double) height / Bi.getWidth(); } AffineTransformOp op = new AffineTransformOp(AffineTransform .getScaleInstance(Ratio, Ratio), null); Itemp = op.filter(Bi, null); try { ImageIO.write((BufferedImage) Itemp, suffix, ThF); } catch (Exception ex) { throw new Exception(" ImageIo.write error in CreatThum.: " + ex.getMessage()); } }
原文:http://www.cnblogs.com/jdxf/p/image.html