朋友圈神器
先看效果,原图片:
/**
*
* @param sepX 横向分隔个数
* @param sepY 纵向分隔个数
* @param path 文件路径
* @return 分割后文件夹路径
*/
public static String cutPic(Integer sepX, Integer sepY, String path) throws Exception {
File file = new File(path);
if (!file.exists() || !file.isFile()) {
throw new RuntimeException("file not exists or un-file:" + path);
}
BufferedImage image = ImageIO.read(file);
int totalWidth = image.getWidth();
int totalHeight = image.getHeight();
int width = totalWidth / sepX;
int height = totalHeight / sepY;
File dirFile = new File(file.getParent(), file.getName().substring(0, file.getName().lastIndexOf(".")));
if (!dirFile.exists()) {
dirFile.mkdir();
}
for (int y = 0, j = 1; y <= totalHeight - height; y += height, j++) {
for (int x = 0, i = 1; x <= totalWidth - width; x += width, i++) {
File targetFile = new File(dirFile, j + "_" + i + ".jpg");
BufferedImage targetImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = targetImage.getGraphics();
g.drawImage(image.getSubimage(x, y, width, height), 0, 0, null);
ImageIO.write(targetImage, "JPG", targetFile);
}
}
return dirFile.getPath();
}
分割后
代码