Java生成縮略圖二
import
java.io.
*
;
import
java.awt.
*
;
import
java.awt.image.
*
;
import
com.sun.image.codec.jpeg.
*
;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c)2007-6-13</p>
* <p>Company: fuen</p>
*
@author
楊振朋
*
@version
1.0
*/
public
class
ccc {
private
String srcFile;
private
String destFile;
private
int
width;
private
int
height;
private
Image img;
/**
* 構造函數
*
@param
fileName String
*
@throws
IOException
*/
public
ccc(String fileName)
throws
IOException {
File _file
=
new
File(fileName);
//
讀入文件
this
.srcFile
=
_file.getName();
this
.destFile
=
"
c:/aa.jpg
"
;
//
this.srcFile.substring(0, this.srcFile.lastIndexOf(".")) +"_s.jpg";
img
=
javax.imageio.ImageIO.read(_file);
//
構造Image對象
width
=
img.getWidth(
null
);
//
獲得源圖寬
height
=
img.getHeight(
null
);
//
獲得源圖長
}
/**
* 強制壓縮/放大圖片到固定的大小
*
@param
w int 新寬度
*
@param
h int 新高度
*
@throws
IOException
*/
public
void
resize(
int
w,
int
h)
throws
IOException {
BufferedImage _image
=
new
BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
_image.getGraphics().drawImage(img,
0
,
0
, w, h,
null
);
//
繪製縮小後的圖
FileOutputStream newimageout
=
new
FileOutputStream(destFile);
//
輸出到文件流
/*
* JPEGImageEncoder 將圖像緩衝數據編碼爲 JPEG 數據流。該接口的用戶應在 Raster
* 或 BufferedImage 中提供圖像數據,在 JPEGEncodeParams 對象中設置必要的參數,
* 併成功地打開 OutputStream(編碼 JPEG 流的目的流)。JPEGImageEncoder 接口可
* 將圖像數據編碼爲互換的縮略 JPEG 數據流,該數據流將寫入提供給編碼器的 OutputStream 中。
注意:com.sun.image.codec.jpeg 包中的類並不屬於核心 Java API。它們屬於 Sun 發佈的
JDK 和 JRE 產品的組成部分。雖然其它得到許可方可能選擇發佈這些類,但開發人員不能寄
但願於從非 Sun 實現的軟件中獲得它們。咱們指望相同的功能最終能夠在覈心 API 或標準擴
展中獲得。
*/
JPEGImageEncoder encoder
=
JPEGCodec.createJPEGEncoder(newimageout);
encoder.encode(_image);
//
近JPEG編碼
newimageout.close();
}
/**
* 按照固定的比例縮放圖片
*
@param
t double 比例
*
@throws
IOException
*/
public
void
resize(
double
t)
throws
IOException {
int
w
=
(
int
) (width
*
t);
int
h
=
(
int
) (height
*
t);
resize(w, h);
}
/**
* 以寬度爲基準,等比例放縮圖片
*
@param
w int 新寬度
*
@throws
IOException
*/
public
void
resizeByWidth(
int
w)
throws
IOException {
int
h
=
(
int
) (height
*
w
/
width);
resize(w, h);
}
/**
* 以高度爲基準,等比例縮放圖片
*
@param
h int 新高度
*
@throws
IOException
*/
public
void
resizeByHeight(
int
h)
throws
IOException {
int
w
=
(
int
) (width
*
h
/
height);
resize(w, h);
}
/**
* 按照最大高度限制,生成最大的等比例縮略圖
*
@param
w int 最大寬度
*
@param
h int 最大高度
*
@throws
IOException
*/
public
void
resizeFix(
int
w,
int
h)
throws
IOException {
if
(width
/
height
>
w
/
h) {
resizeByWidth(w);
}
else
{
resizeByHeight(h);
}
}
/**
* 設置目標文件名
* setDestFile
*
@param
fileName String 文件名字符串
*/
public
void
setDestFile(String fileName)
throws
Exception {
if
(
!
fileName.endsWith(
"
.jpg
"
)) {
throw
new
Exception(
"
Dest File Must end with \
"
.jpg\
"
.
"
);
}
destFile
=
fileName;
}
/**
* 獲取目標文件名
* getDestFile
*/
public
String getDestFile() {
return
destFile;
}
/**
* 獲取圖片原始寬度
* getSrcWidth
*/
public
int
getSrcWidth() {
return
width;
}
/**
* 獲取圖片原始高度
* getSrcHeight
*/
public
int
getSrcHeight() {
return
height;
}
/*
* 調用測試
*/
public
static
void
main(String[] args)
throws
Exception {
ccc ccc
=
new
ccc(
"
c:/image2.jpg
"
);
ccc.resizeFix(
500
,
300
); } }
歡迎關注本站公眾號,獲取更多信息