【发布时间】:2015-03-27 13:28:49
【问题描述】:
我正在尝试将缓冲图像转换为 ByteBuffer,但我得到了这个异常
java.awt.image.DataBufferInt cannot be cast to java.awt.image.DataBufferByte
谁能帮助我并提出一个好的转换方法。
来源:
public static ByteBuffer convertImageData(BufferedImage bi)
{
byte[] pixelData = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
// return ByteBuffer.wrap(pixelData);
ByteBuffer buf = ByteBuffer.allocateDirect(pixelData.length);
buf.order(ByteOrder.nativeOrder());
buf.put(pixelData);
buf.flip();
return buf;
}
这是我的目标
ByteBuffer buf = convertImageData(image);
【问题讨论】:
-
当你创建一个缓冲图像时,你可以给它一个常数来指示图像的色彩空间。看起来您正在混合 int 和 byte 类型的图像。例如,docs.oracle.com/javase/7/docs/api/java/awt/image/…。您也可以使用docs.oracle.com/javase/7/docs/api/java/awt/image/… 来查找现有图像的类型
标签: java image-processing bufferedimage bytebuffer