private static final String SRC_FILE="org//zy//demo//jdk//base//image//car1.jpg";
// 目标图片
private static final String DEST_FILE="c://a.jpg";
/**
* 读取图像文件
* 用 ImageReader
* @param imgPath
* @throws IOException
*/
public void readUsingImageReader(String imgPath) throws IOException{
// 取得图片读入器
Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
System.out.println(readers);
ImageReader reader = (ImageReader)readers.next();
System.out.println(reader);
// 取得图片读入流
InputStream source=this.parseImagePath(ImageDemo.SRC_FILE);
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);
// 图片参数
ImageReadParam param = reader.getDefaultReadParam();
int imageIndex = 0;
int half_width = reader.getWidth(imageIndex)/2;
int half_height = reader.getHeight(imageIndex)/2;
// Rectangle rect = new Rectangle(60, 60, half_width, half_height);
Rectangle rect = new Rectangle(60, 60, 100, 100);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0,param);
ImageIO.write(bi, "jpg", this.initDestFile());
}
我的源图片是:
上面的程序运行后截得的图片如下:
转: http://www.blogjava.net/zhyiwww/archive/2007/01/30/96767.html