?? jpeghandle.java
字號(hào):
/*
* 創(chuàng)建日期 2004-10-18
*
* TODO
*/
package com.ntsky.file;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* @author Administrator
* 圖像處理
* TODO
*/
public class JPEGHandle {
private JPEG jpeg ;
private File file ;
private String contextPath;
public JPEGHandle(File file,JPEG jpeg,String contextPath){
this.file = file;
this.jpeg = jpeg;
this.contextPath = contextPath;
}
protected void encoderPic(){
Image src; //構(gòu)造Image對(duì)象
try {
src = ImageIO.read(file);
int width=src.getWidth(null); //得到源圖寬
int height=src.getHeight(null); //得到源圖長(zhǎng)
int tempWidth = jpeg.getPicWidth();
int tempHeight = jpeg.getPicHeight();
// 當(dāng)圖片高度和寬度確定的情況下
/*if((tempWeidth!=0)&&(tempHeight==0)){
tempHeight =
}*/
// 生成圖片
builderPic(drawPic(src,tempWidth,tempHeight));
} catch (IOException e) {
// TODO 自動(dòng)生成 catch 塊
e.printStackTrace();
}
}
/**
* 繪制圖片框架
* @param src
* @param weigth
* @param height
* @return
*/
private BufferedImage drawPic(Image src,int weigth,int height){
BufferedImage bufferedImage = new BufferedImage(weigth,height,BufferedImage.TYPE_INT_RGB);
bufferedImage.getGraphics().drawImage(src,0,0,weigth,height,null); //繪制縮小后的圖
return bufferedImage;
}
/**
* 生成圖片的各種方案
* 寬度
* @param tag
*/
/*public String getWidth(int width,int height,int tempHeight){
}*/
/**
* 高度
* @param tag
*/
//public String getWidth(){
//}
/**
* 生成圖片部分
* @param tag
*/
private void builderPic(BufferedImage BufferedImage){
FileOutputStream out; //輸出到文件流
try {
String picPath = jpeg.getPicPath(); // 圖片路徑
String picName = jpeg.getPicName(); // 圖片名
// 文件的路徑
String filePath = file.getPath();
filePath = FileUtil.htmlEncode(filePath);
System.out.println("filePath = " + filePath);
//文件名
String fileName = file.getName();
int tempLength = fileName.indexOf("."); //文件名到“.”分割
String picSuffix = fileName.substring(tempLength);
String outPicPath = "";
// 圖片路徑已知的情況
if(picPath == null){
String tempPath = filePath.substring(0,filePath.lastIndexOf("/"))+"/";
// 文件名未知的情況
if(picName == null){
outPicPath = tempPath+fileName.substring(0,tempLength)+"_small"+picSuffix;
//FileUtil.makeFile(outPicPath);
}
else{
// 有文件名的情況
outPicPath = tempPath+picName+picSuffix;
// FileUtil.makeFile(outPicPath);
}
}
else{
// 設(shè)置small圖片路徑的配置
// 目錄的創(chuàng)建
FileUtil.makeDir(picPath,FileUtil.htmlEncode(contextPath));
// System.out.println("picPath = " + picPath + FileUtil.htmlEncode(contextPath));
if(picName == null){
// 不設(shè)置文件名的情況
outPicPath = FileUtil.getAllPath(FileUtil.htmlEncode(contextPath) + picPath) + fileName.substring(0,tempLength)+"_small"+picSuffix;
}
else{
// 有文件名的情況
outPicPath = FileUtil.getAllPath(FileUtil.htmlEncode(contextPath) + picPath) + picName + picSuffix;
}
//System.out.println("outPicPath = " + outPicPath);
}
out = new FileOutputStream(outPicPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(BufferedImage); //近JPEG編碼
out.close();
// 是否想刪除Source原來(lái)的圖片
if(jpeg.isDelSourcePic())
file.delete();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -