将字节数组转换为BufferedImage会出现错误:RasterFormatException:数据数组太小(应该是> 2764799)

时间:2018-06-16 22:21:01

标签: java opencv

我目前正在尝试使用以下函数转换字节数组

private BufferedImage getBuffImage(byte[] latestBytes) {        
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
    img.setData(Raster.createRaster(img.getSampleModel(), new DataBufferByte(latestBytes, latestBytes.length), new Point() ) );
    return img;
}

其中height和width是BufferedImage的图像高度和宽度,它被转换为字节数组并传递给函数。变为字节数组的BufferedImage来自另一个将mp4视频解析为这些图像的函数,最终目标是在JFrame中将BufferedImages转换回上述函数中的BufferedImages。但是,当我运行应用程序时,我收到以下错误:

    Exception in thread "AbstractApp:ImageChipWriter" java.awt.image.RasterFormatException: Data array too small (should be > 2764799 )
at sun.awt.image.ByteComponentRaster.verify(Unknown Source)
at sun.awt.image.ByteComponentRaster.<init>(Unknown Source)
at sun.awt.image.ByteInterleavedRaster.<init>(Unknown Source)
at sun.awt.image.ByteInterleavedRaster.<init>(Unknown Source)
at java.awt.image.Raster.createRaster(Unknown Source)
at ImageChipWriter.getBuffImage(ImageChipWriter.java:199)
at ImageChipWriter.processMessages(ImageChipWriter.java:187)
at com.ng.mpcore.appapi.util.AbstractApp$5.run(AbstractApp.java:151)
at java.lang.Thread.run(Unknown Source)

第一行中的数字2764799比BufferedImage的height * width * 3小1,其中高度和宽度为720 and 1280。但我不知道如何解决这个问题。

0 个答案:

没有答案