开发者

How to access pixels of a Bitmap image on Android?

开发者 https://www.devze.com 2023-01-26 06:10 出处:网络
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

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?


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).


Got an answer from Albert Pucciani on the Android forums. I now create an int buffer and copy the pixels to it, then use get() and put() to extract the pixels. It's also much quicker to use get() and put() instead of the get/setPixel() from the Bitmap 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消