java處理圖片--圖片的縮放,旋轉和馬賽克化

這是我自己結合網上的一些資料封裝的java圖片處理類,支持圖片的縮放,旋轉,馬賽克化。(轉載請註明出處:http://blog.csdn.net/u012116457)
不多說,上代碼:

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<code class="hljs java">package deal;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
 
import javax.imageio.ImageIO;
 
/**
 * 圖像處理類.
 *
 * @author nagsh
 *
 */
public class ImageDeal {
 
    String openUrl; // 原始圖片打開路徑
    String saveUrl; // 新圖保存路徑
    String saveName; // 新圖名稱
    String suffix; // 新圖類型 只支持gif,jpg,png
 
    public ImageDeal(String openUrl, String saveUrl, String saveName,
            String suffix) {
        this.openUrl = openUrl;
        this.saveName = saveName;
        this.saveUrl = saveUrl;
        this.suffix = suffix;
    }
 
    /**
     * 圖片縮放.
     *
     * @param width
     *            需要的寬度
     * @param height
     *            需要的高度
     * @throws Exception
     */
    public void zoom(int width, int height) throws Exception {
        double sx = 0.0;
        double sy = 0.0;
 
        File file = new File(openUrl);
        if (!file.isFile()) {
            throw new Exception("ImageDeal>>>" + file + " 不是一個圖片文件!");
        }
        BufferedImage bi = ImageIO.read(file); // 讀取該圖片
        // 計算x軸y軸縮放比例--如需等比例縮放,在調用之前確保參數width和height是等比例變化的
        sx = (double) width / bi.getWidth();
        sy = (double) height / bi.getHeight();
 
        AffineTransformOp op = new AffineTransformOp(
                AffineTransform.getScaleInstance(sx, sy), null);
        File sf = new File(saveUrl, saveName + "." + suffix);
        Image zoomImage = op.filter(bi, null);
        try {
            ImageIO.write((BufferedImage) zoomImage, suffix, sf); // 保存圖片
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
    /**
     * 旋轉
     *
     * @param degree
     *            旋轉角度
     * @throws Exception
     */
    public void spin(int degree) throws Exception {
        int swidth = 0; // 旋轉後的寬度
        int sheight = 0; // 旋轉後的高度
        int x; // 原點橫座標
        int y; // 原點縱座標
 
        File file = new File(openUrl);
        if (!file.isFile()) {
            throw new Exception("ImageDeal>>>" + file + " 不是一個圖片文件!");
        }
        BufferedImage bi = ImageIO.read(file); // 讀取該圖片
        // 處理角度--確定旋轉弧度
        degree = degree % 360;
        if (degree < 0)
            degree = 360 + degree;// 將角度轉換到0-360度之間
        double theta = Math.toRadians(degree);// 將角度轉爲弧度
 
        // 確定旋轉後的寬和高
        if (degree == 180 || degree == 0 || degree == 360) {
            swidth = bi.getWidth();
            sheight = bi.getHeight();
        } else if (degree == 90 || degree == 270) {
            sheight = bi.getWidth();
            swidth = bi.getHeight();
        } else {
            swidth = (int) (Math.sqrt(bi.getWidth() * bi.getWidth()
                    + bi.getHeight() * bi.getHeight()));
            sheight = (int) (Math.sqrt(bi.getWidth() * bi.getWidth()
                    + bi.getHeight() * bi.getHeight()));
        }
 
        x = (swidth / 2) - (bi.getWidth() / 2);// 確定原點座標
        y = (sheight / 2) - (bi.getHeight() / 2);
 
        BufferedImage spinImage = new BufferedImage(swidth, sheight,
                bi.getType());
        // 設置圖片背景顏色
        Graphics2D gs = (Graphics2D) spinImage.getGraphics();
        gs.setColor(Color.white);
        gs.fillRect(0, 0, swidth, sheight);// 以給定顏色繪製旋轉後圖片的背景
 
        AffineTransform at = new AffineTransform();
        at.rotate(theta, swidth / 2, sheight / 2);// 旋轉圖象
        at.translate(x, y);
        AffineTransformOp op = new AffineTransformOp(at,
                AffineTransformOp.TYPE_BICUBIC);
        spinImage = op.filter(bi, spinImage);
        File sf = new File(saveUrl, saveName + "." + suffix);
        ImageIO.write(spinImage, suffix, sf); // 保存圖片
 
    }
    /**
     * 馬賽克化.
     * @param size  馬賽克尺寸,即每個矩形的長寬
     * @return
     * @throws Exception
     */
    public boolean mosaic(int size) throws Exception {
        File file = new File(openUrl);
        if (!file.isFile()) {
            throw new Exception("ImageDeal>>>" + file + " 不是一個圖片文件!");
        }
        BufferedImage bi = ImageIO.read(file); // 讀取該圖片
        BufferedImage spinImage = new BufferedImage(bi.getWidth(),
                bi.getHeight(), bi.TYPE_INT_RGB);
        if (bi.getWidth() < size || bi.getHeight() < size || size <= 0) { // 馬賽克格尺寸太大或太小
            return false;
        }
 
        int xcount = 0; // 方向繪製個數
        int ycount = 0; // y方向繪製個數
        if (bi.getWidth() % size == 0) {
            xcount = bi.getWidth() / size;
        } else {
            xcount = bi.getWidth() / size + 1;
        }
        if (bi.getHeight() % size == 0) {
            ycount = bi.getHeight() / size;
        } else {
            ycount = bi.getHeight() / size + 1;
        }
        int x = 0;   //座標
        int y = 0;
        // 繪製馬賽克(繪製矩形並填充顏色)
        Graphics gs = spinImage.getGraphics();
        for (int i = 0; i < xcount; i++) {
            for (int j = 0; j < ycount; j++) {
                //馬賽克矩形格大小
                 int mwidth = size;
                 int mheight = size;
                 if(i==xcount-1){   //橫向最後一個比較特殊,可能不夠一個size
                     mwidth = bi.getWidth()-x;
                 }
                 if(j == ycount-1){  //同理
                     mheight =bi.getHeight()-y;
                 }
              // 矩形顏色取中心像素點RGB值
                int centerX = x;
                int centerY = y;
                if (mwidth % 2 == 0) {
                    centerX += mwidth / 2;
                } else {
                    centerX += (mwidth - 1) / 2;
                }
                if (mheight % 2 == 0) {
                    centerY += mheight / 2;
                } else {
                    centerY += (mheight - 1) / 2;
                }
                Color color = new Color(bi.getRGB(centerX, centerY));
                gs.setColor(color);
                gs.fillRect(x, y, mwidth, mheight);
                y = y + size;// 計算下一個矩形的y座標
            }
            y = 0;// 還原y座標
            x = x + size;// 計算x座標
        }
        gs.dispose();
        File sf = new File(saveUrl, saveName + "." + suffix);
        ImageIO.write(spinImage, suffix, sf); // 保存圖片
        return true;
    }
 
    public static void main(String[] args) throws Exception {
        ImageDeal imageDeal = new ImageDeal("e://1.jpg", "e://", "2", "jpg");
        // 測試縮放
        /* imageDeal.zoom(200, 300); */
        // 測試旋轉
        /* imageDeal.spin(90); */
        //測試馬賽克
        /*imageDeal.mosaic(4);*/
    }
 
}
</code>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章