亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java實現自動生成縮略圖片

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

本文實例為大家分享了Java實現自動生成縮略圖片的具體代碼,供大家參考,具體內容如下

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

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; // ? ? ? ? 對象是否己經初始化?
? ? ? ? private String pic_big_pathfilename = null; //定義源圖片所在的帶路徑目錄的文件名
? ? ? ? private String pic_small_pathfilename = null; // 生成小圖片的帶存放路徑目錄的文件名?
? ? ? ? private int smallpicwidth = 0; //定義生成小圖片的寬度和高度,給其一個就可以了?
? ? ? ? private int smallpicheight = 0;?
? ? ? ? private int pic_big_width=0;
? ? ? ? private int pic_big_height=0;
? ? ? ? private double picscale = 0; //定義小圖片的相比原圖片的比例?
? ? ? ? /**?
? ? ? ? * 構造函數?
? ? ? ? * @param 沒有參數?
? ? ? ? */?
? ? ? ? public JpegTool(){
? ? ? ? ? ? ? ? this.isInitFlag = false;?
? ? ? ? }?
? ? ? ? /**?
? ? ? ? * 私有函數,重置所有的參數?
? ? ? ? * @param 沒有參數?
? ? ? ? * @return 沒有返回參數?
? ? ? ? */?
? ? ? ? private void resetJpegToolParams(){?
? ? ? ? ? ? ? ? this.picscale = 0;?
? ? ? ? ? ? ? ? this.smallpicwidth = 0;?
? ? ? ? ? ? ? ? this.smallpicheight = 0;?
? ? ? ? ? ? ? ? this.isInitFlag = false;?
? ? ? ? }?
? ? ? ? /**?
? ? ? ? * @param scale 設置縮影圖像相對于源圖像的大小比例如 0.5?
? ? ? ? * @throws JpegToolException?
? ? ? ? */?
? ? ? ? public void SetScale(double scale) throws JpegToolException
? ? ? ? {?
? ? ? ? ? ? ? ? if(scale<=0){?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? throw new JpegToolException(" 縮放比例不能為 0 和負數! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? this.resetJpegToolParams();?
? ? ? ? ? ? ? ? this.picscale = scale;?
? ? ? ? ? ? ? ? this.isInitFlag = true;?
? ? ? ? }?
? ? ? ? /**?
? ? ? ? * @param smallpicwidth 設置縮影圖像的寬度?
? ? ? ? * @throws JpegToolException?
? ? ? ? */?
? ? ? ? public void SetSmallWidth(int smallpicwidth) throws JpegToolException?
? ? ? ? {?
? ? ? ? ? ? ? ? if(smallpicwidth<=0)
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? ? ? throw new JpegToolException(" 縮影圖片的寬度不能為 0 和負數! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? this.resetJpegToolParams();?
? ? ? ? ? ? ? ? this.smallpicwidth = smallpicwidth;?
? ? ? ? ? ? ? ? this.isInitFlag = true;?
? ? ? ? }?
?
? ? ? ? /**?
? ? ? ? * @param smallpicheight 設置縮影圖像的高度?
? ? ? ? * @throws JpegToolException?
? ? ? ? */?
?
? ? ? ? public void SetSmallHeight(int smallpicheight) throws JpegToolException {?
? ? ? ? ? ? ? ? if(smallpicheight<=0)
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ?throw new JpegToolException(" 縮影圖片的高度不能為 0 和負數! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? 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(" 對象參數沒有初始化! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? 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(" 讀取源圖像文件出錯! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? 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)
? ? ? ? ? ? ? ? {// 根據設定的寬度求出長度?
? ? ? ? ? ? ? ? ? ? ? ? smallw = this.smallpicwidth;// 新生成的縮略圖像的長度?
? ? ? ? ? ? ? ? ? ? ? ? smallh = (smallw*pic_big_height)/pic_big_width ;// 新生成的縮略圖像的寬度?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if(this.smallpicheight!=0)
? ? ? ? ? ? ? ? {// 根據設定的長度求出寬度?
? ? ? ? ? ? ? ? ? ? ? ? smallh = this.smallpicheight;// 新生成的縮略圖像的長度?
? ? ? ? ? ? ? ? ? ? ? ? smallw = (smallh*pic_big_width)/pic_big_height;// 新生成的縮略圖像的寬度?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if(this.picscale!=0)
? ? ? ? ? ? ? ? {// 根據設置的縮小比例設置圖像的長和寬?
? ? ? ? ? ? ? ? ? ? ? ? smallw = (int)((float)pic_big_width*this.picscale);?
? ? ? ? ? ? ? ? ? ? ? ? smallh = (int)((float)pic_big_height*this.picscale);?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? throw new JpegToolException(" 對象參數初始化不正確! ");?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? double sx = (double)smallw/pic_big_width;//小/大圖像的寬度比例?
? ? ? ? ? ? ? ? double sy = (double)smallh/pic_big_height;//小/大圖像的高度比例?
? ? ? ? ? ? ? ? transform.setToScale(sx,sy);// 設置圖像轉換的比例?
? ? ? ? ? ? ? ? //生成圖像轉換操作對象?
? ? ? ? ? ? ? ? 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(" 寫入縮略圖像文件出錯! ");?
? ? ? ? ? ? ? ? }?
? ? ? ? }
}

二、異常處理類:

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

三、調用的例子:

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();
?? ??? ?}
?? ?}
?
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Eclipse中安裝反編譯工具Fernflower的方法(Enhanced Class Decompiler)

    Eclipse中安裝反編譯工具Fernflower的方法(Enhanced Class Decompiler)

    這篇文章主要介紹了Eclipse中安裝反編譯工具Fernflower的方法(Enhanced Class Decompiler),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • SpringBoot整合Spring Security構建安全的Web應用

    SpringBoot整合Spring Security構建安全的Web應用

    pring Security是一個強大的身份驗證和訪問控制框架,本文主要介紹了SpringBoot整合Spring Security構建安全的Web應用,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • Java使用easypoi快速導入導出的實現

    Java使用easypoi快速導入導出的實現

    這篇文章主要介紹了實現Java使用easypoi快速導入導出的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • SpringCloud Gateway自定義filter獲取body中的數據為空的問題

    SpringCloud Gateway自定義filter獲取body中的數據為空的問題

    這篇文章主要介紹了SpringCloud Gateway自定義filter獲取body中的數據為空,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Java中Quartz高可用定時任務快速入門

    Java中Quartz高可用定時任務快速入門

    如果你想做定時任務,有高可用方面的需求,或者僅僅想入門快,上手簡單,那么選用它準沒錯,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 對arraylist中元素進行排序實例代碼

    對arraylist中元素進行排序實例代碼

    這篇文章主要介紹了對arraylist中元素進行排序實例代碼,還是比較不錯的,這里分享給大家,供需要的朋友參考。
    2017-11-11
  • Mybatis有查詢結果但存不進實體類的解決方案

    Mybatis有查詢結果但存不進實體類的解決方案

    這篇文章主要介紹了Mybatis有查詢結果但存不進實體類的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Java深入淺出說流的使用

    Java深入淺出說流的使用

    這篇文章主要介紹了Java深入淺出說流的使用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • SpringBoot之導入靜態(tài)資源詳解

    SpringBoot之導入靜態(tài)資源詳解

    今天帶大家學習SpringBoot導入靜態(tài)資源的過程,文中介紹的非常詳細,對正在學習java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • Mybatis中Collection集合標簽的使用詳解

    Mybatis中Collection集合標簽的使用詳解

    這篇文章主要介紹了Mybatis中Collection集合標簽的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06

最新評論