JAVA中通用JPEG縮影圖Bean的製作與樣本

來源:互聯網
上載者:User
JAVA中通用JPEG縮影圖Bean的製作與樣本


說明: 
前一段時間,看到網上一個網友貼出一個用 Java 跨平台的產生 Jpeg 圖片縮影圖的方法。最近整理了一下,形成了一個通用的類。並製作成一個 jar 包。以方便重用。並給出了使用樣本。(作者: abnerchai ;聯絡: josserchai@yahoo.com ) 

一、原始碼 
1、JpegTool.java 
// JpegTool.java 

package com.abner.jpegtool; 

import javax.imageio.ImageIO; 

import javax.imageio.IIOException; 

import java.awt.image.BufferedImage; 

import java.awt.Image; 

import java.io.File; 

import java.awt.image.AffineTransformOp; 

import java.awt.geom.AffineTransform; 

import com.abner.jpegtool.JpegToolException; 

import java.io.*; 

/** 

* 本類實現一個對 JPG/JPEG 影像檔進行縮影處理的方法 

* 即給定一個 JPG 檔案,可以產生一個該 JPG 檔案的縮影影像檔 (JPG 格式 ) 

* 提供三種產生縮影映像的方法: 

* 1 、設定縮影檔案的寬度,根據設定的寬度和源影像檔的大小來確定新縮影檔案的長度來產生縮影映像 

* 2 、設定縮影檔案的長度,根據設定的長度和源影像檔的大小來確定新縮影檔案的寬度來產生縮影映像 

* 3 、設定縮影檔案相對於源影像檔的比例大小,根據源影像檔的大小及設定的比例來確定新縮影檔案的大小來產生縮影映像 

* 新產生的縮影映像可以比原映像大,這時即是放大源映像。 

* @author abnerchai contact:josserchai@yahoo.com 

* @version 1.0 

* @exception JpegToolException 

*/ 

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 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; 

} 

/** 

* 產生源映像的縮影映像 

* @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 檔案! "); 

{} 

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(" 讀取源影像檔出錯! "); 

{} 

int srcw = bsrc.getWidth();// 原映像的長度 

int srch = bsrc.getHeight();// 原映像的寬度 

double scale = (double)srcw/srch;// 映像的長寬比例 

if(this.smallpicwidth!=0)// 根據設定的寬度求出長度 

smallw = this.smallpicwidth;// 新產生的縮圖像的長度 

smallh = (smallw*srch)/srcw ;// 新產生的縮圖像的寬度 

{}else if(this.smallpicheight!=0)// 根據設定的長度求出寬度 

smallh = this.smallpicheight;// 新產生的縮圖像的長度 

smallw = (smallh*srcw)/srch;// 新產生的縮圖像的寬度 

{}else if(this.picscale!=0)// 根據設定的縮小比例設定映像的長和寬 

smallw = (int)((float)srcw*this.picscale); 

smallh = (int)((float)srch*this.picscale); 

{}else 

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

{} 

System.out.println(" 源檔案大小: "+srcw+"X"+srch); 

System.out.println(" 新檔案大小: "+smallw+"X"+smallh); 

double sx = (double)smallw/srcw;// 小 / 大映像的寬度比例 

double sy = (double)smallh/srch;// 小 / 大映像的高度比例 

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(" 寫入縮圖像檔案出錯! "); 

{} 

} 

} 

2、JpegToolException.java 
// JpegToolException.java 

package com.abner.jpegtool; 

/** 

* <p>Description: JpegTool 使用的異常類 </p> 

* @author abnerchai 

* @version 1.0 

*/ 

public class JpegToolException extends Exception  

private String errMsg = ""; 

public JpegToolException(String errMsg) { 

this.errMsg = errMsg; 

{} 

public String getMsg() 

return "JpegToolException:"+this.errMsg; 

{} 

} 

二、 jar包製作 
編譯上面兩個檔案,形成兩個 class 檔案,均存放在目錄:目前的目錄 comabnerjpegtool 目錄下面。進入目前的目錄,執行: jar cvf jpegtool.jar com/* ;便在目前的目錄產生一個 jpegtool.jar 檔案。發布這個 jar 包,以後便可以像使用其它 jar 包一樣使用這個工具了,下面我們給出一個樣本。 

三、使用樣本 
為了方便,我們給出在 JSP 檔案中使用該包的樣本 JSP 代碼,首先,設定你的類庫路徑,將 jpegtool.jar 檔案放入你的類庫路徑中。然後設定你的應用伺服器的類庫路徑,將 jpegtool.jar 放入你的應用伺服器的類庫路徑中。然後測試下面的 JSP 代碼: 

<!—test.jsp -- > 

<%@page contentType="text/html; charset=GB2312" %> 

<%@page import = "com.abner.jpegtool.JpegTool"%> 

<html><head><title>test</title></head> 

<body bgcolor="#ffffff"> 

<% 

try 

JpegTool jpegTool = new JpegTool(); 

jpegTool.SetSmallWidth(100); 

jpegTool.doFinal("C:/big.jpeg","C:/small.jpeg"); 

{}catch(Exception e) 

e.printStackTrace(); 

{} 

%> 

</body> 

</html> 

對於在 Servlet 和 Java 程式中使用,類似於上面的 JSP 。 

注意:以上程式必須在支援 JDK1.4 的平台上運行。否則有些類庫找不到。 
AbnerChai [原作]   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.