Discussion: How to call Java class in NDK

Source: Internet
Author: User
Tags drawtext

If you don't talk much about it, just upload the code ....... Copy codeThe Code is as follows: package com. clouddevelop. cloudbox;
Import android. graphics. Bitmap;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Paint. FontMetrics;
Public class TextManager
{
Public Bitmap create (String text, float size)
{
Try
{
Paint paint = new Paint ();
Paint. setColor (Color. WHITE );
Paint. setTextSize (size );
Paint. setAlpha (255 );
Paint. setFlags (Paint. ANTI_ALIAS_FLAG );
Paint. setTextAlign (Paint. Align. LEFT );
Paint. setAntiAlias (true );
Float [] widths = new float [text. length ()];
Paint. getTextWidths (text, widths );
Float width = 0;
For (int I = 0; I <widths. length; I ++)
Width + = widths [I];
FontMetrics fm = paint. getFontMetrics ();
Int mFontHeight = (int) (Math. ceil (fm. descent-fm. top) + 2 );
Bitmap textImg = Bitmap. createBitmap (int) width, mFontHeight, Bitmap. Config. ARGB_8888 );
Canvas c = new Canvas (textImg );
If (fm. ascent <0)
C. drawText (text, 0, (float) Math. abs (fm. ascent), paint );
Else
C. drawText (text, 0, fm. ascent *-1, paint );

Return textImg;
}
Catch (Exception e ){}
Return null;
}
Public int getWidth (Bitmap bmp) {return bmp. getWidth ();}
Public int getHeight (Bitmap bmp) {return bmp. getHeight ();}
Public void getPixels (Bitmap bmp, int [] pixels)
{
Int w = bmp. getWidth ();
Int h = bmp. getHeight ();
Bmp. getPixels (pixels, 0, w, 0, 0, w, h );
}
Public void close (Bitmap bmp)
{
Bmp. recycle ();
}
}

To call Java classes in NDK, the first step is to have a Java class. This class is self-built by me.
To generate a Bitmap of a text, there is no technical complexity. You can create a Paint, a Bitmap, and then write the text into the Canvas.
In drawText of the Canvas, the FontMetrics value is used to write the text. Therefore, the fm. ascent is used to align the text up.Copy codeThe Code is as follows: // declare
JNIEXPORT void JNICALL Java_com_clouddevelop_cloudbox_CloudRenderer_nativeTextInit
(JNIEnv * env, jclass cls, jobject textManager );
// Implement
JNIEXPORT void JNICALL Java_com_clouddevelop_cloudbox_CloudRenderer_nativeTextInit
(JNIEnv * env, jclass cls, jobject textManager)
{
G_env = env;
G_textmgr = textManager;
Jclass business_class = env-> GetObjectClass (g_textmgr );
AndroidLog ("initial textmanager success! ");
}

Next, we need to save JNIEnv to the global variable g_env in JNI.Copy codeThe Code is as follows: jobject getInstance (JNIEnv * env, jclass obj_class)
{
JmethodID construction_id = env-> GetMethodID (obj_class, "<init>", "() V ");
Jobject obj = env-> NewObject (obj_class, construction_id );
Return obj;
}
GLuint createText (const char * text, float size, float * rWidth, float * rHeight)
{
If (g_env)
AndroidLog ("g_env exist ");
If (g_textmgr)
AndroidLog ("g_textmgr exist ");
Jclass order_class = g_env-> FindClass ("com/clouddevelop/cloudbox/TextManager ");
AndroidLog ("FindClass succeed ");
G_textmgr = getInstance (g_env, order_class );
Jclass cls = g_env-> GetObjectClass (g_textmgr );
AndroidLog ("get class succeed ");
JmethodID mid;
Mid = g_env-> GetMethodID (cls, "create ",
"(Ljava/lang/String; F) Landroid/graphics/Bitmap ;");
AndroidLog ("get create succeed ");
Jstring data = g_env-> NewStringUTF (text );
Jobject textImage = g_env-> CallObjectMethod (g_textmgr, mid, data, size );
AndroidLog ("call create succeed ");
G_env-> DeleteLocalRef (data );
G_env-> NewGlobalRef (textImage );
/* Get image dimensions */
Mid = g_env-> GetMethodID (cls, "getWidth", "(Landroid/graphics/Bitmap;) I ");
Int width = g_env-> CallIntMethod (g_textmgr, mid, textImage );
AndroidLog ("call getWidth succeed ");
Mid = g_env-> GetMethodID (cls, "getHeight", "(Landroid/graphics/Bitmap;) I ");
Int height = g_env-> CallIntMethod (g_textmgr, mid, textImage );
AndroidLog ("call getHeight succeed ");
* RWidth = width;
* RHeight = height;
/* Get pixels */
JintArray image_data = g_env-> NewIntArray (width * height );
G_env-> NewGlobalRef (image_data );
Mid = g_env-> GetMethodID (cls, "getPixels", "(Landroid/graphics/Bitmap; [I) V ");
G_env-> CallVoidMethod (g_textmgr, mid, textImage, image_data );
AndroidLog ("call getPixels succeed ");
Jint * pixels = g_env-> GetIntArrayElements (image_data, 0 );
// Now generate the OpenGL texture object
GLuint texture;
GlGenTextures (1, & texture );
GlBindTexture (GL_TEXTURE_2D, texture );
GlTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, (GLvoid *) pixels );
GlTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
AndroidLog ("generate texture succeed ");
G_env-> ReleaseIntArrayElements (image_data, pixels, 0 );
G_env-> DeleteGlobalRef (image_data );
/* Free image */
Mid = g_env-> GetMethodID (cls, "close", "(Landroid/graphics/Bitmap;) V ");
G_env-> CallVoidMethod (g_textmgr, mid, textImage );
AndroidLog ("call close succeed ");
G_env-> DeleteGlobalRef (textImage );
Return texture;
}

Copy codeThe Code is as follows: jobject getInstance (JNIEnv * env, jclass obj_class)
{
JmethodID construction_id = env-> GetMethodID (obj_class, "<init>", "() V ");
Jobject obj = env-> NewObject (obj_class, construction_id );
Return obj;
}

The above code creates a Java entity class in native code.
This code is the code used to create text textures in my CloudBox.
Mid = g_env-> GetMethodID (cls, "getWidth", "(Landroid/graphics/Bitmap;) I ");
Int width = g_env-> CallIntMethod (g_textmgr, mid, textImage );
In the two rows, GetMethodID first obtains the method of this class
In GetMethodID, the first parameter is a Java class object. The second parameter is the parameter (or method name), and the third parameter is the signature of the parameter (or method.
How can we obtain the signature of a method?
We need to use the Javap-s TextManager command to do this.

First, go to the. class folder. In my example, go to D: \ CloudAndroid \ CloudBox \ CloudBoxAndroidGameApplication \ bin \ com \ clouddevelop \ cloudbox
Then, type javap-s TextManager and you will get it. Signature is the Signature we want.

After so long, my CloudBox can finally display text !!!!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.