亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > Java > 正文

java實現創建縮略圖、伸縮圖片比例生成的方法

2019-11-26 15:21:16
字體:
來源:轉載
供稿:網友

本文實例講述了java實現創建縮略圖、伸縮圖片比例生成的方法。分享給大家供大家參考。具體實現方法如下:

該實例支持將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
 * @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
     * @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")));
    }
}

希望本文所述對大家的Java程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲人在线观看| 国产一区红桃视频| 91中文在线观看| 国产精品午夜一区二区欲梦| 成人免费视频网址| 亚洲男人的天堂在线播放| 中文字幕日本欧美| 91在线观看免费| 亚洲精品一区二三区不卡| 亚洲第一区在线观看| 欧美日本中文字幕| 久久精品99久久久香蕉| 九九热这里只有精品6| 欧美贵妇videos办公室| 欧美日韩另类在线| 日本一欧美一欧美一亚洲视频| 亚洲视频视频在线| 成人国内精品久久久久一区| 91在线高清免费观看| 亚洲第一免费网站| 日韩亚洲欧美中文在线| 久久成人精品一区二区三区| 亚洲人精选亚洲人成在线| 日韩成人在线网站| 国产精品免费一区二区三区都可以| 亚洲美女精品成人在线视频| 亚洲国产精品国自产拍av秋霞| 精品国产美女在线| 国产欧美一区二区| 欧美电影院免费观看| 欧美日韩国产一区二区三区| 日韩电影中文字幕av| 欧美疯狂xxxx大交乱88av| 欧美精品videos另类日本| 欧美第一淫aaasss性| 色综合亚洲精品激情狠狠| 欧美性猛交xxxxx水多| 亚洲国产日韩欧美综合久久| 久久久久国产精品免费网站| 91精品国产色综合久久不卡98口| 国产欧美在线视频| 91久久精品美女| 欧美插天视频在线播放| 青青久久av北条麻妃黑人| 亚洲第一中文字幕在线观看| 国产91色在线|| 黄色91在线观看| 国产精品v日韩精品| 精品国产依人香蕉在线精品| 国产在线拍揄自揄视频不卡99| 亚洲电影免费观看高清完整版在线| 欧美中文在线字幕| 亚洲人成网在线播放| 97精品国产aⅴ7777| 成人a视频在线观看| 在线国产精品播放| 55夜色66夜色国产精品视频| 亚洲天堂开心观看| 激情成人在线视频| 日韩欧美在线免费观看| 97在线观看免费| 久久伊人精品视频| 91在线视频精品| 日韩精品在线第一页| 亚洲精品理论电影| 欧美一级淫片播放口| 欧美激情一区二区久久久| 国产精品羞羞答答| 91成人在线播放| 日韩av在线直播| 成人日韩av在线| 亚洲国产精品va在线观看黑人| 午夜欧美不卡精品aaaaa| 亚洲一级免费视频| 亚洲第一男人天堂| 欧美华人在线视频| 国产一区深夜福利| 日韩经典第一页| 456亚洲影院| 97在线视频精品| 高清欧美性猛交xxxx黑人猛交| 91精品国产91久久| 欧美激情一区二区三区久久久| 亚洲人成网站999久久久综合| 亚洲精品资源在线| 欧洲精品毛片网站| 久久亚洲精品毛片| 亚洲最大成人在线| 98精品国产自产在线观看| 亚洲国产精品一区二区三区| 疯狂做受xxxx欧美肥白少妇| 精品久久久久久国产91| 在线日韩第一页| 午夜精品久久久久久久久久久久久| 精品香蕉在线观看视频一| 精品无码久久久久久国产| 国产精品www色诱视频| 一本色道久久88综合日韩精品| 一区二区三区视频免费| 国内精品久久久久| 日韩麻豆第一页| 欧美华人在线视频| 有码中文亚洲精品| 精品亚洲一区二区三区| 性色av一区二区三区红粉影视| 国产91精品高潮白浆喷水| 久久人人爽人人爽人人片av高请| 亚洲国产成人精品一区二区| 国产一区二区三区高清在线观看| 午夜精品一区二区三区在线视| 伊人青青综合网站| 欧美一级大片视频| 亚洲国产精品人久久电影| 国产精品偷伦视频免费观看国产| 色狠狠av一区二区三区香蕉蜜桃| 亚洲japanese制服美女| 中文字幕一区二区精品| 色婷婷亚洲mv天堂mv在影片| 欧美在线一区二区三区四| 国产精品三级网站| 91麻豆国产语对白在线观看| 成人网欧美在线视频| 久久成人精品视频| 欧美高清在线播放| 97av在线影院| 国产成人av在线| 亚洲一区二区三区xxx视频| 亚洲欧美一区二区三区在线| 久久国产精品久久久久久久久久| 91国内精品久久| 欧美一级大胆视频| 国产91色在线| 国内精品模特av私拍在线观看| 亚洲成人1234| 午夜精品美女自拍福到在线| 欧美成人免费在线视频| 久热精品视频在线| 亚洲美腿欧美激情另类| 亚洲国产欧美在线成人app| 高清欧美一区二区三区| 成人免费淫片aa视频免费| 国产在线不卡精品| 欧美性猛xxx| 亚洲日本aⅴ片在线观看香蕉| 国产亚洲精品va在线观看| 欧美一级视频一区二区| 亚洲最大成人免费视频| 国产成人精品久久二区二区| 国产精品一区二区久久| 91免费看国产| 欧美性色视频在线| 在线播放日韩av| 亚洲国产欧美一区二区三区久久| 在线看国产精品| 91沈先生作品| 91欧美精品午夜性色福利在线| 动漫精品一区二区| 国产精品久久久久秋霞鲁丝| 欧美大尺度在线观看| 不卡av日日日| 亚洲精品99久久久久中文字幕| 欧美在线观看视频| 在线观看国产成人av片| 91精品国产自产在线老师啪|