今からはじめるプログラミング43
前々回、数百個のアイコンをがっちゃんこするクラスを作ったので、今回は、逆に分割するプログラムをつくります。
正直なところ、そんなに難しくないだろうとたかを括っていたのですが、いろいろ考えすぎて半日使ってしまいましたw
SplitImg
-----------------------------------------
package sample24;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SplitImg {
private BufferedImage resourceImage = null;
private int width = 192;
private int hight = 192;
public void setSize(int w, int h) {
this.width = w;
this.hight = h;
}
public boolean splite(String folder) {
// 画像
try {
resourceImage = ImageIO.read(new File(folder));
int x = 0;
int y = 0;
int idx = 0;
for (int i = 0; i < 20; i++) {
System.out.println("i=" + i);
for (int j = 0; j < 20; j++) {
x = j * width;
y = i * hight;
BufferedImage sub = null;
sub = resourceImage.getSubimage(x, y, width, hight);
//
ImageIO.write(sub, "png", new File("./" + idx + "sub.png"));
idx++;
}
x = 0;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SplitImg sImg = new SplitImg();
sImg.splite("integ.png");
}
}
-----------------------------------------
元画像
分割後
まぁこんな感じ。