如何在Java中实现图片裁剪?
下面是我的一段代码,实现如何截取图片的:
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.45fan.com/a/question/71415.html