縮略圖實現,將圖片(jpg,gif,bmp等等)真實的變成想要的大小
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.*;
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.sql.*;
//縮略圖類,
//本java類能將jpg圖片文件,進行等比或非等比的大小轉換。
//具體使用方法
//s_pic(大圖片路徑,生成小圖片路徑,大圖片文件名,生成小圖片文名,生成小圖片寬度,生成小圖片高度,是否等比縮放(默認為true))
public class Small_pic{
String InputDir; //輸入圖路徑
String OutputDir; //輸出圖路徑
String InputFileName; //輸入圖文件名
String OutputFileName; //輸出圖文件名
int OutputWidth=80; //默認輸出圖片寬
int OutputHeight=80; //默認輸出圖片高
int rate=0;
boolean proportion=true; //是否等比縮放標記(默認為等比縮放)
public Small_pic(){
//初始化變量
InputDir="";
OutputDir="";
InputFileName="";
OutputFileName="";
OutputWidth=80;
OutputHeight=80;
rate=0;
}
public void setInputDir(String InputDir){
this.InputDir=InputDir;
}
public void setOutputDir(String OutputDir){
this.OutputDir=OutputDir;
}
public void setInputFileName(String InputFileName){
this.InputFileName=InputFileName;
}
public void setOutputFileName(String OutputFileName){
this.OutputFileName=OutputFileName;
}
public void setOutputWidth(int OutputWidth){
this.OutputWidth=OutputWidth;
}
public void setOutputHeight(int OutputHeight){
this.OutputHeight=OutputHeight;
}
public void setW_H(int width,int height){
this.OutputWidth=width;
this.OutputHeight=height;
}
public String s_pic(){
BufferedImage image;
String NewFileName;
//建立輸出文件對象
File file = new File(OutputDir+OutputFileName);
FileOutputStream tempout =null;
try{
tempout= new FileOutputStream(file);
}catch(Exception ex){
System.out.println(ex.toString());
}
Image img=null;
Toolkit tk=Toolkit.getDefaultToolkit();
Applet app=new Applet();
MediaTracker mt = new MediaTracker(app);
try {
img=tk.getImage(InputDir+InputFileName);
mt.addImage(img, 0);
mt.waitForID(0);
}catch(Exception e) {
e.printStackTrace();
}
if(img.getWidth(null)==-1){
System.out.println(" can't read,retry!"+"<BR>");
return "no";
}else{
int new_w;
int new_h;
if (this.proportion==true) //判斷是否是等比縮放.
{
//為等比縮放計算輸出的圖片寬度及高度
double rate1=((double)img.getWidth(null))/(double)OutputWidth+0.1;
double rate2=((double)img.getHeight(null))/(double)OutputHeight+0.1;
double rate=rate1>rate2?rate1:rate2;
new_w=(int)(((double)img.getWidth(null))/rate);
new_h=(int)(((double)img.getHeight(null))/rate);
}
else{
new_w=OutputWidth; //輸出的圖片寬度
new_h=OutputHeight; //輸出的圖片高度
}
BufferedImage buffImg = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
Graphics g = buffImg.createGraphics();
g.setColor(Color.white);
g.fillRect(0,0,new_w,new_h);
g.drawImage(img,0,0,new_w,new_h,null);
g.dispose();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);
try{
encoder.encode(buffImg);
tempout.close();
}catch(IOException ex){
System.out.println(ex.toString());
}
}
return "ok";
}
public String s_pic(String InputDir,String OutputDir,String InputFileName,String OutputFileName){
//輸入圖路徑
this.InputDir=InputDir;
//輸出圖路徑
this.OutputDir=OutputDir;
//輸入圖文件名
this.InputFileName=InputFileName;
//輸出圖文件名
this.OutputFileName=OutputFileName;
return s_pic();
}
public String s_pic(String InputDir,String OutputDir,String InputFileName,String OutputFileName,int width,int height,boolean gp){
//輸入圖路徑
this.InputDir=InputDir;
//輸出圖路徑
this.OutputDir=OutputDir;
//輸入圖文件名
this.InputFileName=InputFileName;
//輸出圖文件名
this.OutputFileName=OutputFileName
//設置圖片長寬
setW_H(width,height);
//是否是等比縮放 標記
this.proportion=gp;
return s_pic();
}
public static void main(String [] a)
{
//s_pic(大圖片路徑,生成小圖片路徑,大圖片文件名,生成小圖片文名,生成小圖片寬度,生成小圖片高度)
Small_pic mypic =new Small_pic();
System.out.println(
mypic.s_pic("E:\\JAVA\\J2EEDatum\\王亮jsp資料\\縮圖例子\\personal\\",
"E:\\JAVA\\J2EEDatum\\酒劍仙jsp資料\\縮圖例子\\personal\\",
"1.jpg","new1.jpg",80,80,true)
);
}