1

我正在尝试从像素数据创建一个BufferedImage 。以下是我的示例代码。它适用于 TYPE_BYTE_GRAY 但会抛出java.lang.ArrayIndexOutOfBoundsException的 TYPE_INT_RGB

BufferedImage originalImage=<received from the caller>;
Raster raster = originalImage.getData();
SampleModel sampleModel = raster.getSampleModel();

WritableRaster writableRaster= Raster.createWritableRaster(sampleModel, new  Point(0,0));
/*
 * Write data to the writableRaster
 */
int imageType;

if(isColordImage)
    imageType = BufferedImage.TYPE_INT_RGB;
else
    imageType = BufferedImage.TYPE_BYTE_GRAY;

BufferedImage newImage = new BufferedImage(imageWidth, imageHeight, imageType);
newImage.setData(writableRaster);

我哪里错了?提前致谢。

4

0 回答 0