ater 发表于 2013-1-27 06:17:13

JAVA实现对图片的缩放

<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.awt.Graphics2D;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.awt.RenderingHints;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.awt.geom.AffineTransform;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.awt.image.BufferedImage;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.awt.image.ColorModel;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.awt.image.WritableRaster;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.io.File;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.io.FileNotFoundException;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport javax.imageio.ImageIO;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport org.apache.commons.logging.Log;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport org.apache.commons.logging.LogFactory;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://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.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublic 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.gifhttp://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.gifhttp://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.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            throws Exception, FileNotFoundException ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://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.gifhttp://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.gifhttp://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.gifhttp://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.gifhttp://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.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        if (hx < 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.gifhttp://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.gifhttp://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.gifhttp://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.gifhttp://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.gifhttp://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.gifhttp://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
页: [1]
查看完整版本: JAVA实现对图片的缩放