62,614
社区成员
发帖
与我相关
我的任务
分享
/**
* 把图片平均分割成rows行,cols列的小图片
*
* @param img
* @param rows
* @param cols
* @return
*/
public static Image[] splitImage(Image img, int rows, int cols) {
if (img == null)
return null;
Image[] result = new Image[rows * cols];
int w = img.getWidth() / cols;
int h = img.getHeight() / rows;
for (int i = 0; i < result.length; i++) {
result[i] = Image.createImage(w, h);
Graphics g = result[i].getGraphics();
g.translate((-i % cols) * w, (-i / cols) * h);
g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP);
}
return result;
}