Nonsense not much said, directly on the code ....
Copy Code code 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 of course to have a Java class, which I built myself
To produce a literal bitmap, there is no technical complexity, build paint, build bitmap, and then write the text in canvas
In Canvas's DrawText, the FontMetrics value is used to write text, so use fm.ascent to align text up
Copy Code code 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, you will save jnienv to a global variable in JNI g_env
Copy Code code 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 Code code 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 is to create a Java entity class in native code
This code is the code that creates the text texture in my cloudbox.
MID = G_env->getmethodid (CLS, "GetWidth", "(Landroid/graphics/bitmap;) I");
int width = G_env->callintmethod (G_textmgr, Mid, textimage);
Of these two lines, Getmethodid first obtains the method of the class
The first parameter in Getmethodid is the Java class object. The second parameter is the parameter (or method name) and the third parameter is the signature of the parameter (or method).
So how do you get the signature of the method?
We're going to use javap-s textmanager this instruction.
First to. Class is located in the folder, in my example is in the D:\CloudAndroid\CloudBox\CloudBoxAndroidGameApplication\bin\com\clouddevelop\cloudbox
Then type javap-s Textmanager to get it, signature is the signature we want.
After so long, my cloudbox finally can display the words!!!!!!