使用字节访问位图内存信息
(我上周在 SO 上发表了三篇关于我正在从事的 Java 项目的文章。我感到内疚,但到底是什么,你的答案太棒了。)
这是一段 C# 代码:
Bitmap bitmap = ...
int dstStride = bitmap.Stride;
byte* bdst = (byte*)bitmap.Scan0;
我想要Java 中的等效算法。基于其他类似问题。
我实际上可以复制位图的步幅信息,但是当然,该 byte*
几乎不可能复制。后来发生的事情是,有一个 for
循环来操作位图图像,la:
bdst[x * 3 + y * dstStride + 2] = (byte)(alpha * bsrc[dx * 3 + L * srcStride + 2]);
(x & y 是循环中的迭代器)
当然,我无法简单地使 bdst 成为字节数组,因为那没有意义。 根据这篇非常棒的文章,Scan0是“固定数据数组的内存地址”。
从上面的 SO 帖子来看,这在 Java 中是不可能的。确认/否认?
(I've made three posts in the last week on SO regarding a Java project I'm working on. I feel guilty, but what the hell, your answers are amazing.)
Here's a chunk of code in C#:
Bitmap bitmap = ...
int dstStride = bitmap.Stride;
byte* bdst = (byte*)bitmap.Scan0;
I want to make an equivalent algorithm in Java. I'm beginning to think this is impossible, based on other, similar questions.
I can actually replicate the stride info of my bitmap, but of course, that byte*
is nigh impossible to reproduce. What happens later is that there's a for
loop that manipulates the bitmap image, a la:
bdst[x * 3 + y * dstStride + 2] = (byte)(alpha * bsrc[dx * 3 + L * srcStride + 2]);
(x & y are iterators in a loop)
Naturally I'm unable to simply make bdst a byte array because that doesn't make sense. According to this totally awesome article, Scan0 is "[t]he address in memory of the fixed data array."
And judging by the above SO post, this is not possible in Java. Confirm / deny?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Java 中您无法获得指向内存的直接指针。这显然是设计使然。
但是,您可以使用 PixelGrabber 类。或者,如果您有 BufferedImage ,您可以使用 Raster直接地。这可以帮助您在不直接访问内存的情况下实现目标。
You cannot get a direct pointer to memory in Java. This is obviously by design.
However you can get an array of pixels from an Image using the PixelGrabber class. Or, if you have a BufferedImage, you can work with the Raster directly. This may help you achieve your goal without accessing memory directly.