Java等比例缩小图片最短边到size尺寸,然后再截取正中间正方形区域
public File squareThumbAndCutMaker(MultipartFile mFile,int size){
File thumbnailFileNm = null;
File originalFileNm = null;
try {
originalFileNm = multipartToFile(mFile);
thumbnailFileNm = new File(System.getProperty("catalina.home") + File.separator + "temp" + File.separator + getUUID().substring(0,3) + "_" + mFile.getOriginalFilename());
thumbnailFileNm.createNewFile();
BufferedImage originalImg = ImageIO.read(originalFileNm);
int width = originalImg.getWidth();
int height = originalImg.getHeight();
BufferedImage thumbnailImg = new BufferedImage(
size,size,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = thumbnailImg.createGraphics();
g.drawImage(originalImg,
0, 0,
size,
size,
width < height ? 0 : (width - height)/2,
height < width ? 0 : (height - width)/2,
width < height ? width : height + (width - height)/2,
height < width ? height : width + (height - width)/2,
null);
ImageIO.write(thumbnailImg, "png", thumbnailFileNm);
} catch (Exception e) {
logger.error("[make thumb error]",e);
e.printStackTrace();
} finally {
originalFileNm.delete();
}
return thumbnailFileNm;
}