假设您有这样的裁剪图像的功能:
public Image crop(Image src, int topLeftX, int topLeftY, int bottomRightX, int bottomRightY);
此示例代码将帮助您将图片拆分为 R 行和 C 列:
public List<Image> split(Image src, int r, int c) {
List<Image> result = new ArrayList<>();
int topLeftX, topLeftY, bottomRightX, bottomRightY;
int res_height = src.height / r;
int res_width = src.width / c;
for (int i = 0; i < r; i++) {
topLeftX = i * res_height;
bottomRightX = (i + 1) * res_height;
for (int j = 0; j < c; j++) {
topLeftY = j * res_width;
bottomRightY = (j + 1) * res_width;
result.add(crop(src, topLeftX, topLeftY, bottomRightX, bottomRightY));
}
}
return result;
}