• About Me
  • Java基礎教學
  • 部落格聯播

Java利用Imagick來ReSize圖片檔-使用JMagick

分類: Java, opensource, 影像處理, 程式 時間:2009/9/18 瀏覽:819 瀏覽數 — 留下回應

程式很簡單 ,只需要在利用command在程式目錄下執行

java -jar ReSizeImage.jar

就可以啦!

預設是縮成以寬為500px基準,如果要改變寬可以利用

java -jar ReSizeImage.jar 數字

縮完的圖會放到程式目錄的resize目錄裡

我Blog都是以500寬為基準,我想應該夠用了吧^^

程式目錄結構

ReSizeImage.jar

jmagick.dll

lib/Jmagick.jar

記得要先下載Imagick來安裝才能使用喔

按裝方法如下:

JMagick-Java open source free影像壓縮

範例圖檔可以按圖下載,放置於程式目錄

DSCN3599.JPG

原始碼如下:


class Tool整個壓縮以外的實作均在此

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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壓縮的實作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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看壓好的圖效果

resizeDSCN3599.JPG


Related Posts Plugin for WordPress, Blogger...

留下您想說的話:

*