自己實現圖片壓縮技術,直接上程式碼,絕對好用(優化效果不錯)

風的王子發表於2013-05-23
package photo.zipcode;


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 zcw
 */
public class Img_Middle {
public void img_change(String url, String name) {
Tosmallerpic(url, new File(url + name), "_middle", name, 188, 165,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_small", name, 45, 45,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_smaller", name, 116, 100,
(float) 0.7);
}


/**
* 將檔案放到一個資料夾,自動生成三種不同的大小壓縮圖片

* @param url
* @param name
*/
private static void img_change11(String url, String 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(url,new
// File(url+name),"_middle",name,1440,900,(float)0.7);
Tosmallerpic(url, new File(url + name), "_small", name, 1024, 1024,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_smaller", name, 1024, 500,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_middle", name, 1024, 640,
(float) per);
}


private static void img_changeOne(String url, String 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(url,new
// File(url+name),"_middle",name,1440,900,(float)0.7);
Tosmallerpic(url, new File(url + name), "_small", name, 500, 375,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_smaller", name, 500, 375,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_middle", name, 500, 375,
(float) per);
}


/**
* 壓縮圖片將圖片寬高和路徑名字傳入

* @param url
* @param name
* @param w
* @param h
*/
private static void img_changeMyself(String url, String name, int w, int h) {
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(url,new
// File(url+name),"_middle",name,1440,900,(float)0.7);
Tosmallerpic(url, new File(url + name), "_small", name, w, h,
(float) 0.7);
// Tosmallerpic(url,new
// File(url+name),"_smaller",name,500,375,(float)0.7);
// Tosmallerpic(url, new File(url + name), "", name, w, h, (float) per);
}


/**
* 生成新的圖片江河源圖片檔名字一致,放在當前資料夾的test臨時目錄

* @param f
*            圖片所在的資料夾路徑
* @param filelist
*            圖片路徑
* @param ext
*            副檔名
* @param n
*            圖片名
* @param w
*            目標寬
* @param h
*            目標高
* @param per
*            百分比
*/
private static void Tosmallerpic(String f, File filelist, String ext,
String n, int w, int h, float per) {
Image src;
try {
src = javax.imageio.ImageIO.read(filelist); // 構造Image物件


String img_midname = f + "test/" + n.substring(0, n.indexOf("."))
+ 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);


// 圖片跟據長寬留白,成一個正方形圖。
BufferedImage oldpic;
/*
* if(old_w>old_h) { oldpic=new
* BufferedImage(old_w,old_w,BufferedImage.TYPE_INT_RGB); }else{
* if(old_w<old_h){ oldpic=new
* BufferedImage(old_h,old_h,BufferedImage.TYPE_INT_RGB); }else{
* oldpic=new BufferedImage(old_w,old_h,BufferedImage.TYPE_INT_RGB);
* } }
*/
oldpic = new BufferedImage(old_w, old_h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = oldpic.createGraphics();
g.setColor(Color.white);


if (old_w > old_h) {
g.fillRect(0, 0, old_w, old_w);
g.drawImage(src, 0, 0, old_w, old_h, Color.white, null);
// g.fillRect(0, 0, old_w, old_w);
// g.drawImage(src, 0, (old_w - old_h) / 2, old_w, old_h,
// Color.white, null);
} else {
if (old_w < old_h) {
g.fillRect(0, 0, old_h, old_h);
g.drawImage(src, 0, 0, old_w, old_h, Color.white, null);
// g.fillRect(0,0,old_h,old_h);
// g.drawImage(src, (old_h - old_w) / 2, 0, old_w, old_h,
// Color.white, null);
} else {
// g.fillRect(0,0,old_h,old_h);
g.drawImage(src.getScaledInstance(old_w, old_h,
Image.SCALE_SMOOTH), 0, 0, null);
}
}


g.fillRect(0, 0, old_w, old_h);
g.drawImage(src, 0, 0, old_w, old_h, Color.white, null);
g.dispose();
src = oldpic;
// 圖片調整為方形結束
if (old_w > w)
new_w = (int) Math.round(old_w / w2);
else
new_w = old_w;
if (old_h > h)
new_h = (int) Math.round(old_h / h2);// 計算新圖長寬
else
new_h = old_h;
new_h = h;
new_w = w;


BufferedImage tag = new BufferedImage(new_w, new_h,
BufferedImage.TYPE_INT_RGB);
// tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //繪製縮小後的圖
tag.getGraphics().drawImage(
src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0,
0, null);
FileOutputStream newimage = new FileOutputStream(img_midname); // 輸出到檔案流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/* 壓縮質量 */
jep.setQuality(per, true);
encoder.encode(tag, jep);
// encoder.encode(tag); //近JPEG編碼
newimage.close();
} catch (IOException ex) {
Logger.getLogger(Img_Middle.class.getName()).log(Level.SEVERE,
null, ex);
}
}


/**/public static void main(String args[]) {
// String n = "xiangshou1.png";
String f = "G:/map/";
File file = new File(f);
// img_changeOne(f, n);


File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].exists() && files[i].isFile()) {
System.err.println(files[i].getName());
img_changeMyself(f, files[i].getName(), 1202, 765);
}
}


// img_changeMyself(f, n);
/*
* if(file.exists()) { File[] filelist=file.listFiles(); for(int
* i=0;i<filelist.length;i++) { String n=filelist[i].getName();
* Tosmallerpic(f,filelist[i],"_middle",n,185,160,(float)0.7);
* Tosmallerpic(f,filelist[i],"_small",n,45,45,(float)0.7);
* Tosmallerpic(f,filelist[i],"_smaller",n,116,100,(float)0.7); } }
*/
}
}

相關文章