利用Java代码实现图片切割
程序员文章站
2022-04-13 14:11:26
...
利用Java代码实现图片切割
将一张大图分割成指定数量的小图
private static void splitImage() throws IOException {
//String originalImg = "C:\\img\\split\\a380_1280x1024.jpg";
String originalImg = "F:\\images\\school\\-11ff2b32525fd5f2.jpg";
// 读入大图
File file = new File(originalImg);
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis);
// 分割成4*4(16)个小图
int rows = 4;
int cols = 4;
int chunks = rows * cols;
// 计算每个小图的宽度和高度
int chunkWidth = image.getWidth() / cols;
int chunkHeight = image.getHeight() / rows;
int count = 0;
BufferedImage imgs[] = new BufferedImage[chunks];
for (int x = 0; x < rows; x++) {
for (int y = 0; y < cols; y++) {
//设置小图的大小和类型
imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());
//写入图像内容
Graphics2D gr = imgs[count++].createGraphics();
gr.drawImage(image, 0, 0,
chunkWidth, chunkHeight,
chunkWidth * y, chunkHeight * x,
chunkWidth * y + chunkWidth,
chunkHeight * x + chunkHeight, null);
gr.dispose();
}
}
// 输出小图
for (int i = 0; i < imgs.length; i++) {
//ImageIO.write(imgs[i], "jpg", new File("C:\\img\\split\\img" + i + ".jpg"));
ImageIO.write(imgs[i], "jpg", new File("E:\\temp" + i + ".jpg"));
}
System.out.println("完成分割!");
}