java 分割图片
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class cutPicUtils{
public static void main(String[] args) throws IOException {
File file = new File("img1.png");//本地图片位置 --这是相对路径 在本项目根文件下
BufferedImage image = (BufferedImage) ImageIO.read(file);
BufferedImage cut1 = cut(0, 0, 100, 100, image);
ImageIO.write(cut1, "png", new File("cut1.png"));//分割后的图片位置
}
public static BufferedImage cut(int x, int y, int wight, int hight, BufferedImage img) {
int[] simgRgb = new int[wight * hight];
img.getRGB(x, y, wight, hight, simgRgb, 0, wight);
BufferedImage newImage = new BufferedImage(wight, hight, BufferedImage.TYPE_INT_ARGB);
newImage.setRGB(0, 0, wight, hight, simgRgb, 0, wight);
return newImage;
}
}
这是一个对图片进行多次分割的代码
public static void MycutPic(String filePath) throws IOException {
File file = new File(filePath);//本地图片
BufferedImage image = (BufferedImage) ImageIO.read(file);
int count = 0;
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
count++;
int x1 = i * 75;
int y1 = k * 75;
BufferedImage cut1 = cut(x1, y1, 75, 75, image);
File file1 = new File("cut" + count + ".png");
ImageIO.write(cut1, "png", file1);
count++;
if (i == 3 || k == 3) {
continue;
}
int x2 = (i * 75) + 50;
int y2 = (k * 75) + 50;
BufferedImage cut2 = cut(x2, y2, 75, 75, image);
File file2 = new File("cut" + count + ".png");
ImageIO.write(cut2, "png", file2);
count++;
int x3 = (i * 75) + 25;
int y3 = (k * 75) + 25;
BufferedImage cut3 = cut(x3, y3, 75, 75, image);
File file3 = new File("cut" + count + ".png");
ImageIO.write(cut3, "png", file3);
}
}
}
原图找不到了,这是原图做了灰度处理后的图片
这是分割效果