缓冲区不够大,用于像素

发布于 2025-01-26 23:01:54 字数 761 浏览 5 评论 0原文

我正在尝试从字节数组中获取一个位图

                            val bitmap_tmp =
                                Bitmap.createBitmap(height, width, Bitmap.Config.ARGB_8888)
                            val buffer = ByteBuffer.wrap(decryptedText)
                            bitmap_tmp.copyPixelsFromBuffer(buffer)
                            callback.bitmap(bitmap_tmp)

,我在下行中面临错误:

                            bitmap_tmp.copyPixelsFromBuffer(buffer)

错误读取为:

java.lang.RuntimeException: Buffer not large enough for pixels

我尝试了在堆栈上找到的其他解决方案,例如在错误之前添加行,但仍然崩溃:

buffer.rewind()

但是怪异的部分是同一图像的不同位置的相同代码[具有相同维度的同一图像]获得了完美的功能,我得到了位图,但在这里崩溃了。 我该如何解决? 感谢ADV

I am trying to get a bitmap From byte array

                            val bitmap_tmp =
                                Bitmap.createBitmap(height, width, Bitmap.Config.ARGB_8888)
                            val buffer = ByteBuffer.wrap(decryptedText)
                            bitmap_tmp.copyPixelsFromBuffer(buffer)
                            callback.bitmap(bitmap_tmp)

I am facing a error in the below line :

                            bitmap_tmp.copyPixelsFromBuffer(buffer)

The Error Reads As:

java.lang.RuntimeException: Buffer not large enough for pixels

I have tried Different Solutions found on stack Like Add the line before error but still it crashes:

buffer.rewind()

However the Weird part is the same code at a different place for the same image [Same image with same dimensions] get perfectly functioned and I get the bitmap but here it crashes.
How do I solve this?
Thanks in Adv

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剑心龙吟 2025-02-02 23:01:55

错误消息听起来像您要复制的缓冲区不够大,就像它需要在需要覆盖位图中的每个像素时至少包含至少包含设置大小和像素配置)。

该方法的文档并不清楚,但是这是位图类的来源,在这种方法中:

if (bufferBytes < bitmapBytes) {
    throw new RuntimeException("Buffer not large enough for pixels");
}

是的,您不能部分覆盖位图,您需要足够的数据来填充它。如果您检查源,这取决于缓冲区的当前位置limit(不仅是其容量,还需要读取多少数据)。

如果它在其他地方工作,我猜decryptedText在那里有所不同,或者也许您正在创建bitmap使用不同的bitmap.config(像argb_8888每个像素需要4个字节)

The error message makes it sound like the buffer you're copying from isn't large enough, like it needs to contain at least as many bytes as necessary to overwrite every pixel in your bitmap (which has a set size and pixel config).

The documentation for the method doesn't make it clear, but here's the source for the Bitmap class, and in that method:

if (bufferBytes < bitmapBytes) {
    throw new RuntimeException("Buffer not large enough for pixels");
}

So yeah, you can't partially overwrite the bitmap, you need enough data to fill it. And if you check the source, that depends on the buffer's current position and limit (it's not just its capacity, it's how much data is remaining to be read).

If it works elsewhere, I'm guessing decryptedText is different there, or maybe you're creating your Bitmap with a different Bitmap.Config (like ARGB_8888 requires 4 bytes per pixel)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文