Re: Native developpement using NDK / how to write text on screen?

2,351 views
Skip to first unread message

Romain Rodriguez

unread,
Mar 27, 2013, 5:54:57 AM3/27/13
to andro...@googlegroups.com
Dear all,

Maybe I can call some Java function inside my C code ?
That is possible with JNI ?

could I get any example of that ? like calling a println method from my C code and not the invert

thanks

David Turner

unread,
Mar 27, 2013, 6:24:42 AM3/27/13
to andro...@googlegroups.com
I'm not sure what you really want to do but here's some information:

1/ While Android provides extensive graphics API to draw text and other things from Java, it doesn't have any equivalent thing for C/C++.
2/ However, it provides a way to access the pixel buffer of bitmap objects (see <android/bitmap.h> as distributed with the NDK for details).

As such, you have two options:

1/ If you want to do everything in C/C++, use the <android/bitmap.h> to access the pixel buffer of a given bitmap, and modifying the pixels directly there. This of course requires that you have your own graphics / text rendering library in your project. Access to installed font files might be problematic though, I'm not sure there is an official way to access them from native code (I guess using hard-coded paths might work, but this is fragile, i.e. OEMs can easily change the set of fonts installed on their system, as well as the location of said fonts).

2/ Use JNI to call the relevant Java APIs to draw text directly into whatever target Bitmap object you need.

Note that, alternatively, you could just write a little Java. In many cases, it will simply be a lot simpler than doing things in C/C++ anyway.

Hope this helps

On Tue, Mar 26, 2013 at 4:33 PM, Romain Rodriguez <romai...@gmail.com> wrote:
Dear all,

I'm a new android developper, and I need to program a native application, in C language.
I have already build the native-plasma sample, end everything works great.

My need are :
Define some zones on my screen buffer.
Write text in theses zones.

define zones corresponding to my screen should be ok, but how can I write text easely to my screen buffer ?
Is any libraries or something to do that easely?  

Or I have to define all letters in "pixel"  ?

Thanks in advance

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

Gerard Meier

unread,
Mar 27, 2013, 6:30:48 AM3/27/13
to andro...@googlegroups.com
Alternatively one could use Freetype combined with OpenGL to render text via C/C++. We use this successfully in our projects. Freetype, however, is hardly beginners material.

David Turner

unread,
Mar 27, 2013, 6:40:41 AM3/27/13
to andro...@googlegroups.com
On Wed, Mar 27, 2013 at 11:30 AM, Gerard Meier <ger...@gmail.com> wrote:
Alternatively one could use Freetype combined with OpenGL to render text via C/C++. We use this successfully in our projects. Freetype, however, is hardly beginners material.

I think it's not too bad really, even though clearly not perfect :-) Full disclosure: I'm very biased when it comes to this library.

If you want to build it with the NDK, I'd recommend getting the external/freetype directory from the AOSP sources, it comes with an Android.mk that should work directly with ndk-build. I.e.


Note: you will also need code to compose the FreeType character bitmaps into your final bitmaps. This typically involves pixel processing operations (e.g. Porter-Duff) that are typically provided by a small graphics library on top of the font engine.

jeff shanab

unread,
Mar 27, 2013, 7:41:43 AM3/27/13
to andro...@googlegroups.com
My App, a video player uses opengles to draw the pixels to a texture. For me this had the added benefit of color conversion and scaling being accelerated.  The code is 99% c++ but because it is in an activity started by java, I have another activity overlayed on top that uses any and all of the controls including text-boxes to place on top of the image.
perhpas this technique will work for your app to

Romain Rodriguez

unread,
Mar 27, 2013, 8:09:37 AM3/27/13
to andro...@googlegroups.com
Thanks a lot for answers guys,

I don't really know what is the best solution for my current project.

2/ Use JNI to call the relevant Java APIs to draw text directly into whatever target Bitmap object you need.
       I tried to find a way to call Java method from C, infortunately i found only native code called from Java example, and I don't know how to get the JVM in my C (to attach the env and call the good method).
       I think this is the best way for me, to prepare all datas needed to be displayed in native and then I send datas to the Java method. If you can help about how to do that, would be great, :)

just to be clear I have imposition about use C and not directly Java !

For now, i'll try freeType combined with OpenGL, will be hard (newbie I am). Is there some tutos ? :)

Thanks a lot



David Given

unread,
Mar 27, 2013, 8:12:43 AM3/27/13
to andro...@googlegroups.com
David Turner wrote:
[...]
> I think it's not too bad really, even though clearly not perfect :-)

I'm using Sean Barrett's public domain TTF font renderer from here:

http://nothings.org/stb/stb_truetype.h

It's more limited than Freetype, but it's a single source file, has a
very friendly license, is really easy to port, and compiles into about
30kB of code. It works fine when we point it at the Android system
fonts. It supports subpixel antialiasing, too.

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────

│ 𝕻𝖍'𝖓𝖌𝖑𝖚𝖎 𝖒𝖌𝖑𝖜'𝖓𝖆𝖋𝖍 𝕮𝖙𝖍𝖚𝖑𝖍𝖚 𝕽'𝖑𝖞𝖊𝖍
𝖜𝖌𝖆𝖍'𝖓𝖆𝖌𝖑 𝖋𝖍𝖙𝖆𝖌𝖓.


signature.asc

David Turner

unread,
Mar 27, 2013, 8:44:25 AM3/27/13
to andro...@googlegroups.com
On Wed, Mar 27, 2013 at 1:12 PM, David Given <d...@cowlark.com> wrote:
David Turner wrote:
[...]
> I think it's not too bad really, even though clearly not perfect :-)

I'm using Sean Barrett's public domain TTF font renderer from here:

http://nothings.org/stb/stb_truetype.h

It's more limited than Freetype, but it's a single source file, has a
very friendly license, is really easy to port, and compiles into about
30kB of code. It works fine when we point it at the Android system
fonts. It supports subpixel antialiasing, too.

That definitely looks like great too. Thanks for the link.

Romain Rodriguez

unread,
Mar 27, 2013, 9:33:37 AM3/27/13
to andro...@googlegroups.com
great, I just success to use Java Method from my C native code !

So I can use every Method from Java to display / draw text or something, is that right ?

thanks again


2013/3/27 David Turner <di...@android.com>

--
You received this message because you are subscribed to a topic in the Google Groups "android-ndk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/android-ndk/Lqo12rwc6Zo/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to android-ndk...@googlegroups.com.

Raul Carvente

unread,
Mar 27, 2013, 10:08:32 AM3/27/13
to andro...@googlegroups.com
Also you could use OpenCV libraries, they have methods to write text on Canvas screen.

Greets,


2013/3/27 Romain Rodriguez <romai...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
M. en C. Raúl Carvente Hernández
Celular: 044-55-40-05-45-22
Skype: brachialste

Ray Donnelly

unread,
Mar 27, 2013, 4:30:44 PM3/27/13
to andro...@googlegroups.com
You could also use Qt for Android to render some text.

http://blog.qt.digia.com/blog/2013/03/13/preview-of-qt-5-for-android/

Whilst there's many great reasons for using Qt on Android, I feel like
I've just recommended cracking a nut with a sledgehammer.
Reply all
Reply to author
Forward
0 new messages