gpt4 book ai didi

java - DataBuffer 到 DataBufferByte 转换抛出错误

转载 作者:行者123 更新时间:2023-12-01 13:52:40 25 4
gpt4 key购买 nike

我有以下java代码

final byte[] pixels;
DataBuffer rasterData = image.getRaster().getDataBuffer();
DataBufferByte rasterByteData = (DataBufferByte)rasterData;
pixels = rasterByteData.getData();

其中图像是 BufferedImage 类型的对象,我希望将 Image 转换为 DataBufferByte 对象,但我的转换抛出以下错误.

java.lang.ClassCastException: java.awt.image.DataBufferInt cannot be cast to java.awt.image.DataBufferByte

我正在尝试使用这篇文章 Java - get pixel array from image 中的代码

有什么想法如何转换或为什么我无法从 DataBuffer 转换为 DataBufferByte

编辑:将Image更改为BufferedImage

最佳答案

DataBuffer to DataBufferByte cast throws error

看看reference API - 光栅数据可以是任何东西,而不仅仅是字节。所以我认为这就是你得到异常的原因。

尝试用于您的 BufferedImage 属性:

BufferedImage.TYPE_3BYTE_BGR

这应该可以解决这个问题,并且光栅数据应该以字节形式返回。

关于java - DataBuffer 到 DataBufferByte 转换抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19853265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com