java databufferbyte_DataBufferByte无法强制转换为DataBufferInt?

你可能做错了。 : - )

BufferedImage image = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);

...应创建一个带有IntegerInterleavedRaster和DataBufferInt的图像(它在OS X上为我使用我使用的所有已安装的Java 6和7 JRE)。尝试在创建后立即打印栅格和数据缓冲区。我相信它会打印出您最初的预期。

下面的推测性答案:

您尚未发布代码的相关部分,但我会根据您的评论猜测a)创建空图像并将其分配给image,然后b)使用ImageIO读取其他图像并将结果分配给image,替换之前的image值。该图像现在具有byte类型的栅格和数据缓冲区。然后你尝试转换为int类型,然后繁荣。

现在,可以将ImageIO读入预先创建的BufferedImage,或者只指定类型。但这是插件和源特定的(即:不能保证它将支持TYPE_INT_RGB)。要查询读者支持的图像布局,请执行以下操作:

ImageInputStream stream = ...;

Iterator readers = ImageIO.getImageReaders(stream);

if (readers.hasNext()) {

ImageReader reader = readers.next();

try {

reader.setInput(stream);

ImageReadParam param = reader.getDefaultReadParam();

Iterator specs = reader.getImageTypes();

while (specs.hasNext()) {

ImageTypeSpecifier spec = specs.next();

if (/*spec is for TYPE_INT_RGB*/) {

// Either pass your pre-allocated image:

reader.setDestination(image);

// *OR* simply tell the reader what type you like the result to be:

reader.setDestinationType(spec);

}

}

// TODO: Handle the case where there's no match among the ImageTypeSpecifiers

// In case image was passed, it will be the same image returned here

image = reader.read(0, param);

}

finally {

reader.dispose();

}

}

Casting会做它一直以来所做的事情,但我不认为这是你的问题。 ; - )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值