Java
如何截取图片
Author
: zhyiwww
E-Mail
: zhyiwww@163.com
Date
: 2007-1-30
转载请注明出处
www.BlogJava.net/zhyiwww
(copyright by @ zhangyi)
下面是我的一段代码,实现如何截取图片的:
//
图片源
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());
}
我的源图片是:
<!--[if !vml]-->
<!--[endif]-->
上面的程序运行后截得的图片如下:
<!--[if !vml]-->
<!--[endif]-->