程式很簡單 ,只需要在利用command在程式目錄下執行
java -jar ReSizeImage.jar
就可以啦!
預設是縮成以寬為500px基準,如果要改變寬可以利用
java -jar ReSizeImage.jar 數字
縮完的圖會放到程式目錄的resize目錄裡
我Blog都是以500寬為基準,我想應該夠用了吧^^
程式目錄結構
- ReSizeImage.jar
- jmagick.dll
- lib/Jmagick.jar
記得要先下載Imagick來安裝才能使用喔
按裝方法如下:
範例圖檔可以按圖下載,放置於程式目錄
原始碼下載
class Tool整個壓縮以外的實作均在此
package image;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class Tool {
public Tool(int width) {
// 取得class路徑所有檔案
File f = new File(".");
File[] list = f.listFiles();
for (int i = 0; i < list.length; i++) {
// 是否為圖檔,是的話進行縮圖
if (isImg(getFileType(list[i].getName()))) {
System.out.println("處理檔案名稱:" + list[i].getName());
toCompressImg(list[i].getAbsolutePath(), getRealDir(list[i]
.getAbsolutePath())
+ "resize" + File.separator + list[i].getName(), width);
}
}
}
// 取得副檔名
private String getFileType(String str) {
int startIndex = str.lastIndexOf(46) + 1;
int endIndex = str.length();
return str.substring(startIndex, endIndex); // 副檔名
}
// 傳入副檔名 檢查是否為指定圖檔jpg gif png
private boolean isImg(String str) {
return (("jpg,gif,png").indexOf(str.toLowerCase())) > -1;
}
// 找目錄
private String getRealDir(String str) {
int endIndex = str.lastIndexOf(File.separator) + 1;
int startIndex = 0;
return str.substring(startIndex, endIndex); // 副檔名
}
public void toCompressImg(String iFile, String oFile, int newWidth) {
try {
File fi = new File(iFile); // 大圖文件
if (!fi.exists())
return;
String oPath = oFile
.substring(0, oFile.lastIndexOf(File.separator));
File foPath = new File(oPath);
if (!foPath.exists())
if (foPath.mkdir())
System.out.println("ooo toCompressImg() File out path:"
+ oPath + " mkdir成功!");
File fo = new File(oFile); // 將要轉換出的小圖文件
int nw = newWidth;
// AffineTransform transform = new AffineTransform();
BufferedImage bis = ImageIO.read(fi);
int w = 0, h = 0;
try {
w = bis.getWidth();
h = bis.getHeight();
} catch (Exception e) {
fileCopy(fi, fo);
return;
}
if (nw > (w < h ? h : w)) {
fileCopy(fi, fo);
return;
} // 壓縮寬度比原始檔大,那就不壓縮了;
int nh = (nw * h) / w;
// double sx = (double) nw / w;
// double sy = (double) nh / h;
JMagickScale.Compress(fi.getAbsolutePath(), fo.getAbsolutePath(),
nw, nh);
/*
* transform.setToScale(sx, sy); //System.out.println(w + ", " + h);
* AffineTransformOp ato = new AffineTransformOp(transform, null);
* BufferedImage bid = new BufferedImage(nw, nh,
* BufferedImage.TYPE_3BYTE_BGR); ato.filter(bis, bid);
* ImageIO.write(bid, "jpeg", fo);
*/
} catch (Exception e) {
e.printStackTrace();
}
}
public void fileCopy(File inputFile, File outputFile) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(inputFile);
fos = new FileOutputStream(outputFile);
int c;
while ((c = fis.read()) != -1)
fos.write(c);
fis.close();
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String args[]) {
int width = 500;
try {
width = Integer.parseInt(args[0]);
} catch (Exception e) {
}
if (width <= 0) {
System.out.println("Width Error");
} else {
new Tool(width);
}
}
}
class JmagickScale壓縮的實作
package image;
import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;
public class JMagickScale {
public static void Compress(String source, String To, int width, int height)
throws MagickException {
if (System.getProperty("jmagick.systemclassloader") == null) {
System.setProperty("jmagick.systemclassloader", "no");
}
ImageInfo info = new ImageInfo(source);
MagickImage image = new MagickImage(info);
// resize image
MagickImage scaleImg = image.scaleImage(width, height);
// write image to file
scaleImg.setFileName(To);
scaleImg.writeImage(info);
}
}
成功的圖,可以按進去flickr看壓好的圖效果