package cn.byref.demo.image;import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.io.File;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;import org.junit.Test;public class ImageDemo { @Test public void test_cut_image() throws IOException { String imgPath = "C:\\Users\\GJ\\Desktop\\1.jpg"; String subPath = "C:\\Users\\GJ\\Desktop\\2.jpg"; File f = new File(imgPath); File t = new File(subPath); if (t.exists()) { t.delete(); } //图片输入流 ImageInputStream iis = ImageIO.createImageInputStream(f); //图片读取器 Iteratorit = ImageIO.getImageReaders(iis); if (it.hasNext()) { ImageReader r = it.next(); //设置输入流 r.setInput(iis, true); System.out.println("格式=" + r.getFormatName()); System.out.println("宽=" + r.getWidth(0)); System.out.println("高=" + r.getHeight(0)); //读取参数 ImageReadParam param = r.getDefaultReadParam(); //创建要截取的矩形范围 Rectangle rect = new Rectangle(100, 100, 400, 200); //设置截取范围参数 param.setSourceRegion(rect); //读取截图数据 BufferedImage bi = r.read(0, param); // 保存图片 ImageIO.write(bi, "jpg", t); } }}