jpg圖像文件縮放Java類(按指定比例縮放,按指定寬高縮放)

b4c2 10年前發布 | 3K 次閱讀 Java IT 兼職 撰稿 作者

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;

/**

  • jpg圖像文件縮放類
  • 本類實現一個對 JPG/JPEG 圖像文件進行縮放處理的方法 即給定一個 JPG 文件,可以生成一個該 JPG 文件的縮影圖像文件 (JPG 格式 )
  • 提供三種生成縮影圖像的方法: 1 、設置縮影文件的寬度,根據設置的寬度和源圖像文件的大小來確定新縮影文件的長度來生成縮影圖像 2
  • 、設置縮影文件的長度,根據設置的長度和源圖像文件的大小來確定新縮影文件的寬度來生成縮影圖像 3
  • 、設置縮影文件相對于源圖像文件的比例大小,根據源圖像文件的大小及設置的比例來確定新縮影文件的大小來生成縮影圖像
  • 新生成的縮影圖像可以比原圖像大,這時即是放大源圖像。 */ public class JPGTransformer { // 對象是否己經初始化 private boolean isInitFlag = false;

    // 定義生目標圖片的寬度和高度,給其一個就可以了 private int targetPicWidth = 0; private int targetPicHeight = 0;

    // 定義目標圖片的相比原圖片的比例 private double picScale = 0;

    /**

    • 構造函數 */ public JPGTransformer() { this.isInitFlag = false; }

      /**

    • 重置JPG圖片縮放器 */ public void resetJPGTransformer() { this.picScale = 0; this.targetPicWidth = 0; this.targetPicHeight = 0; this.isInitFlag = false; }

      /**

    • 設置目標圖片相對于源圖片的縮放比例
    • @param scale
    • @throws JPGException */ public void setPicScale(double scale) throws JPGException { if (scale <= 0) {

       throw new JPGException(" 縮放比例不能為0和負數! ");
      

      }

      this.resetJPGTransformer(); this.picScale = scale; this.isInitFlag = true; }

      /**

    • 設置目標圖片的寬度
    • @param width
    • @throws JPGException */ public void SetSmallWidth(int width) throws JPGException { if (width <= 0) {

       throw new JPGException(" 縮影圖片的寬度不能為 0 和負數! ");
      

      }

      this.resetJPGTransformer(); this.targetPicWidth = width; this.isInitFlag = true; }

      /**

    • 設置目標圖片的高度
    • @param height
    • @throws JPGException */ public void SetSmallHeight(int height) throws JPGException { if (height <= 0) {

       throw new JPGException(" 縮影圖片的高度不能為 0 和負數! ");
      

      }

      this.resetJPGTransformer(); this.targetPicHeight = height; this.isInitFlag = true; }

      /**

    • 開始縮放圖片
    • @param srcPicFileName
    • 源圖片的文件名
    • @param targetPicFileName
    • 生成目標圖片的文件名
    • @throws JPGException */ public void transform(String srcPicFileName, String targetPicFileName)

       throws JPGException {
      
      

      if (!this.isInitFlag) {

       throw new JPGException(" 對象參數沒有初始化! ");
      

      } if (srcPicFileName == null || targetPicFileName == null) {

       throw new JPGException(" 包含文件名的路徑為空! ");
      

      } if ((!srcPicFileName.toLowerCase().endsWith("jpg"))

           && (!srcPicFileName.toLowerCase().endsWith("jpeg"))) {
       throw new JPGException(" 只能處理 JPG/JPEG 文件! ");
      

      } if ((!targetPicFileName.toLowerCase().endsWith("jpg"))

           && !targetPicFileName.toLowerCase().endsWith("jpeg")) {
       throw new JPGException(" 只能處理 JPG/JPEG 文件! ");
      

      }

      // 新建源圖片和生成圖片的文件對象 File fin = new File(srcPicFileName); File fout = new File(targetPicFileName);

      // 通過緩沖讀入源圖片文件 BufferedImage bSrc = null; try {

       // 讀取文件生成BufferedImage
       bSrc = ImageIO.read(fin);
      

      } catch (IOException ex) {

       throw new JPGException(" 讀取源圖像文件出錯! ");
      

      } // 源圖片的寬度和高度 int srcW = bSrc.getWidth(); int srcH = bSrc.getHeight();

      // 設置目標圖片的實際寬度和高度 int targetW = 0; int targetH = 0; if (this.targetPicWidth != 0) {

       // 根據設定的寬度求出長度
       targetW = this.targetPicWidth;
       targetH = (targetW * srcH) / srcW;
      

      } else if (this.targetPicHeight != 0) {

       // 根據設定的長度求出寬度
       targetH = this.targetPicHeight;
       targetW = (targetH * srcW) / srcH;
      

      } else if (this.picScale != 0) {

       // 根據設置的縮放比例設置圖像的長和寬
       targetW = (int) ((float) srcW * this.picScale);
       targetH = (int) ((float) srcH * this.picScale);
      

      } else {

       throw new JPGException(" 對象參數初始化不正確! ");
      

      }

      System.out.println(" 源圖片的分辨率: " + srcW + "×" + srcH); System.out.println(" 目標圖片的分辨率: " + targetW + "×" + targetH); // 目標圖像的緩沖對象 BufferedImage bTarget = new BufferedImage(targetW, targetH,

           BufferedImage.TYPE_3BYTE_BGR);
      
      

      // 求得目標圖片與源圖片寬度、高度的比例。 double sx = (double) targetW / srcW; double sy = (double) targetH / srcH;

      // 構造圖像變換對象 AffineTransform transform = new AffineTransform(); // 設置圖像轉換的比例 transform.setToScale(sx, sy);

      // 構造圖像轉換操作對象 AffineTransformOp ato = new AffineTransformOp(transform, null); // 實現轉換,將bSrc轉換成bTarget ato.filter(bSrc, bTarget);

      // 輸出目標圖片 try {

       // 將目標圖片的BufferedImage寫到文件中去,jpeg為圖片的格式
       ImageIO.write(bTarget, "jpeg", fout);
      

      } catch (IOException ex1) {

       throw new JPGException(" 寫入縮略圖像文件出錯! ");
      

      } }

      /**

    • JPG縮放時可能出現的異常 */ class JPGException extends Exception { public JPGException(String msg) {

       super(msg);
      

      }

      public JPGException(Exception e) {

       super(e);
      

      }

      public JPGException(Throwable t) {

       super(t);
      

      }

      public JPGException(String msg, Throwable t) {

       super(msg, t);
      

      } }

      /**

    • 測試文件為: C:/temp/scenery.jpg
    • @param args
    • @throws JPGException */ public static void main(String[] args) throws JPGException { JPGTransformer jpg = new JPGTransformer();

      jpg.setPicScale(0.5); // 將原圖片縮小一半 String srcFileName = "C:/temp/scenery.jpg"; String targetFileName = "C:/temp/scenery_new.jpg"; jpg.transform(srcFileName, targetFileName); } } </pre>

 本文由用戶 b4c2 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!