Java异常“线程中的异常”主要" java.lang.ClassCastException:[B不能转换为[I“]调用java.awt.image.BufferedImage.copyData()时 [英] Java exception "Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to [I" when calling java.awt.image.BufferedImage.copyData()

查看:375
本文介绍了Java异常“线程中的异常”主要" java.lang.ClassCastException:[B不能转换为[I“]调用java.awt.image.BufferedImage.copyData()时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我试图将1024 * 1024 png组合成几个更大的png。代码失败并出现此异常:

In the following code, I'm trying to combine some 1024*1024 png into several larger pngs. The code fails with this exception:

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)

这可能是我忽略的小而愚蠢的东西,但我可以找不到代码有什么问题。
代码:

It's probably something small and silly which I overlooked, but I can't find anything wrong with the code. Code:

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类型是什么?)
我还有一些问题,但这个错误是固定的。
(更多问题,如。)

I think I have this figured out by now. The images I was loading were not the same type as the image I created. (I'm still not sure what type they are, what is the 13 type?) I have some more problems, but this error is fixed. (More problems, as in this.)

推荐答案

该库将一个字节数组转换为一个int数组,你不能这样做。

The library casts a byte array to an int array, which you cannot do.

我是不熟悉BufferedImage,但合格的猜测是你读入的PNG文件被视为字节值而不是整数值。

I am unfamiliar with BufferedImage but a qualified guess would be that the PNG file you read in, is treated as byte values instead of integer values.

这篇关于Java异常“线程中的异常”主要&quot; java.lang.ClassCastException:[B不能转换为[I“]调用java.awt.image.BufferedImage.copyData()时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆