最近項目需要用到對圖片的壓縮處理,Graphics是Java繪圖的核心類,它可以支持兩種繪圖:一種是基本的繪圖,如畫線、矩形、圓等;另一種是畫圖像,主要用於動畫製作。
直接上代碼:
package Splash;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author wxb
* @date 2011-01-14
* @versions 1.0 圖片壓縮工具類 提供的方法中可以設定生成的
* 縮略圖片的大小尺寸等
*/
public class PictureChangeSize {
public static void main(String args[]) {
try {
Map<Integer, String> map = readfile("C:/Users/zhqPc/Desktop/zhqaz.jpg", null);
for (int i = 0; i < map.size(); i++) {
System.out.println(map.get(i) + " ==" + i);
System.out.println();
String oldpath = map.get(i);
compressImage(map.get(i), "C:/Users/zhqPc/Desktop/zhqazya.jpg", 1500);
}
} catch (Exception ex) {
}
System.out.println("ok");
}
/**
* 圖片文件讀取
* @param srcImgPath
* @return
*/
private static BufferedImage InputImage(String srcImgPath) {
BufferedImage srcImage = null;
try {
FileInputStream in = new FileInputStream(srcImgPath);
srcImage = javax.imageio.ImageIO.read(in);
} catch (IOException e) {
System.out.println("讀取圖片文件出錯!" + e.getMessage());
e.printStackTrace();
}
return srcImage;
}
/**
* * 將圖片按照指定的圖片尺寸壓縮
* @param srcImgPath :源圖片路徑
* @param outImgPath :輸出的壓縮圖片的路徑
* @param new_w :壓縮後的圖片寬
* @param new_h :壓縮後的圖片高
*/
public static void compressImage(String srcImgPath, String outImgPath,
int new_w, int new_h) {
BufferedImage src = InputImage(srcImgPath);
disposeImage(src, outImgPath, new_w, new_h);
}
/**
* 指定長或者寬的最大值來壓縮圖片
* @param srcImgPath :源圖片路徑
* @param outImgPath :輸出的壓縮圖片的路徑
* @param maxLength :長或者寬的最大值
*/
public static void compressImage(String srcImgPath, String outImgPath,
int maxLength) {
// 得到圖片
BufferedImage src = InputImage(srcImgPath);
if (null != src) {
int old_w = src.getWidth();
// 得到源圖寬
int old_h = src.getHeight();
// 得到源圖長
int new_w = 0;
// 新圖的寬
int new_h = 0;
// 新圖的長
// 根據圖片尺寸壓縮比得到新圖的尺寸
if (old_w > old_h) {
// 圖片要縮放的比例
new_w = maxLength;
new_h = (int) Math.round(old_h * ((float) maxLength / old_w));
} else {
new_w = (int) Math.round(old_w * ((float) maxLength / old_h));
new_h = maxLength;
}
disposeImage(src, outImgPath, new_w, new_h);
}
}
/**
* 處理圖片
* @param src
* @param outImgPath
* @param new_w
* @param new_h
*/
private synchronized static void disposeImage(BufferedImage src,
String outImgPath, int new_w, int new_h) {
// 得到圖片
int old_w = src.getWidth();
// 得到源圖寬
int old_h = src.getHeight();
// 得到源圖長
BufferedImage newImg = null;
// 判斷輸入圖片的類型
switch (src.getType()) {
case 13:
// png,gifnewImg = new BufferedImage(new_w, new_h,
// BufferedImage.TYPE_4BYTE_ABGR);
break;
default:
newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
break;
}
Graphics2D g = newImg.createGraphics();
// 從原圖上取顏色繪製新圖
g.drawImage(src, 0, 0, old_w, old_h, null);
g.dispose();
// 根據圖片尺寸壓縮比得到新圖的尺寸
newImg.getGraphics().drawImage(
src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0,
null);
// 調用方法輸出圖片文件
OutImage(outImgPath, newImg);
}
/**
* 將圖片文件輸出到指定的路徑,並可設定壓縮質量
* @param outImgPath
* @param newImg
*/
private static void OutImage(String outImgPath, BufferedImage newImg) {
// 判斷輸出的文件夾路徑是否存在,不存在則創建
File file = new File(outImgPath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}// 輸出到文件流
try {
ImageIO.write(newImg,
outImgPath.substring(outImgPath.lastIndexOf(".") + 1),
new File(outImgPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Map<Integer, String> readfile(String filepath,
Map<Integer, String> pathMap) throws Exception {
if (pathMap == null) {
pathMap = new HashMap<Integer, String>();
}
File file = new File(filepath);
// 文件
if (!file.isDirectory()) {
pathMap.put(pathMap.size(), file.getPath());
} else if (file.isDirectory()) { // 如果是目錄, 遍歷所有子目錄取出所有文件名
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "/" + filelist[i]);
if (!readfile.isDirectory()) {
pathMap.put(pathMap.size(), readfile.getPath());
} else if (readfile.isDirectory()) { // 子目錄的目錄
readfile(filepath + "/" + filelist[i], pathMap);
}
}
}
return pathMap;
}
}