實例代碼:代碼中有不是注釋,很好看懂!
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
|
package com.json.test; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; public class OperateImage { // ===源圖片路徑名稱如:c:\1.jpg private String srcpath ; // ===剪切圖片存放路徑名稱.如:c:\2.jpg private String subpath ; // ===剪切點x坐標 private int x ; private int y ; // ===剪切點寬度 private int width ; private int height ; public OperateImage() { } public OperateImage( int x, int y, int width, int height) { this .x = x ; this .y = y ; this .width = width ; this .height = height ; } /** * 對圖片裁剪,并把裁剪完蛋新圖片保存 。 */ public void cut() throws IOException { FileInputStream is = null ; ImageInputStream iis = null ; try { // 讀取圖片文件 is = new FileInputStream(srcpath); /* * 返回包含所有當前已注冊 ImageReader 的 Iterator,這些 ImageReader * 聲稱能夠解碼指定格式。 參數:formatName - 包含非正式格式名稱 . *(例如 "jpeg" 或 "tiff")等 。 */ Iterator < ImageReader > it=ImageIO.getImageReadersByFormatName("jpg"); ImageReader reader = it.next(); // 獲取圖片流 iis = ImageIO.createImageInputStream(is); /* * <p>iis:讀取源.true:只向前搜索 </p>.將它標記為 ‘只向前搜索'。 * 此設置意味著包含在輸入源中的圖像將只按順序讀取,可能允許 reader * 避免緩存包含與以前已經讀取的圖像關聯的數據的那些輸入部分。 */ reader.setInput(iis, true ) ; /* * <p>描述如何對流進行解碼的類<p>.用于指定如何在輸入時從 Java Image I/O * 框架的上下文中的流轉換一幅圖像或一組圖像。用于特定圖像格式的插件 * 將從其 ImageReader 實現的 getDefaultReadParam 方法中返回 * ImageReadParam 的實例。 */ ImageReadParam param = reader.getDefaultReadParam(); /* * 圖片裁剪區域。Rectangle 指定了坐標空間中的一個區域,通過 Rectangle 對象 * 的左上頂點的坐標(x,y)、寬度和高度可以定義這個區域。 */ Rectangle rect = new Rectangle(x, y, width, height); // 提供一個 BufferedImage,將其用作解碼像素數據的目標。 param.setSourceRegion(rect); /* * 使用所提供的 ImageReadParam 讀取通過索引 imageIndex 指定的對象,并將 * 它作為一個完整的 BufferedImage 返回。 */ BufferedImage bi=reader.read( 0 ,param); // 保存新圖片 ImageIO.write(bi, "jpg" , new File(subpath)); } finally { if (is != null ) is.close() ; if (iis != null ) iis.close(); } } public int getHeight() { return height; } public void setHeight( int height) { this .height = height; } public String getSrcpath() { return srcpath; } public void setSrcpath(String srcpath) { this .srcpath = srcpath; } public String getSubpath() { return subpath; } public void setSubpath(String subpath) { this .subpath = subpath; } public int getWidth() { return width; } public void setWidth( int width) { this .width = width; } public int getX() { return x; } public void setX( int x) { this .x = x; } public int getY() { return y; } public void setY( int y) { this .y = y; } public static void main(String[] args) { OperateImage operateImage = new OperateImage( 20 , 20 , 100 , 100 ); operateImage.srcpath = "C:/test/1.jpg" ; operateImage.subpath = "C:/test/2.jpg" ; try { operateImage.cut(); } catch (IOException e) { e.printStackTrace(); } } } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/nooneyanghuijava/article/details/8147350