B4A Library Native DirectBuffer library

Native DirectBuffer allows you to create and work with memory outside of the Java heap, including memory-mapped files. This feature is a great way to work with large amounts of structured data. The example show how to Draw over 50
bitmaps and store them to the buffer and get them back later.
The library use jni "C" code compiled .so files to make the buffer outside of the Java heap.
Update ver. 1.10
the problem with BitmapcopyPixelsFromBuffer solved
couple more function added
Very easy to use, just extract the files from keylabBufferLibrary.zip
to your lib folder and open the attached example
 

Attachments

  • keylabBufferLibrary1.10.zip
    25.9 KB · Views: 274
  • keylabDirectBufferExample.zip
    133.7 KB · Views: 278
Last edited:

Informatix

Expert
Licensed User
Longtime User
... but not working as expected on my Gingerbread device, or it's just a problem with the example. Look at the screenshot.
 

Attachments

  • screen.jpg
    screen.jpg
    86.1 KB · Views: 256

keylab83

Member
Licensed User
Longtime User
... but not working as expected on my Gingerbread device, or it's just a problem with the example. Look at the screenshot.
Yes you are right, I tested it on my Galaxy S (Gingerbread) and the result is the same as your screenshot :( but when I release the library I tested it
on my tabled acer iconia b1 and was perfect :confused:
I tried to mess with the byte order, but was not useful
:sign0163: idea what can be wrong?

the eclipse lines related with copyPixelsFromBitmap and copyPixelsToBitmap
PHP:
   public void copyPixelsFromBitmap(Bitmap bm){bm.copyPixelsToBuffer(bb);}
   public void copyPixelsToBitmap(Bitmap bm){bm.copyPixelsFromBuffer(bb);}
 

keylab83

Member
Licensed User
Longtime User
... but not working as expected on my Gingerbread device, or it's just a problem with the example. Look at the screenshot.

I found the solution and library is updated and uploaded
this is the new java code for copyPixelsToBitmap

B4X:
   public Bitmap copyPixelsToBitmap(int Width,int Height,Config config){
      Bitmap bmT=Bitmap.createBitmap(Width , Height ,config);   
      bmT.copyPixelsFromBuffer(bb);
      return bmT;
   }

I don't think that is very optimized because make new bitmap every time but fix the problem if anyone have better solution I will update it again;

Informatix thank you for pointing me this bug.
 

Informatix

Expert
Licensed User
Longtime User
Great.

I found something else, but not critical. Under Gingerbread, when I display the free memory with my StrictMode library, I get 0 at the end of the process because there's a negative value for android.os.Debug.getNativeHeapAllocatedSize. That means that computing the free memory is wrong with your lib and using it may induce side effects.
Since you skip the garbage collector, don't you have to clear the memory yourself at the end?
And could you document your functions?
 
Last edited:

keylab83

Member
Licensed User
Longtime User
Great.
Since you skip the garbage collector, don't you have to clear the memory yourself at the end?

you can use free_native_buffer when finish with the buffer
example: b4a
B4X:
    bb.free_native_buffer(bb.rewind) '!we use .rewind or some of the others functions that return DirectBuffer for pointer to the DirectBuffer
 

Adam888

Member
Licensed User
Longtime User
Hi

I use this lib to modify bitmap , the code as below
B4X:
Sub SeekBar1_ValueChanged (Value As Int, UserChanged As Boolean)
   
    buff.Setposition(buff.PositionslistGet(Value))'set the position in the buffer
    '----------------------------------
    Dim i As Int
    Dim rr(640*480*4)As Byte
    buff.get2(rr)
    For i = 0 To 640*480*4-1  Step 4
        rr(i)=255
    Next
   
    buff.putByteArray(rr)
    buff.rewind
    '----------------------------------
    ImageView1.Bitmap=buff.copyPixelsToBitmap(bm.Width,bm.Height,buff.Config_ARGB_8888)
    ImageView1.Invalidate
    Log("remaining bytes after  copyPixelsToBitmap"&buff.remaining)
End Sub

I want to do the imageview become red color. But it doesn't work. Could you please help me? THANK YOU.
 

keylab83

Member
Licensed User
Longtime User
Hi! Sorry for the late respond but I was offline for a while.
What error you receive in the log?
 

js1234

Member
Licensed User
Longtime User
I try above example: keylabdirectbufferexample-zip
but get compile error occured on line 29:

Dim bmex As BitmapExtended
"Are you missing library reference?"

p.s.
I'm import library: keylabBufferLibrary1.10 and refresh it!

Where is problem?
 

Adam888

Member
Licensed User
Longtime User
Thank you all.

Sure,I have import library and refresh it.
But the code " rr(i)=255 " still not work. The buff is no any changed.
Is there something I missing?
 

keylab83

Member
Licensed User
Longtime User
Hi
try the code below
or just do buffer.rewind before buff.putByteArray(rr)
I try it and it work, the result is what you want image get more red

B4X:
Sub SeekBar1_ValueChanged (Value As Int, UserChanged As Boolean)

    buff.Setposition(buff.PositionslistGet(Value))'set the position in the buffer
    '----------------------------------
    Dim i As Int
    Dim rr(640*480*4)As Byte
    buff.get2(rr)
    For i = 0 To 640*480*4-1  Step 4
        rr(i)=255
    Next
    buff.rewind             
    buff.putByteArray(rr)
    buff.rewind
    '----------------------------------
    ImageView1.Bitmap=buff.copyPixelsToBitmap(bm.Width,bm.Height,buff.Config_ARGB_8888)
    ImageView1.Invalidate
    Log("remaining bytes after  copyPixelsToBitmap"&buff.remaining)
End Sub

you have to rewind the buffer on position where you want to put the bytes array first

By the way Sub SeekBar1_ValueChanged is not good place to make lots of processing, is good idea to make them just once in button_click or like in my example in Activity_Create or something like this.
 
Last edited:
Top