最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)自動生成縮略圖片

 更新時(shí)間:2022年04月22日 15:17:04   作者:憤怒的火柴  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)自動生成縮略圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java實(shí)現(xiàn)自動生成縮略圖片的具體代碼,供大家參考,具體內(nèi)容如下

一、自動生成縮略圖方法:

package writeimg;
?
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
?
public class JpegTool {?
? ? ? ? private boolean isInitFlag = false; // ? ? ? ? 對象是否己經(jīng)初始化?
? ? ? ? private String pic_big_pathfilename = null; //定義源圖片所在的帶路徑目錄的文件名
? ? ? ? private String pic_small_pathfilename = null; // 生成小圖片的帶存放路徑目錄的文件名?
? ? ? ? private int smallpicwidth = 0; //定義生成小圖片的寬度和高度,給其一個(gè)就可以了?
? ? ? ? private int smallpicheight = 0;?
? ? ? ? private int pic_big_width=0;
? ? ? ? private int pic_big_height=0;
? ? ? ? private double picscale = 0; //定義小圖片的相比原圖片的比例?
? ? ? ? /**?
? ? ? ? * 構(gòu)造函數(shù)?
? ? ? ? * @param 沒有參數(shù)?
? ? ? ? */?
? ? ? ? public JpegTool(){
? ? ? ? ? ? ? ? this.isInitFlag = false;?
? ? ? ? }?
? ? ? ? /**?
? ? ? ? * 私有函數(shù),重置所有的參數(shù)?
? ? ? ? * @param 沒有參數(shù)?
? ? ? ? * @return 沒有返回參數(shù)?
? ? ? ? */?
? ? ? ? private void resetJpegToolParams(){?
? ? ? ? ? ? ? ? this.picscale = 0;?
? ? ? ? ? ? ? ? this.smallpicwidth = 0;?
? ? ? ? ? ? ? ? this.smallpicheight = 0;?
? ? ? ? ? ? ? ? this.isInitFlag = false;?
? ? ? ? }?
? ? ? ? /**?
? ? ? ? * @param scale 設(shè)置縮影圖像相對于源圖像的大小比例如 0.5?
? ? ? ? * @throws JpegToolException?
? ? ? ? */?
? ? ? ? public void SetScale(double scale) throws JpegToolException
? ? ? ? {?
? ? ? ? ? ? ? ? if(scale<=0){?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? throw new JpegToolException(" 縮放比例不能為 0 和負(fù)數(shù)! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? this.resetJpegToolParams();?
? ? ? ? ? ? ? ? this.picscale = scale;?
? ? ? ? ? ? ? ? this.isInitFlag = true;?
? ? ? ? }?
? ? ? ? /**?
? ? ? ? * @param smallpicwidth 設(shè)置縮影圖像的寬度?
? ? ? ? * @throws JpegToolException?
? ? ? ? */?
? ? ? ? public void SetSmallWidth(int smallpicwidth) throws JpegToolException?
? ? ? ? {?
? ? ? ? ? ? ? ? if(smallpicwidth<=0)
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? ? ? throw new JpegToolException(" 縮影圖片的寬度不能為 0 和負(fù)數(shù)! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? this.resetJpegToolParams();?
? ? ? ? ? ? ? ? this.smallpicwidth = smallpicwidth;?
? ? ? ? ? ? ? ? this.isInitFlag = true;?
? ? ? ? }?
?
? ? ? ? /**?
? ? ? ? * @param smallpicheight 設(shè)置縮影圖像的高度?
? ? ? ? * @throws JpegToolException?
? ? ? ? */?
?
? ? ? ? public void SetSmallHeight(int smallpicheight) throws JpegToolException {?
? ? ? ? ? ? ? ? if(smallpicheight<=0)
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ?throw new JpegToolException(" 縮影圖片的高度不能為 0 和負(fù)數(shù)! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? this.resetJpegToolParams();?
? ? ? ? ? ? ? ? this.smallpicheight = smallpicheight;?
? ? ? ? ? ? ? ? this.isInitFlag = true;?
? ? ? ? }?
? ? ? ??
? ? ? ? /**
? ? ? ? ?*返回大圖片路徑?
? ? ? ? ?*/
? ? ? ? public String getpic_big_pathfilename()
? ? ? ? {
? ? ? ? ? ? ? ? return this.pic_big_pathfilename;
? ? ? ? }
? ? ? ? /**
? ? ? ? ?* 返回小圖片路徑
? ? ? ? ?*/
? ? ? ? public String getpic_small_pathfilename()
? ? ? ? {
? ? ? ? ? ? ? ? return this.pic_small_pathfilename;
? ? ? ? }
? ? ? ??
? ? ? ? public int getsrcw()
? ? ? ? {
? ? ? ? ? ? ? ? return this.pic_big_width;
? ? ? ? }
? ? ? ? public int getsrch()
? ? ? ? {
? ? ? ? ? ? ? ? return this.pic_big_height;
? ? ? ? }
? ? ? ? /**?
? ? ? ? * 生成源圖像的縮影圖像?
? ? ? ? * @param pic_big_pathfilename 源圖像文件名,包含路徑(如 windows 下 C:\\pic.jpg ; Linux 下 /home/abner/pic/pic.jpg )?
? ? ? ? * @param pic_small_pathfilename 生成的縮影圖像文件名,包含路徑(如 windows 下 C:\\pic_small.jpg ; Linux 下 /home/abner/pic/pic_small.jpg )?
? ? ? ? * @throws JpegToolException?
? ? ? ? */?
? ? ? ? public void doFinal(String pic_big_pathfilename,String pic_small_pathfilename) throws JpegToolException {?
? ? ? ? ? ? ? ? if(!this.isInitFlag){?
? ? ? ? ? ? ? ? ? ? throw new JpegToolException(" 對象參數(shù)沒有初始化! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? if(pic_big_pathfilename==null || pic_small_pathfilename==null){?
? ? ? ? ? ? ? ? ? ? throw new JpegToolException(" 包含文件名的路徑為空! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? if((!pic_big_pathfilename.toLowerCase().endsWith("jpg")) && (!pic_big_pathfilename.toLowerCase().endsWith("jpeg"))){?
? ? ? ? ? ? ? ? ? ? throw new JpegToolException(" 只能處理 JPG/JPEG 文件! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? if((!pic_small_pathfilename.toLowerCase().endsWith("jpg")) && !pic_small_pathfilename.toLowerCase().endsWith("jpeg")){?
? ? ? ? ? ? ? ? ? ? throw new JpegToolException(" 只能處理 JPG/JPEG 文件! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? this.pic_big_pathfilename=pic_big_pathfilename;
? ? ? ? ? ? ? ? this.pic_small_pathfilename=pic_small_pathfilename;
? ? ? ? ? ? ? ? int smallw = 0;?
? ? ? ? ? ? ? ? int smallh = 0;?
? ? ? ? ? ? ? ? // 新建源圖片和生成的小圖片的文件對象?
? ? ? ? ? ? ? ? File fi = new File(pic_big_pathfilename);?
? ? ? ? ? ? ? ? File fo = new File(pic_small_pathfilename);?
? ? ? ? ? ? ? ? //生成圖像變換對象?
? ? ? ? ? ? ? ? AffineTransform transform = new AffineTransform();?
? ? ? ? ? ? ? ? //通過緩沖讀入源圖片文件?
? ? ? ? ? ? ? ? BufferedImage bsrc = null;?
? ? ? ? ? ? ? ? try {?
? ? ? ? ? ? ? ? bsrc = ImageIO.read(fi);?
? ? ? ? ? ? ? ? }catch (IOException ex) {?
? ? ? ? ? ? ? ? ? ? throw new JpegToolException(" 讀取源圖像文件出錯(cuò)! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? this.pic_big_width= bsrc.getWidth();// 原圖像的長度?
? ? ? ? ? ? ? ? this.pic_big_height = bsrc.getHeight();// 原圖像的寬度?
? ? ? ? ? ? ? ? double scale = (double)pic_big_width/pic_big_height;// 圖像的長寬比例?
? ? ? ? ? ? ? ? if(this.smallpicwidth!=0)
? ? ? ? ? ? ? ? {// 根據(jù)設(shè)定的寬度求出長度?
? ? ? ? ? ? ? ? ? ? ? ? smallw = this.smallpicwidth;// 新生成的縮略圖像的長度?
? ? ? ? ? ? ? ? ? ? ? ? smallh = (smallw*pic_big_height)/pic_big_width ;// 新生成的縮略圖像的寬度?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if(this.smallpicheight!=0)
? ? ? ? ? ? ? ? {// 根據(jù)設(shè)定的長度求出寬度?
? ? ? ? ? ? ? ? ? ? ? ? smallh = this.smallpicheight;// 新生成的縮略圖像的長度?
? ? ? ? ? ? ? ? ? ? ? ? smallw = (smallh*pic_big_width)/pic_big_height;// 新生成的縮略圖像的寬度?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if(this.picscale!=0)
? ? ? ? ? ? ? ? {// 根據(jù)設(shè)置的縮小比例設(shè)置圖像的長和寬?
? ? ? ? ? ? ? ? ? ? ? ? smallw = (int)((float)pic_big_width*this.picscale);?
? ? ? ? ? ? ? ? ? ? ? ? smallh = (int)((float)pic_big_height*this.picscale);?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? throw new JpegToolException(" 對象參數(shù)初始化不正確! ");?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? double sx = (double)smallw/pic_big_width;//小/大圖像的寬度比例?
? ? ? ? ? ? ? ? double sy = (double)smallh/pic_big_height;//小/大圖像的高度比例?
? ? ? ? ? ? ? ? transform.setToScale(sx,sy);// 設(shè)置圖像轉(zhuǎn)換的比例?
? ? ? ? ? ? ? ? //生成圖像轉(zhuǎn)換操作對象?
? ? ? ? ? ? ? ? AffineTransformOp ato = new AffineTransformOp(transform,null);?
? ? ? ? ? ? ? ? //生成縮小圖像的緩沖對象?
? ? ? ? ? ? ? ? BufferedImage bsmall = new BufferedImage(smallw,smallh,BufferedImage.TYPE_3BYTE_BGR);?
? ? ? ? ? ? ? ? //生成小圖像?
? ? ? ? ? ? ? ? ato.filter(bsrc,bsmall);?
? ? ? ? ? ? ? ? //輸出小圖像?
? ? ? ? ? ? ? ? try{
? ? ? ? ? ? ? ? ? ? ? ? ImageIO.write(bsmall, "jpeg", fo);?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (IOException ex1)?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ?throw new JpegToolException(" 寫入縮略圖像文件出錯(cuò)! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? }
}

二、異常處理類:

package jpegtool;
?
? ? public class JpegToolException extends Exception {
? ? ? ? ? ? private String errMsg = "";?
? ? ? ? ? ? public JpegToolException(String errMsg)?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? this.errMsg = errMsg;?
? ? ? ? ? ? }?
?
? ? ? ? ? ? public String getMsg(){?
? ? ? ? ? ? ? ? return "JpegToolException:"+this.errMsg;?
? ? ? ? ? ? }?
? ? }

三、調(diào)用的例子:

package writeimg;
?
public class T {
?
?
?? ?public static void main(String[] args) {
?
?? ??? ?JpegTool j = new JpegTool();
?? ??? ?try {
?? ??? ??? ?j.SetScale(0.7);
?? ??? ??? ?j.SetSmallHeight(100);
?? ??? ??? ?j.doFinal("D:\\305\\c\\javatest\\src\\11.jpg","D:\\305\\c\\javatest\\src\\22.jpg");
?? ??? ?} catch (JpegToolException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
?
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 深入分析Java內(nèi)存區(qū)域的使用詳解

    深入分析Java內(nèi)存區(qū)域的使用詳解

    本篇文章對Java內(nèi)存區(qū)域的使用進(jìn)行了詳細(xì)的介紹。需要的朋友參考下
    2013-05-05
  • Java類的加載連接和初始化實(shí)例分析

    Java類的加載連接和初始化實(shí)例分析

    這篇文章主要介紹了Java類的加載連接和初始化,結(jié)合具體實(shí)例形式分析了java類的加載、連接、初始化相關(guān)原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-07-07
  • SpringAop源碼及調(diào)用過程概述

    SpringAop源碼及調(diào)用過程概述

    這篇文章主要介紹了SpringAop源碼及調(diào)用過程概述,Spring AOP(面向切面編程)是Spring框架的一個(gè)重要特性,它提供了一種在程序運(yùn)行期間動態(tài)地將額外的行為織入到代碼中的方式,需要的朋友可以參考下
    2023-10-10
  • Java?Mybatis框架由淺入深全解析上篇

    Java?Mybatis框架由淺入深全解析上篇

    MyBatis是一個(gè)優(yōu)秀的持久層框架,它對jdbc的操作數(shù)據(jù)庫的過程進(jìn)行封裝,使開發(fā)者只需要關(guān)注SQL本身,而不需要花費(fèi)精力去處理例如注冊驅(qū)動、創(chuàng)建connection、創(chuàng)建statement、手動設(shè)置參數(shù)、結(jié)果集檢索等jdbc繁雜的過程代碼本文將為大家初步的介紹一下MyBatis的使用
    2022-07-07
  • java設(shè)計(jì)模式-裝飾者模式詳解

    java設(shè)計(jì)模式-裝飾者模式詳解

    這篇文章主要介紹了Java設(shè)計(jì)模式之裝飾者模式詳解和代碼實(shí)例,Decorator模式(別名Wrapper):動態(tài)將職責(zé)附加到對象上,若要擴(kuò)展功能,裝飾者提供了比繼承更具彈性的代替方案,需要的朋友可以參考下
    2021-07-07
  • JVM內(nèi)存模型/內(nèi)存空間:運(yùn)行時(shí)數(shù)據(jù)區(qū)

    JVM內(nèi)存模型/內(nèi)存空間:運(yùn)行時(shí)數(shù)據(jù)區(qū)

    這篇文章主要介紹了JVM內(nèi)存模型/內(nèi)存空間的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java虛擬機(jī),感興趣的朋友可以了解詳細(xì),希望能夠給你帶來幫助
    2021-08-08
  • java排查一個(gè)線上死循環(huán)cpu暴漲的過程分析

    java排查一個(gè)線上死循環(huán)cpu暴漲的過程分析

    這篇文章主要介紹了java排查一個(gè)線上死循環(huán)cpu暴漲的過程分析,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Springboot基于websocket實(shí)現(xiàn)簡單在線聊天功能

    Springboot基于websocket實(shí)現(xiàn)簡單在線聊天功能

    這篇文章主要介紹了Springboot基于websocket實(shí)現(xiàn)簡單在線聊天功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • java實(shí)現(xiàn)視頻轉(zhuǎn)碼工具類

    java實(shí)現(xiàn)視頻轉(zhuǎn)碼工具類

    這篇文章主要介紹了java實(shí)現(xiàn)視頻轉(zhuǎn)碼,涉及到工具類用到的參數(shù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • Spring Boot無縫集成MongoDB

    Spring Boot無縫集成MongoDB

    這篇文章主要介紹了Spring Boot無縫集成MongoDB的相關(guān)知識,本文涉及到MongoDB的概念和nosql的應(yīng)用場景,需要的朋友可以參考下
    2017-04-04

最新評論

盱眙县| 吉安县| 和顺县| 望城县| 鸡东县| 沛县| 敦化市| 资兴市| 文成县| 宁城县| 涿鹿县| 柳江县| 枣强县| 盘山县| 楚雄市| 汪清县| 兴化市| 桃园市| 青川县| 鄂尔多斯市| 宁陵县| 黄龙县| 金湖县| 定南县| 南江县| 蓬安县| 嘉定区| 增城市| 肥乡县| 隆尧县| 奉节县| 周宁县| 随州市| 祁门县| 元朗区| 彩票| 呼和浩特市| 武隆县| 吉水县| 嘉峪关市| 无极县|