你可能做错了。 : - )
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会做它一直以来所做的事情,但我不认为这是你的问题。 ; - )