次のコードでは、1024*1024 の png をいくつかの大きな png に結合しようとしています。コードは次の例外で失敗します。
Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to [I
at sun.awt.image.IntegerInterleavedRaster.setDataElements(Unknown Source)
at java.awt.image.BufferedImage.copyData(Unknown Source)
at mloc.bs12.mapimagemerger.Merger.main(Merger.java:27)
それはおそらく私が見落とした小さくてばかげたものですが、コードに問題は見つかりません。コード:
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class Merger {
public static void main(String[] args) {
String toX, toY, toZ;
try {
toX = args[0];
toY = args[1];
toZ = args[2];
} catch(ArrayIndexOutOfBoundsException E) {
//E.printStackTrace();
toX = "3";
toY = "5";
toZ = "4";
}
int yproper = 1;
for(int z = 1; z <= Integer.parseInt(toZ); z++) {
BufferedImage img = new BufferedImage(Integer.parseInt(toX) * 1024, Integer.parseInt(toY) * 1024, BufferedImage.TYPE_INT_RGB);
for(int x = 1; x <= Integer.parseInt(toX); x++) {
for(int y = 1; y <= Integer.parseInt(toY); y++) {
BufferedImage simg = img.getSubimage(x*1024, y*1024, 1024, 1024);
BufferedImage tempimg = loadImage(x + "-" + y + "-" + z + ".png");
WritableRaster rsimg = simg.getRaster();
rsimg = tempimg.copyData(rsimg); <-- Error!
yproper++;
}
}
saveImage(img, z + ".png");
}
}
public static BufferedImage loadImage(String path) {
BufferedImage bimg = null;
try {
bimg = ImageIO.read(new File(path));
} catch (Exception e) {
e.printStackTrace();
}
return bimg;
}
public static void saveImage(BufferedImage img, String path) {
try {
ImageIO.write(img, "png", new File(path));
} catch (Exception e) {
e.printStackTrace();
}
return;
}
}
私はこれでこれを理解したと思います。読み込んでいた画像は、作成した画像と同じタイプではありませんでした。(私はまだそれらが何のタイプかわかりません。13 のタイプは何ですか?) まだいくつか問題がありますが、このエラーは修正されています。(こののように、さらに問題があります。)