最近工作需要對服務(wù)器上面的文件進(jìn)行壓縮,并且把原來一些縮略圖進(jìn)行壓縮。貼出代碼片斷供大家交流。
搜略圖實現(xiàn)規(guī)定大小畫布上縮小圖片,方便列表顯示。
圖片壓縮提供了2種方法。1、可以自定義壓縮比。2、采用ImageIO進(jìn)行默認(rèn)壓縮(更安全一些,不會出現(xiàn)內(nèi)存溢出)。
????
private
?
void
?doThumbCompress(File?file)
{
????????
????????BufferedImage?bis
=
null
;
????????
????????BufferedImage?bid
=
null
;
????????

????????
try
{

????????????
int
?nw
=
?
113
;?
//
?定義寬為113
????????????
int
?nh
=
?
84
;?
//
??定義高為84
????????????
????????????bis?
=
?ImageIO.read(file);
????????????
int
?c_w
=
?bis.getWidth();
????????????
int
?c_h
=
?bis.getHeight();


????????????
if
(c_w
<
114
?
&&
?file.length()
<
MIN_LENGTH)
{????????
????????????????bis?
=
?
null
;
????????????????
return
;
????????????}
????????????
if
(c_w
>
WIDTH)
{
????????????????nw??
=
?WIDTH;
????????????????nh?
=
?c_h?
*
?(WIDTH)
/
c_w;
????????????????c_h
=
nh;
????????????????c_w
=
nw;
????????????}
????
????????????

????????????
if
(c_h
>
HEIGHT)
{
????????????????nh?
=
?HEIGHT;
????????????????nw??
=
?c_w?
*
?(HEIGHT)
/
c_h;?????????????
????????????}
????????????
????????????
????????????
????????????bid
=
?
new
?BufferedImage(WIDTH,?HEIGHT,BufferedImage.TYPE_INT_RGB);????????????????????????
????????????Graphics?g?
=
?bid.getGraphics();

????????????g.setColor(Color.white);

????????????g.fillRect(
0
,
0
,?WIDTH,?HEIGHT);
????????????
????????????g.drawImage(bis,(WIDTH
-
nw)
/
2
,
0
,nw,nh,
null
);
????????????
????????????
????????????
????????????
long
?oldFileSize
=
file.length();
????????????
?????????????

????????????
if
(isOverlay)
{
????????????????ImageIO.write(bid,?
"
jpeg
"
,?file);

????????????}
?
else
{
????????????????ImageIO.write(ImageIO.read(file),?
"
jpeg
"
,?
new
?File(outPath
????????????????????????????????
+
File.separator
+
file.getName()));
????????????}
????????????
long
?newFileSize
=
file.length();
????????????log(
"
ok:
"
+
file
+
"
\t
"
+
oldFileSize
+
"
\t
"
+
newFileSize);

????????}
?
catch
?(Exception?e)
{
????????????log(file
+
"
?error:
"
+
e.getMessage());
?????????????
????????}
????????
????????
????????bis
=
null
;
????????
????????bid
=
null
;
????}
????????
???? 壓縮圖片代碼:

????/**?*//**
?????*?可以自定義壓縮比例
?????*?@param?file
?????*/

????private?void?doCompressQuality(File?file)??
{
????????BufferedImage?src?=?null;
????????FileOutputStream?out?=??null;
????????JPEGImageEncoder?encoder?=?null;
????????JPEGEncodeParam??param???=?null;
????????

????????try
{
????????????src=?ImageIO.read(file);//or?png.

????????????if(isOverlay)
{
????????????????out=?new?FileOutputStream(file);

????????????}else
{
????????????????out=?new?FileOutputStream(outPath+File.separator+file.getName());
????????????}
????????????encoder=?JPEGCodec.createJPEGEncoder(out);
????????????param=?encoder.getDefaultJPEGEncodeParam(src);????????????
????????????param.setQuality(imageQuality,?false);
????????????encoder.setJPEGEncodeParam(param);????????????
????????????encoder.encode(src);//近JPEG編碼
????????????out.close();
????????????//ImageIO.write(src,?"jpeg",?new?File(outPath+File.separator+file.getName()));????????????
????????????log("over:"+file.getPath());

????????}?catch?(IOException?e)
{
????????????log("compress?error:"+file.getPath()+";error:"+e.getMessage());
????????}
????????out?=?null;
????????encoder?=?null;
????????param???=?null;
????????src?????=?null;
????}


????/**?*//**
?????*?壓縮圖片,默認(rèn)壓縮比例75%
?????*?@param?file
?????*/

????private?void?doCompress(File?file)
{
????????File?descFile?=?null;
????????boolean?writeOk=false;
????????long?fileSize=file.length();


????????try
{
????????????

????????????if(isOverlay)
{????????????????
????????????????descFile?=?file;
????????????????writeOk?=?ImageIO.write(ImageIO.read(file),?"jpeg",?descFile);

????????????}else
{
????????????????descFile?=?new?File(outPath+File.separator+file.getName());
????????????????writeOk?=?ImageIO.write(ImageIO.read(file),?"jpeg",?descFile);
????????????}????????
????????????

????????????if(writeOk)
{
????????????????log("ok:"+file.getPath()+"\t"+fileSize+"\t"+descFile.length());

????????????}else
{
????????????????log("fail:"+file.getPath()+"\t"+fileSize+"\t"+descFile.length());
????????????}????????????
????????????

????????}?catch?(Exception?e)
{
????????????log("compress?error:"+file.getPath()+";descFile="+descFile+";error:"+e.getMessage());
????????}
????????descFile=null;
????}