先来看看效果:
测试一:
原图:
效果图:
测试二:
原图:
效果图:
代码部分:
1 /**
2 *
3 */
4 package com.b510;
5
6 import java.awt.Rectangle;
7 import java.awt.image.BufferedImage;
8 import java.io.File;
9 import java.io.FileInputStream;
10 import java.io.IOException;
11 import java.util.Date;
12 import java.util.Iterator;
13
14 import javax.imageio.ImageIO;
15 import javax.imageio.ImageReadParam;
16 import javax.imageio.ImageReader;
17 import javax.imageio.stream.ImageInputStream;
18
19 /**
20 * @date 2012-11-26
21 * @author xhw
22 *
23 */
24 public class ImageCut {
25
26 /**
27 * 源图片路径名称如:c:\1.jpg
28 */
29 private String srcpath = "e:/poool.jpg";
30 /**
31 * 剪切图片存放路径名称.如:c:\2.jpg
32 */
33 private String subpath = "e:/pool_end";
34 /**
35 * jpg图片格式
36 */
37 private static final String IMAGE_FORM_OF_JPG = "jpg";
38 /**
39 * png图片格式
40 */
41 private static final String IMAGE_FORM_OF_PNG = "png";
42 /**
43 * 剪切点x坐标
44 */
45 private int x;
46
47 /**
48 * 剪切点y坐标
49 */
50 private int y;
51
52 /**
53 * 剪切点宽度
54 */
55 private int width;
56
57 /**
58 * 剪切点高度
59 */
60 private int height;
61
62 public ImageCut() {
63
64 }
65
66 public ImageCut(int x, int y, int width, int height) {
67 this.x = x;
68 this.y = y;
69 this.width = width;
70 this.height = height;
71 }
72
73 public static void main(String[] args) throws Exception {
74 ImageCut imageCut = new ImageCut(134, 0, 366, 366);
75 imageCut.cut(imageCut.getSrcpath(), imageCut.getSubpath());
76 }
77
78 /**
79 * 返回包含所有当前已注册 ImageReader 的 Iterator,这些 ImageReader 声称能够解码指定格式。
80 * 参数:formatName - 包含非正式格式名称 .(例如 "jpeg" 或 "tiff")等 。
81 *
82 * @param postFix
83 * 文件的后缀名
84 * @return
85 */
86 public Iterator<ImageReader> getImageReadersByFormatName(String postFix) {
87 switch (postFix) {
88 case IMAGE_FORM_OF_JPG:
89 return ImageIO.getImageReadersByFormatName(IMAGE_FORM_OF_JPG);
90 case IMAGE_FORM_OF_PNG:
91 return ImageIO.getImageReadersByFormatName(IMAGE_FORM_OF_PNG);
92 default:
93 return ImageIO.getImageReadersByFormatName(IMAGE_FORM_OF_JPG);
94 }
95 }
96
97 /**
98 * 对图片裁剪,并把裁剪完蛋新图片保存 。
99 * @param srcpath 源图片路径
100 * @param subpath 剪切图片存放路径
101 * @throws IOException
102 */
103 public void cut(String srcpath, String subpath) throws IOException {
104 FileInputStream is = null;
105 ImageInputStream iis = null;
106 try {
107 // 读取图片文件
108 is = new FileInputStream(srcpath);
109
110 // 获取文件的后缀名
111 String postFix = getPostfix(srcpath);
112 System.out.println("图片格式为:" + postFix);
113 /*
114 * 返回包含所有当前已注册 ImageReader 的 Iterator,这些 ImageReader 声称能够解码指定格式。
115 * 参数:formatName - 包含非正式格式名称 .(例如 "jpeg" 或 "tiff")等 。
116 */
117 Iterator<ImageReader> it = getImageReadersByFormatName(postFix);
118
119 ImageReader reader = it.next();
120 // 获取图片流
121 iis = ImageIO.createImageInputStream(is);
122
123 /*
124 * <p>iis:读取源.true:只向前搜索 </p>.将它标记为 ‘只向前搜索’。
125 * 此设置意味着包含在输入源中的图像将只按顺序读取,可能允许 reader 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分。
126 */
127 reader.setInput(iis, true);
128
129 /*
130 * <p>描述如何对流进行解码的类<p>.用于指定如何在输入时从 Java Image I/O
131 * 框架的上下文中的流转换一幅图像或一组图像。用于特定图像格式的插件 将从其 ImageReader 实现的
132 * getDefaultReadParam 方法中返回 ImageReadParam 的实例。
133 */
134 ImageReadParam param = reader.getDefaultReadParam();
135
136 /*
137 * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象
138 * 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。
139 */
140 Rectangle rect = new Rectangle(x, y, width, height);
141
142 // 提供一个 BufferedImage,将其用作解码像素数据的目标。
143 param.setSourceRegion(rect);
144 /*
145 * 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象,并将 它作为一个完整的
146 * BufferedImage 返回。
147 */
148 BufferedImage bi = reader.read(0, param);
149
150 // 保存新图片
151 ImageIO.write(bi, postFix, new File(subpath + "_" + new Date().getTime() + "." + postFix));
152 } finally {
153 if (is != null)
154 is.close();
155 if (iis != null)
156 iis.close();
157 }
158
159 }
160
161 /**
162 * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"<br>
163 *
164 * @param inputFilePath
165 * @return
166 */
167 public String getPostfix(String inputFilePath) {
168 return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
169 }
170
171 public int getHeight() {
172 return height;
173 }
174
175 public void setHeight(int height) {
176 this.height = height;
177 }
178
179 public String getSrcpath() {
180 return srcpath;
181 }
182
183 public void setSrcpath(String srcpath) {
184 this.srcpath = srcpath;
185 }
186
187 public String getSubpath() {
188 return subpath;
189 }
190
191 public void setSubpath(String subpath) {
192 this.subpath = subpath;
193 }
194
195 public int getWidth() {
196 return width;
197 }
198
199 public void setWidth(int width) {
200 this.width = width;
201 }
202
203 public int getX() {
204 return x;
205 }
206
207 public void setX(int x) {
208 this.x = x;
209 }
210
211 public int getY() {
212 return y;
213 }
214
215 public void setY(int y) {
216 this.y = y;
217 }
218
219 }