如何在 Android 上访问位图图像的像素?
简而言之,我无法访问位图图像的所有像素。
我使用意图来触发本机相机应用程序并将位图图像返回到我的应用程序活动。数据绝对是一个位图对象,我能够显示、获取高度/宽度等并使用 getPixel() 访问一些像素。但是,当我使用 getHeight() 和 getWidth() 的值时,出现数组越界错误。通过反复试验,我发现我只能访问图像的少量像素,例如,对于返回高度和宽度值为 420,380 的一张图像,我还可以访问 200,100。然后我进行一些图像处理并在原始图像上使用 setPixel() 。当我显示图像时,它显示 200,100 个正在处理的像素,其余部分正常,因此像素显然在那里并且可以由 android 访问,但不能由我访问。我必须与其他也遇到此图像问题的人交谈。
有谁知道更多关于这件事的信息,原因吗?或者解决方法?
非常感谢。
似乎没有办法解决这个问题,有人认为使用 NDK 直接在内存中访问图像会更好/可能吗?
In short I am unable to access all the pixels of a bitmap image.
I have used an intent to fire the native Camera app and returned a Bitmap image to my application activity. The data is definitely a bitmap object and I am able to display, get the height/width etc and access some pixels using getPixel(). However when I use the values of getHeight() and getWidth() I get an array out of bounds error. By trail and error I have found I can only access a reduced number of pixels of the image, for example with one image which returned a height and width value of 420,380, I could also access 200,100. I then do some image processing and used setPixel() on the original image. When I display the image it shows the, say 200,100, processing pixels and the rest normal, therefore the pixels are obviously there and accessible by android but not by me. I have to spoken to other people who have also had this problem with images.
Does anyone know anything more about this, reasons? or a work around?
Many thanks in advance.
It seems that there's no way around this, does anyone think it would be better/possible to access the image directly in memory maybe using the NDK?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将无法访问任何图像中 (getWidth(),getHeight()) 处的像素,因为与其他所有内容一样,它们都是 0 索引的。像素的有效范围是(0到getWidth()-1, 0到getHeight()-1),因此最右下角的像素是通过b.getPixel(b.getWidth()-1, b.getHeight()-获得的1)。
You won't be able to access the pixel at (getWidth(),getHeight()) in any image because like everything else they are 0-indexed. The valid range of pixels is (0 to getWidth()-1, 0 to getHeight()-1), and thus the bottomrightmost pixel is obtained by b.getPixel(b.getWidth()-1, b.getHeight()-1).
在 Android 论坛上得到了 Albert Pucciani 的回答。我现在创建一个
int 缓冲区
并将像素复制到其中,然后使用get()
和put()
提取像素。使用get()
和put()
而不是Bitmap
中的get/setPixel()
也更快代码>类。现在需要测试这是否确实将所有图像的所有像素返回到缓冲区。经过更多测试后,我发现这只是一个内存问题,因为为每个进程分配的数量包括所有位图。
Got an answer from Albert Pucciani on the Android forums. I now create an
int buffer
and copy the pixels to it, then useget()
andput()
to extract the pixels. It's also much quicker to useget()
andput()
instead of theget/setPixel()
from theBitmap
class. Need to test now whether this does return all the pixels to the buffer for all images.After more testing I have discovered this is simply a memory issue as the amount allocated for each process includes all bitmaps.