import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
public class ImageUtils {
/**
* 裁切图片
*
* @param imgFile
* @param x
* @param y
* @param width
* @param height
* @return
* @throws Exception
*/
public static BufferedImage cutAreaImage(File imgFile, int x, int y, int width, int height) throws Exception {
String resultFile = "cut/"+imgFile.getName().split("\\.")[0]+"_"+x+"_"+y+".jpg";
BufferedImage image = ImageIO.read(imgFile);
image = image.getSubimage(x, y, width, height);
ImageIO.write(image, "jpg", new File(resultFile));
return image;
}
/**
* 缩放图片
*
* @param imgFile
* @param width
* @param height
* @return
* @throws Exception
*/
public static File zoomImage(File imgFile, int width, int height) throws Exception{
BufferedImage imageBuf = ImageIO.read(imgFile);
BufferedImage zoomImg = new BufferedImage(width, height, imageBuf.getType());
Image img = imageBuf.getScaledInstance(width, height, Image.SCALE_SMOOTH);
Graphics gc = zoomImg.getGraphics();
gc.setColor(Color.WHITE);
gc.drawImage(img, 0, 0, null);
ImageIO.write(zoomImg, "jpg", imgFile);
return imgFile;
}
}