<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-57  評論-117  文章-1  trackbacks-0

    支持將Image的寬度、高度縮放到指定width、height,并保存在指定目錄
    通過目標對象的大小和標準(指定)大小計算出圖片縮小的比例
    可以設置圖片縮放質量,并且可以根據指定的寬高縮放圖片

    源碼:

    package com.hoo.util;
     
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import com.sun.image.codec.jpeg.ImageFormatException;
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGEncodeParam;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
     
    /**
     * <b>function:</b> 縮放圖片工具類,創建縮略圖、伸縮圖片比例
     * @author hoojo
     * @createDate 2012-2-3 上午10:08:47
     * @file ScaleImageUtils.java
     * @package com.hoo.util
     * @blog http://blog.csdn.net/IBM_hoojo
     * http://hoojo.cnblogs.com
     * @email hoojo_@126.com
     * @version 1.0
     */
    public abstract class ScaleImageUtils {
     
        private static final float DEFAULT_SCALE_QUALITY = 1f;
        private static final String DEFAULT_IMAGE_FORMAT = ".jpg"; // 圖像文件的格式 
        private static final String DEFAULT_FILE_PATH = "C:/temp-";
        
        /**
         * <b>function:</b> 設置圖片壓縮質量枚舉類;
         * Some guidelines: 0.75 high quality、0.5  medium quality、0.25 low quality
         * @author hoojo
         * @createDate 2012-2-7 上午11:31:45
         * @file ScaleImageUtils.java
         * @package com.hoo.util
         * @project JQueryMobile
         * @blog http://blog.csdn.net/IBM_hoojo
         * @email hoojo_@126.com
         * @version 1.0
         */
        public enum ImageQuality {
            max(1.0f), high(0.75f), medium(0.5f), low(0.25f);
            
            private Float quality;
            public Float getQuality() {
                return this.quality;
            }
            ImageQuality(Float quality) {
                this.quality = quality;
            }
        }
        
        private static Image image;
        
        /**
         * <b>function:</b> 通過目標對象的大小和標準(指定)大小計算出圖片縮小的比例
         * @author hoojo
         * @createDate 2012-2-6 下午04:41:48
         * @param targetWidth 目標的寬度
         * @param targetHeight 目標的高度
         * @param standardWidth 標準(指定)寬度
         * @param standardHeight 標準(指定)高度
         * @return 最小的合適比例
         */
        public static double getScaling(double targetWidth, double targetHeight, double standardWidth, double standardHeight) {
            double widthScaling = 0d;
            double heightScaling = 0d;
            if (targetWidth > standardWidth) {
                widthScaling = standardWidth / (targetWidth * 1.00d);
            } else {
                widthScaling = 1d;
            }
            if (targetHeight > standardHeight) {
                heightScaling = standardHeight / (targetHeight * 1.00d);
            } else {
                heightScaling = 1d;
            }
            return Math.min(widthScaling, heightScaling);
        }
        
        /**
         * <b>function:</b> 將Image的寬度、高度縮放到指定width、height,并保存在savePath目錄
         * @author hoojo
         * @createDate 2012-2-6 下午04:54:35
         * @param width 縮放的寬度
         * @param height 縮放的高度
         * @param savePath 保存目錄
         * @param targetImage 即將縮放的目標圖片
         * @return 圖片保存路徑、名稱
         * @throws ImageFormatException
         * @throws IOException
         */
        public static String resize(int width, int height, String savePath, Image targetImage) throws ImageFormatException, IOException {
            width = Math.max(width, 1);
            height = Math.max(height, 1);
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            image.getGraphics().drawImage(targetImage, 0, 0, width, height, null);
            
            if (savePath == null || "".equals(savePath)) {
                savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT;
            }
            
            FileOutputStream fos = new FileOutputStream(new File(savePath));
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
            encoder.encode(image);
     
            image.flush();
            fos.flush();
            fos.close();
            
            return savePath;
        }
        
        /**
         * <b>function:</b> 可以設置圖片縮放質量,并且可以根據指定的寬高縮放圖片
         * @author hoojo
         * @createDate 2012-2-7 上午11:01:27
         * @param width 縮放的寬度
         * @param height 縮放的高度
         * @param quality 圖片壓縮質量,最大值是1; 使用枚舉值:{@link ImageQuality}
         *             Some guidelines: 0.75 high quality、0.5  medium quality、0.25 low quality
         * @param savePath 保存目錄
         * @param targetImage 即將縮放的目標圖片
         * @return 圖片保存路徑、名稱
         * @throws ImageFormatException
         * @throws IOException
         */
        public static String resize(int width, int height, Float quality, String savePath, Image targetImage) throws ImageFormatException, IOException {
            width = Math.max(width, 1);
            height = Math.max(height, 1);
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            image.getGraphics().drawImage(targetImage, 0, 0, width, height, null);
            
            if (savePath == null || "".equals(savePath)) {
                savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT;
            }
            
            FileOutputStream fos = new FileOutputStream(new File(savePath));
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
            
            JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(image); 
            if (quality == null || quality <= 0) {
                quality = DEFAULT_SCALE_QUALITY;
            }
            /** 設置圖片壓縮質量 */  
            encodeParam.setQuality(quality, true);  
            encoder.encode(image, encodeParam);  
     
            image.flush();
            fos.flush();
            fos.close();
            
            return savePath;
        }
        
        /**
         * <b>function:</b> 通過指定大小和圖片的大小,計算出圖片縮小的合適大小
         * @author hoojo
         * @createDate 2012-2-6 下午05:53:10
         * @param width 指定的寬度
         * @param height 指定的高度
         * @param image 圖片文件
         * @return 返回寬度、高度的int數組
         */
        public static int[] getSize(int width, int height, Image image) {
            int targetWidth = image.getWidth(null);
            int targetHeight = image.getHeight(null);
            double scaling = getScaling(targetWidth, targetHeight, width, height);
            long standardWidth = Math.round(targetWidth * scaling);
            long standardHeight = Math.round(targetHeight * scaling);
            return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) };
        }
        
        /**
         * <b>function:</b> 通過指定的比例和圖片對象,返回一個放大或縮小的寬度、高度
         * @author hoojo
         * @createDate 2012-2-7 上午10:27:59
         * @param scale 縮放比例
         * @param image 圖片對象
         * @return 返回寬度、高度
         */
        public static int[] getSize(float scale, Image image) {
            int targetWidth = image.getWidth(null);
            int targetHeight = image.getHeight(null);
            long standardWidth = Math.round(targetWidth * scale);
            long standardHeight = Math.round(targetHeight * scale);
            return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) };
        }
        
        public static int[] getSize(int width, Image image) {
            int targetWidth = image.getWidth(null);
            int targetHeight = image.getHeight(null);
            long height = Math.round((targetHeight * width) / (targetWidth * 1.00f));
            return new int[] { width, Integer.parseInt(String.valueOf(height)) };
        }
        
        public static int[] getSizeByHeight(int height, Image image) {
            int targetWidth = image.getWidth(null);
            int targetHeight = image.getHeight(null);
            long width = Math.round((targetWidth * height) / (targetHeight * 1.00f));
            return new int[] { Integer.parseInt(String.valueOf(width)), height };
        }
        
        /**
         * 
         * <b>function:</b> 將指定的targetFile圖片文件的寬度、高度大于指定width、height的圖片縮小,并保存在savePath目錄
         * @author hoojo
         * @createDate 2012-2-6 下午04:57:02
         * @param width 縮小的寬度
         * @param height 縮小的高度
         * @param savePath 保存目錄
         * @param targetImage 改變的目標圖片
         * @return 圖片保存路徑、名稱
         * @throws ImageFormatException
         * @throws IOException
         */
        public static String resize(int width, int height, String savePath, File targetFile) throws ImageFormatException, IOException {
            image = ImageIO.read(targetFile);
            int[] size = getSize(width, height, image);
            return resize(size[0], size[1], savePath, image);
        }
        
        /**
         * 
         * <b>function:</b> 將指定的targetURL網絡圖片文件的寬度、高度大于指定width、height的圖片縮小,并保存在savePath目錄
         * @author hoojo
         * @createDate 2012-2-6 下午04:57:07
         * @param width 縮小的寬度
         * @param height 縮小的高度
         * @param savePath 保存目錄
         * @param targetImage 改變的目標圖片
         * @return 圖片保存路徑、名稱
         * @throws ImageFormatException
         * @throws IOException
         */
        public static String resize(int width, int height, String savePath, URL targetURL) throws ImageFormatException, IOException {
            image = ImageIO.read(targetURL);
            int[] size = getSize(width, height, image);
            return resize(size[0], size[1], savePath, image);
        }
        
        /**
         * <b>function:</b> 將一個本地的圖片文件按照指定的比例進行縮放
         * @author hoojo
         * @createDate 2012-2-7 上午10:29:18
         * @param scale 縮放比例
         * @param savePath 保存文件路徑、名稱
         * @param targetFile 本地圖片文件
         * @return 新的文件名稱
         * @throws ImageFormatException
         * @throws IOException
         */
        public static String resize(float scale, String savePath, File targetFile) throws ImageFormatException, IOException {
            image = ImageIO.read(targetFile);
            int[] size = getSize(scale, image);
            return resize(size[0], size[1], savePath, image);
        }
        
        /**
         * <b>function:</b> 將一個網絡圖片文件按照指定的比例進行縮放
         * @author hoojo
         * @createDate 2012-2-7 上午10:30:56
         * @param scale 縮放比例
         * @param savePath 保存文件路徑、名稱
         * @param targetFile 本地圖片文件
         * @return 新的文件名稱
         * @throws ImageFormatException
         * @throws IOException
         */
        public static String resize(float scale, String savePath, URL targetURL) throws ImageFormatException, IOException {
            image = ImageIO.read(targetURL);
            int[] size = getSize(scale, image);
            return resize(size[0], size[1], savePath, image);
        }
        
        /**
         * <b>function:</b> 按照固定寬度進行等比縮放本地圖片
         * @author hoojo
         * @createDate 2012-2-7 上午10:49:56
         * @param width 固定寬度
         * @param savePath 保存路徑、名稱
         * @param targetFile 本地目標文件
         * @return 返回保存路徑
         * @throws ImageFormatException
         * @throws IOException
         */
        public static String resize(int width, String savePath, File targetFile) throws ImageFormatException, IOException {
            image = ImageIO.read(targetFile);
            int[] size = getSize(width, image);
            return resize(size[0], size[1], savePath, image);
        }
        
        /**
         * <b>function:</b> 按照固定寬度進行等比縮放網絡圖片
         * @author hoojo
         * @createDate 2012-2-7 上午10:50:52
         * @param width 固定寬度
         * @param savePath 保存路徑、名稱
         * @param targetFile 本地目標文件
         * @return 返回保存路徑
         * @throws ImageFormatException
         * @throws IOException
         */
        public static String resize(int width, String savePath, URL targetURL) throws ImageFormatException, IOException {
            image = ImageIO.read(targetURL);
            int[] size = getSize(width, image);
            return resize(size[0], size[1], savePath, image);
        }
        
        /**
         * 
         * <b>function:</b> 按照固定高度進行等比縮放本地圖片
         * @author hoojo
         * @createDate 2012-2-7 上午10:51:17
         * @param height 固定高度
         * @param savePath 保存路徑、名稱
         * @param targetFile 本地目標文件
         * @return 返回保存路徑
         * @throws ImageFormatException
         * @throws IOException
         */
        public static String resizeByHeight(int height, String savePath, File targetFile) throws ImageFormatException, IOException {
            image = ImageIO.read(targetFile);
            int[] size = getSizeByHeight(height, image);
            return resize(size[0], size[1], savePath, image);
        }
        
        /**
         * <b>function:</b> 按照固定高度進行等比縮放網絡圖片
         * @author hoojo
         * @createDate 2012-2-7 上午10:52:23
         * @param height 固定高度
         * @param savePath 保存路徑、名稱
         * @param targetFile 本地目標文件
         * @return 返回保存路徑
         * @throws ImageFormatException
         * @throws IOException
         */
        public static String resizeByHeight(int height, String savePath, URL targetURL) throws ImageFormatException, IOException {
            image = ImageIO.read(targetURL);
            int[] size = getSizeByHeight(height, image);
            return resize(size[0], size[1], savePath, image);
        }
        
        /**
         * <b>function:</b>
         * @author hoojo
         * @createDate 2012-2-3 上午10:08:47
         * @param args
         * @throws IOException 
         * @throws MalformedURLException 
         * @throws ImageFormatException 
         */
        public static void main(String[] args) throws ImageFormatException, MalformedURLException, IOException {
            
            System.out.println(ScaleImageUtils.resize(140, 140, null, new URL("http://www.open-open.com/lib/images/logo.jpg")));
            ScaleImageUtils.resize(100, 100, ImageQuality.high.getQuality(), null, ImageIO.read(new URL("http://www.open-open.com/lib/images/logo.jpg")));
        }
    }
     


    作者:hoojo
    出處:
    blog:http://blog.csdn.net/IBM_hoojo
             http://hoojo.cnblogs.com
    本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


    版權所有,轉載請注明出處 本文出自:
    分享道版權所有,歡迎轉載,轉載請注明出處,謝謝
    posted on 2012-02-08 13:48 hoojo 閱讀(2559) 評論(0)  編輯  收藏 所屬分類: JavaEEJavaSE
    主站蜘蛛池模板: 免费三级毛片电影片| 亚洲一级在线观看| 免费看国产精品麻豆| 免费下载成人电影| 999任你躁在线精品免费不卡| 免费无遮挡无码视频网站| 久久久无码精品亚洲日韩按摩 | 狠狠综合久久综合88亚洲| 亚洲精品无播放器在线播放| 亚洲国产精品久久丫| 亚洲人成人无码网www国产| 暖暖日本免费在线视频| 免费高清资源黄网站在线观看 | 狠狠综合亚洲综合亚洲色| 久久久久精品国产亚洲AV无码| 亚洲国产成人精品久久| 亚洲成av人片不卡无码| 亚洲人成网男女大片在线播放| 亚洲国产电影在线观看| 2019亚洲午夜无码天堂| 亚洲成av人无码亚洲成av人 | 四只虎免费永久观看| 亚洲人成影院在线无码按摩店| 亚洲中文无码a∨在线观看| 国产人成免费视频| 亚洲最大av无码网址| 亚洲久悠悠色悠在线播放| 一区二区三区无码视频免费福利| 成人免费视频网址| 亚洲美女视频网站| 香蕉视频在线免费看| 在线精品免费视频| 国产亚洲欧洲Aⅴ综合一区 | 久久久亚洲欧洲日产国码aⅴ| 亚洲国产无线乱码在线观看| 中文字幕不卡免费高清视频| 亚洲色大情网站www| 国产婷婷成人久久Av免费高清| 久草视频免费在线| 亚洲色精品vr一区二区三区| 亚洲激情视频图片|