資源簡介
java圖片壓縮處理(可以壓縮為任意大小)

代碼片段和文件信息
import?java.io.*;?
import?java.util.logging.Level;?
import?java.util.logging.Logger;?
import?java.awt.*;?
import?com.sun.image.codec.jpeg.*;?
import?java.awt.image.BufferedImage;?
/**?
*?
*?@author?Jiangpin?
*/?
public?class?Img_Middle?{?
????public?void?img_change(String?urlString?name)?
????{?
????????????Tosmallerpic(urlnew?File(url+name)“_middle“name188165(float)0.7);?
????????????Tosmallerpic(urlnew?File(url+name)“_small“name4545(float)0.7);?
????????????Tosmallerpic(urlnew?File(url+name)“_smaller“name116100(float)0.7);?
????}?
???private?static?void?img_change11(String?urlString?name)?
????{?
????????????File??f=new?File(url+name);
????????????float?per?=(300*1024)/f.length();
????????????if(f.length()>300*1024){
????????????????per?=(float)(300*1024)/f.length();
????????????????per?=(float)(per+(1-per)/5);
????????????}else{
????????????????
????????????????per=(float)1.0;
????????????}
????????????System.out.println(“圖片大小:“+f.length()/1024+“K“?);
????????????System.out.println(“壓宿比例:“+per?);
????????????//Tosmallerpic(urlnew?File(url+name)“_middle“name1440900(float)0.7);?
????????????Tosmallerpic(urlnew?File(url+name)“_small“name10241024(float)0.7);?
????????????Tosmallerpic(urlnew?File(url+name)“_smaller“name1024500(float)0.7);?
????????????Tosmallerpic(urlnew?File(url+name)“_middle“name1024640(float)per);?
????}?
/**?
*?
*?@param?f?圖片所在的文件夾路徑?
*?@param?filelist?圖片路徑?
*?@param?ext?擴展名?
*?@param?n?圖片名?
*?@param?w?目標寬?
*?@param?h?目標高?
*?@param?per?百分比?
*/?
??????private?static?void??Tosmallerpic(String?fFile?filelistString?extString?nint?wint?hfloat?per){?
????????????Image?src;?
????????????try?{?
????????????????src?=?javax.imageio.ImageIO.read(filelist);?//構(gòu)造Image對象?
???????????????String?img_midname=f+n.substring(0n.indexOf(“.“))+ext+n.substring(n.indexOf(“.“));?
???????????????int?old_w=src.getWidth(null);?//得到源圖寬?
???????????????int?old_h=src.getHeight(null);?
???????????????int?new_w=0;?
???????????????int?new_h=0;?//得到源圖長?
???????????????double?w2=(old_w*1.00)/(w*1.00);?
???????????????double?h2=(old_h*1.00)/(h*1.00);?
???????????????//圖片跟據(jù)長寬留白,成一個正方形圖。?
???????????????BufferedImage?oldpic;?
???????????????/*
???????????????if(old_w>old_h)?
???????????????{?
???????????????????oldpic=new?BufferedImage(old_wold_wBufferedImage.TYPE_INT_RGB);?
???????????????}else{
??????????????????if(old_w ?????????????????????oldpic=new?BufferedImage(old_hold_hBufferedImage.TYPE_INT_RGB);?
??????????????????}else{?
????????????????????oldpic=new?BufferedImage(old_wold_hBufferedImage.TYPE_INT_RGB);?
?????????????????}?????
???????????????}*/
????????????????oldpic=new?BufferedImage(old_wold_hBufferedImage.TYPE_INT_RGB);??
????????????????Graphics2D?g?=?oldpic.createGraphics();?
????????????????g.setColor(Color.white);?
????????????????
????????????????if(old_w>old_h)?
????????????????{?
??????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????30634??2011-09-24?23:25??java圖片壓縮處理(可以壓縮為任意大小,壓縮后高清,不變形(留白),可以改后綴名,可以修改壓縮分辨率)?-?-?ITeye技術(shù)網(wǎng)站.htm
?????文件???????6141??2011-09-24?23:58??Img_Middle.java
-----------?---------??----------?-----??----
????????????????36775????????????????????2
評論
共有 條評論