切圖的代碼如下:
package com.fnpac.gotop.helper.image;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
/**
* Created by liuchunlong on 2018/1/13.
* <p>
* 圖片處理工具類
*/
public class ImageUtils {
/**
* 圖片剪裁
*
* @param x 距離左上角的x軸距離
* @param y 距離左上角的y軸距離
* @param width 寬度
* @param height 高度
* @param sourcePath 圖片源
* @param descpath 目標(biāo)位置
*/
public static void imageCut(int x, int y, int width, int height,
String sourcePath, String descpath) {
FileInputStream fis = null;
ImageInputStream iis = null;
try {
String fileSuffix = sourcePath.substring(sourcePath.lastIndexOf(".") + 1);
Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(fileSuffix);
ImageReader reader = it.next();
fis = new FileInputStream(sourcePath);
iis = ImageIO.createImageInputStream(fis);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, fileSuffix, new File(descpath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
fis = null;
}
if (iis != null) {
try {
iis.close();
} catch (IOException e) {
e.printStackTrace();
}
iis = null;
}
}
}
}