---
title: "JAVA实现对图片的缩放"
author: "ater"
date: "2007-03-08T15:52:00Z"
tags: []
slug: "java"
draft: false
lastmod: "2007-03-08T15:52:00Z"
id: 58250
url: "https://zsy.iteye.com/blog/58250"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.awt.Graphics2D;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.awt.RenderingHints;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.awt.geom.AffineTransform;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.awt.image.BufferedImage;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.awt.image.ColorModel;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.awt.image.WritableRaster;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.File;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.FileNotFoundException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.imageio.ImageIO;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.commons.logging.Log;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.commons.logging.LogFactory;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @author zsy  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) *  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class ZoomImage ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private static final Log log = LogFactory.getLog(ZoomImage.class);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public static String fileSeparator = System.getProperty("file.separator");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     *   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param filePath(E:\\cfimg\\userImg\\NBCANH5PIOWX\\9GZJO8QN1QWX.jpg)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param outPutDir(E:\\cfimg\\userImg\\NBCANH5PIOWX\\9GZJO8QN1QWX-60-60.jpg)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param height  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param width  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param replace 是否覆盖已有文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return 生成文件名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @throws Exception  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @throws FileNotFoundException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public static void zoomImage(String filePath, String outPutFile,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            int width, int height, boolean replace)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            throws Exception, FileNotFoundException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        File inPutFile = new File(filePath);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        File outPut = new File(outPutFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        zoomImage(inPutFile, outPut, width, height, replace);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public static void zoomImage(File inPutFile, File outPutFile,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            int width, int height, boolean replace)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            throws Exception, FileNotFoundException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if (!inPutFile.isFile()) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            log.error("文件不存在:" + inPutFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            throw new FileNotFoundException("文件不存在:" + inPutFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if (!outPutFile.exists() || replace) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            zoomImage(inPutFile, outPutFile, height, width);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * 按指定大小缩放图片  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param inPutFile  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param outPutFile  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param height  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param width  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @throws Exception  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public static void zoomImage(File inPutFile, File outPutFile,   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            int width, int height) throws Exception ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        BufferedImage source = ImageIO.read(inPutFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if (source == null) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            return;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        double hx = (double)height / source.getHeight();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        double wy = (double)width / source.getWidth();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if (hx &lt; wy) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            wy = hx;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            width = (int)(source.getWidth() * wy);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        } else ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            hx = wy;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            height = (int)(source.getHeight() * hx);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        int type = source.getType();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        BufferedImage target = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if (type == BufferedImage.TYPE\_CUSTOM) ...{ // handmade  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            ColorModel cm = source.getColorModel();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            WritableRaster raster =   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                cm.createCompatibleWritableRaster(width, height);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            boolean alphaPremultiplied = cm.isAlphaPremultiplied();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            target = new BufferedImage(cm, raster, alphaPremultiplied, null);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        } else ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            target = new BufferedImage(width, height, type);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Graphics2D g = target.createGraphics();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // smoother than exlax:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        g.setRenderingHint(RenderingHints.KEY\_INTERPOLATION,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                RenderingHints.VALUE\_INTERPOLATION\_BICUBIC);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        g.drawRenderedImage(source, AffineTransform.getScaleInstance(wy, hx));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        g.dispose();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        try ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            ImageIO.write(target, "JPEG", outPutFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        } catch (Exception ex) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            ex.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public static void main(String arg\[]) throws Exception ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        zoomImage("c:/V.gif", "c:/V\_50\_50.gif", 50, 50, false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)