除了根据网络上的一些代码实现了图片的裁剪功能,初次之外也在作者的基础上进一步更加简洁地实现图片的裁剪的功能。
/**
* 裁剪图片
* @param srcImageString
* @param detImageString
* @param x
* @param y
* @param w
* @param h
* @throws Exception
*/
public void imageCut(String srcImageString, String detImageString, int x, int y, int w, int h) throws Exception{
File imagePutFile = new File(srcImageString);
if (!imagePutFile.exists()) {
throw new Exception("imagefile is not exited!");
}
//图片流读入缓存
ImageInputStream llStream = ImageIO.createImageInputStream(new FileInputStream(srcImageString));
Iterator<ImageReader> iterator =ImageIO.getImageReaders(llStream);
ImageReader iamgeradeReader = (ImageReader) iterator.next();
iamgeradeReader.setInput(llStream);
ImageReadParam param = iamgeradeReader.getDefaultReadParam();
param.setSourceRegion(new Rectangle(x, y, w, h));
BufferedImage bImage = iamgeradeReader.read(0, param);
ImageIO.write(bImage, "png", new File(detImageString));
}
上述代码地址为:https://blog.youkuaiyun.com/qq_40162735/article/details/88029484
以下为自己的实现代码:
public void testImage(String imageString, String detImageString) throws Exception{
File file = new File(imageString);
if (!file.exists()) {
throw new Exception("file is not exited!");
}
BufferedImage image = ImageIO.read(new FileInputStream(file));
BufferedImage bImage = image.getSubimage(0, 0, 200, 200);
ImageIO.write(bImage, "png", new File(detImageString));
}