Bitmap creation and composition in native code

4,007 views
Skip to first unread message

Lukas Adamec

unread,
Aug 17, 2010, 5:43:00 AM8/17/10
to android-ndk
Hi,
I'd like to ask, if somebody could give me an advise or explanation to
some questions about bitmaps.
1) how can I create bitmap in native code and this bitmap pass to Java
where it will be drawn?
2) how can I passed to Java composition of many smaller bitmap as one
complete bitmap?

Are there other solutions than defining bitmap in Java using
Bitmap.createBitmap(...), passing it to native where I can manipulate
with each pixel and modified bitmap pass back to Java to redraw?

3) What to do, when I don't know bitmap's width and height in Java and
dimensions will be defined in native code?

Thanks a lot.

Onur Cinar

unread,
Aug 17, 2010, 12:34:15 PM8/17/10
to andro...@googlegroups.com

Hi Lukas,

The android-8 NDK has a new header file, bitmap.h, for dealing with the bitmaps from the native space. You will still need to create the bitmap in Java, then the new API will let you modify it directly in the native space.

If you need to create the bitmap in the native space, there is no specific API for that. However, you can still use JNI calls to invoke the Bitmap.createBitmap() from the native space.

The NDK has an example project called bitmap-plasma for demonstrating the new API.

Regards,

-onur





--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

---
www.zdo.com

Lukas Adamec

unread,
Aug 18, 2010, 1:09:21 AM8/18/10
to android-ndk
Thanks, I'm quite familiar with bitmap.h. Could you please explain me,
how I can call Bitmap.createBitmap(...) from native code over JNI?
Could you give me some sample code? I'm begginer in Android
development.
Thanks a lot.
> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > .
Message has been deleted

Lukas Adamec

unread,
Aug 18, 2010, 2:27:13 AM8/18/10
to android-ndk
Thanks, I'm quite familiar with bitmap.h. Could you please explain me,
how I can call Bitmap.createBitmap(...) from native code over JNI?
Could you give me some sample code? I'm begginer in Android
development.
Thanks a lot.

On 17 srp, 18:34, Onur Cinar <onur.ci...@gmail.com> wrote:
> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > .

Lukas Adamec

unread,
Aug 18, 2010, 6:59:03 AM8/18/10
to android-ndk
And I have another question: How I can call invalidate() from native
code?
Thanks.

Onur Cinar

unread,
Aug 18, 2010, 2:34:31 PM8/18/10
to andro...@googlegroups.com

Hi Lukas,

I'll try to e-mail some example code tonight. But briefly here are the calls you will need to make:

First the color space:

jclass bitmapConfig = jni->FindClass("android/graphics/Bitmap$Config");
jfieldID rgb565FieldID = jni->GetStaticFieldID(bitmapConfig, "RGB_565",
    "Landroid/graphics/Bitmap$Config;");

jobject rgb565Obj = jni->GetStaticObjectField(bitmapConfig, rgb565FieldID);

Then the bitmap:

jclass bitmapClass = jni->FindClass("android/graphics/Bitmap");
jmethodID createBitmapMethodID = jni->GetStaticMethodID(bitmapClass,"createBitmap",
    "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");


jobject bitmapObj = jni->CallStaticObjectMethod(bitmapClass, createBitmapMethodID,
    width, height, rgb565Obj);


After this you can modify the pixels, and then you can pass it to Java.

Best regards,

-onur

To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


---
www.zdo.com

Lukas Adamec

unread,
Aug 24, 2010, 3:53:23 AM8/24/10
to android-ndk
Thanks, it's useful.
I have one other question: How can I call invalidate() or
invalidate(l,t,r,b) method from native code when I request redrawing
bitmap from native code? Or is there any other method to do that?

Lukas

On 18 srp, 20:34, Onur Cinar <onur.ci...@gmail.com> wrote:
> Hi Lukas,
>
> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>

mic...@gmail.com

unread,
Aug 27, 2010, 5:31:56 AM8/27/10
to andro...@googlegroups.com
yes you can call it by jni. then onDraw will be called later by system.

> To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

Onur Cinar

unread,
Aug 27, 2010, 12:15:49 PM8/27/10
to andro...@googlegroups.com

Hi Lukas,

I usually rely on the SurfaceView for drawing from the native thread.

Regards,

-onur

To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

Lukas Adamec

unread,
Aug 29, 2010, 12:43:29 AM8/29/10
to android-ndk
It sounds good. Could you give me some sample code, please?

Thanks a lot.

On 27 srp, 18:15, Onur Cinar <onur.ci...@gmail.com> wrote:
> Hi Lukas,
>
> I usually rely on the SurfaceView for drawing from the native thread.
>
> Regards,
>
> -onur
>
> > > > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>
> > <android-ndk%252Buns...@googlegroups.com<android-ndk%25252Bun...@googlegroups.com>
>
> > > > > > .
> > > > > > For more options, visit this group at
> > > > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > > > > ---
>
> > > > >www.zdo.com
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "android-ndk" group.
> > > > To post to this group, send email to andro...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>
>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/android-ndk?hl=en.
>

Onur Cinar

unread,
Aug 30, 2010, 12:49:42 PM8/30/10
to andro...@googlegroups.com

Hi Lukas,

SurfaceView is a view component that can be modified from any thread, not limited to UI thread.  In the native code, after I populate the bitmap, I pass it to the Java code through JNI where I call Surface.lockCanvas() to get the Canvas to paint on, then as soon as I unlock it it will get displayed.

Here are the things that needs to happen:

1. Add a SurfaceView to the UI

(http://developer.android.com/reference/android/view/SurfaceView.html)

2. On onCreate, subscribe to receive Surface state notifications by doing:
    surfaceView.getHolder().addCallback( ... )

(http://developer.android.com/reference/android/view/SurfaceHolder.html)

3. SurfaceHolder.callback will let the application know when the Surface is available, and its size.
4. To do the painting, get the Surface from SurfaceHolder, through surfaceHolder.getSurface()
5. Get the Canvas from Surface through  surface.lockCanvas()
6. Do the painting of the bitmap.
7. Release the canvas through surface.unlockCanvas()

With the private API, it is also possible to access the Surface from the native code, which allows you to do memcpy operations, but I would still rely on the Java API since the other one is frequently changing.

Regards,

-onur


To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


---
www.zdo.com

Lukas Adamec

unread,
Aug 31, 2010, 1:37:52 AM8/31/10
to android-ndk
Thanks. But, I need perform drawing operations with pixels in native
code. Is there some possibility to do periodically updates of my
bitmap from native code? I'd like to use separate thread for rendering
operations and periodically redraw bitmap on the screen and I don't
want to use any private API or OpenGL. Is there some solution to do
it?

On 30 srp, 18:49, Onur Cinar <onur.ci...@gmail.com> wrote:
> Hi Lukas,
>
> > > > > > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>
> > <android-ndk%252Buns...@googlegroups.com<android-ndk%25252Bun...@googlegroups.com>
>
> > > > <android-ndk%252Buns...@googlegroups.com<android-ndk%25252Bun...@googlegroups.com>
> > <android-ndk%25252Bun...@googlegroups.com<android-ndk%2525252Bu...@googlegroups.com>
>
> > > > > > > > .
> > > > > > > > For more options, visit this group at
> > > > > > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > > > > > > ---
>
> > > > > > >www.zdo.com
>
> > > > > > --
> > > > > > You received this message because you are subscribed to the Google
> > > > Groups
> > > > > > "android-ndk" group.
> > > > > > To post to this group, send email to andro...@googlegroups.com.
> > > > > > To unsubscribe from this group, send email to
> > > > > > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>
>
> > > > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>
> > <android-ndk%252Buns...@googlegroups.com<android-ndk%25252Bun...@googlegroups.com>
>
> > > > > > .
> > > > > > For more options, visit this group at
> > > > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > > > ---www.zdo.com
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "android-ndk" group.
> > > > To post to this group, send email to andro...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>
>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-ndk?hl=en.
>
> ---www.zdo.com

Lukas Adamec

unread,
Aug 31, 2010, 9:46:29 AM8/31/10
to android-ndk
I've just tried to use perform rendering in native code in separate
thread and periodically do callbacks from native thread to Java to
call postInvalidate(). But I know, postInvalidate is asynchronous,
which is not very good. Is there some synchronous method with respect
to requests for update comes from native code?
Reply all
Reply to author
Forward
0 new messages