From 2dd88e8be53762ebd137ccac8ec784ee7bdd0293 Mon Sep 17 00:00:00 2001 From: Wenhai Lin Date: Wed, 15 Apr 2015 18:20:52 +0800 Subject: [PATCH] Fixed JNI illegal start byte error --- cocos/platform/android/CCDevice-android.cpp | 10 ++++++---- .../java/src/org/cocos2dx/lib/Cocos2dxBitmap.java | 12 +++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cocos/platform/android/CCDevice-android.cpp b/cocos/platform/android/CCDevice-android.cpp index f18cf7cfa1..0e54bd1601 100644 --- a/cocos/platform/android/CCDevice-android.cpp +++ b/cocos/platform/android/CCDevice-android.cpp @@ -88,7 +88,7 @@ public: { JniMethodInfo methodInfo; if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmapShadowStroke", - "(Ljava/lang/String;Ljava/lang/String;IIIIIIIIZFFFFZIIIIF)Z")) + "([BLjava/lang/String;IIIIIIIIZFFFFZIIIIF)Z")) { CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__); return false; @@ -111,10 +111,12 @@ public: * and data. * use this approach to decrease the jni call number */ - jstring jstrText = methodInfo.env->NewStringUTF(text); + int count = strlen(text); + jbyteArray strArray = methodInfo.env->NewByteArray(count); + methodInfo.env->SetByteArrayRegion(strArray, 0, count, reinterpret_cast(text)); jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str()); - if(!methodInfo.env->CallStaticBooleanMethod(methodInfo.classID, methodInfo.methodID, jstrText, + if(!methodInfo.env->CallStaticBooleanMethod(methodInfo.classID, methodInfo.methodID, strArray, jstrFont, textDefinition._fontSize, textDefinition._fontFillColor.r, textDefinition._fontFillColor.g, textDefinition._fontFillColor.b, textDefinition._fontAlpha, eAlignMask, nWidth, nHeight, @@ -126,7 +128,7 @@ public: return false; } - methodInfo.env->DeleteLocalRef(jstrText); + methodInfo.env->DeleteLocalRef(strArray); methodInfo.env->DeleteLocalRef(jstrFont); methodInfo.env->DeleteLocalRef(methodInfo.classID); diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxBitmap.java index 0bbefef575..ffe578ac67 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -93,20 +93,26 @@ public class Cocos2dxBitmap { final int fontSize, final int alignment, final int width, final int height) { - createTextBitmapShadowStroke( string, fontName, fontSize, 255, 255, 255, 255, // text font and color + createTextBitmapShadowStroke( string.getBytes(), fontName, fontSize, 255, 255, 255, 255, // text font and color alignment, width, height, // alignment and size false, 0.0f, 0.0f, 0.0f, 0.0f, // no shadow false, 255, 255, 255, 255, 0.0f); // no stroke } - public static boolean createTextBitmapShadowStroke(String string, final String fontName, int fontSize, + public static boolean createTextBitmapShadowStroke(byte[] bytes, final String fontName, int fontSize, int fontTintR, int fontTintG, int fontTintB, int fontTintA, int alignment, int width, int height, boolean shadow, float shadowDX, float shadowDY, float shadowBlur, float shadowOpacity, boolean stroke, int strokeR, int strokeG, int strokeB, int strokeA, float strokeSize) { - + String string; + if (bytes == null || bytes.length == 0) { + string = ""; + } else { + string = new String(bytes); + } + final int horizontalAlignment = alignment & 0x0F; final int verticalAlignment = (alignment >> 4) & 0x0F;