What is an efficient way (in terms of memory and cpu) to convert an ArrayList<boolean[]> or a boolean[][] into a Bitmap?
Does this way change if we know that the Bitmap must be modified or appended later?
Your best bet is probably to convert your boolean[][] to an int[] and use setPixels. This is especially beneficial if you're going to do many pixel-level modifications later (just re-use the int[] and call setPixels again).
Related
I have a question (hope it is not stupid). Is getDrawable(int resId) slow ?
And is it faster than decoding resource with BitmapFactory ? And is it faster to call getDrawable(resid).getInstrinsicHeight() than decoding with Bitmap.Options with inJustDecodeBounds = true and than get the width ? I know that decoding of bitmaps is slow, and they consume a lot of memory, but what about getDrawable(int redId) and the result Drawable object ? It is allocate a lot of memory.
Thanks in advance.
Your Question is Similar to this one :
Efficient way of creating Bitmap out of Drawable from res (BitmapFactory vs Type Casting)
"You can take a look at the source code for Bitmap factory at http://source.android.com specifically the code for decodeResource.
I would reason that using BitmapFactory is preferred but in either case if you are decoding multiple bitmaps then you should call getResources() once and store the result for use as the resources argument for the functions."
Further, i would recommend you to go through 1st 2 articles here, to learn about the workaround for using either of these effficiently :
http://www.coderzheaven.com/tag/getdrawable/
I know how to do this converting it to a bitmap, but I want an Image (android.media) on the receiver side.
Thanks in advance.
If you want to pass only one bitmap at a time, I suggest creating a static variable in a class. and assign the bitmap object to it, and use it in the receiver class.
But if this is very big bitmap, it may cause OutOfMemory issue.
You probably don't want to do this. Even if you create a Serializable or Parcelable object that includes your data like this:
http://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html
which you may be able to do, by creatively encoding your image.
But an Intent has a limitation in size. See this post here:
Maximum length of Intent putExtra method? (Force close)
It says 1MB, but in my experience it might be as high as 4MB, depending on the platform (I may have that wrong, I don't have a specific reference to Android documentation to support it, but I see errors that appear to support it).
I'm sure you can move a lot of images within this restriction, but for any that fall outside of it, you will need a "work around" - which you should then probably make the "standard" and avoid the issue altogether.
Im my app, i use alot of getResources() like that
BitmapFactory.decodeResource(getResources(), resourceid, opts);
i can use Resources res = getResources() as Global variable and use
BitmapFactory.decodeResource(res, resourceid, opts);
So, my question is: What's the better way to avoid memory leak?
Thanks
I do it this way on occasion which I think can be fine but you just have to watch it. In your situation, I'm not sure that its worth creating a Global variable when nothing is changing. I will do it for a function to get a certain resource depending on context or some other variable. But here, I don't see how
BitmapFactory.decodeResource(getResources(), resourceid, opts);
is any better than
BitmapFactory.decodeResource(res, resourceid, opts);
It seems that you are adding one extra unnecessary step
I wouldn't try to micro optimize this too much, just use common practices about creating less garbage where possible. By this, I mean avoid decoding something, and letting it go out of scope and decoding it again.
Really, unless you keep decoding huge images or something like that, I don't see any problems.
I'm working with Android and I really need a fast way to get a bitmap of format BGRA to be filled in ARGB.
One thing I aslo want to let u know that The Data comes in byte[] format and I have to convert in int[] format also.
Can AnyOne Tell me How to do this ...
Thanks in Advance
If you want to load a Bitmap by bytestream, you can use Bitmap.decodeStream. You could then use getPixel(s?) to get the int array. This is one way I know how to do this, probably not the fastest though. A faster way would be to convert bytes to int, if your byte array is nothing but pixeldata this won't be too hard.
BGRA to ARGB can be done with bitshifting quite fast.
A nice source you would probably like:
https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx
The fastest way I think would be to do it in native code using the NDK. I've been considering to use it for image processing for some time but didn't get the chance yet. So I don't know much about it (i.e. how you would access your byte buffer) but you could start from the Plasma sample for bitmap processing in JNI.
Can someone please explain to me what a mutable bitmap is? What advantages/disadvantages or what limitations do mutable and immutable bitmaps have?
It's about the same difference as with String vs StringBuilder - String is immutable so you can't change its content (well at least not without any hacks), while for StringBuilder you can change its content.
In order to convert an immutable bitmap to a mutable one, check out this post:
https://stackoverflow.com/a/16314940/878126.
If your bitmap include mutable flag, its pixels can be change, if doesn't, pixel changings throw an error. It's the difference between them.
And here is Android: convert Immutable Bitmap into Mutable
Not sure if there is any performance gain for using immutable bitmaps.
Often immutable is just for being thread safe (or if you are going to share the image with another process, process safe)
Tries to make a new bitmap based on the dimensions of this bitmap, setting the new bitmap's config to the one specified, and then copying this bitmap's pixels into the new bitmap.
and you can set config of image.