Fixed JNI illegal start byte error

This commit is contained in:
Wenhai Lin 2015-04-15 18:20:52 +08:00
parent d54ddbd010
commit 2dd88e8be5
2 changed files with 15 additions and 7 deletions

View File

@ -88,7 +88,7 @@ public:
{ {
JniMethodInfo methodInfo; JniMethodInfo methodInfo;
if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmapShadowStroke", 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__); CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__);
return false; return false;
@ -111,10 +111,12 @@ public:
* and data. * and data.
* use this approach to decrease the jni call number * 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<const jbyte*>(text));
jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str()); 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, jstrFont, textDefinition._fontSize, textDefinition._fontFillColor.r, textDefinition._fontFillColor.g,
textDefinition._fontFillColor.b, textDefinition._fontAlpha, textDefinition._fontFillColor.b, textDefinition._fontAlpha,
eAlignMask, nWidth, nHeight, eAlignMask, nWidth, nHeight,
@ -126,7 +128,7 @@ public:
return false; return false;
} }
methodInfo.env->DeleteLocalRef(jstrText); methodInfo.env->DeleteLocalRef(strArray);
methodInfo.env->DeleteLocalRef(jstrFont); methodInfo.env->DeleteLocalRef(jstrFont);
methodInfo.env->DeleteLocalRef(methodInfo.classID); methodInfo.env->DeleteLocalRef(methodInfo.classID);

View File

@ -93,19 +93,25 @@ public class Cocos2dxBitmap {
final int fontSize, final int alignment, final int width, final int fontSize, final int alignment, final int width,
final int height) { 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 alignment, width, height, // alignment and size
false, 0.0f, 0.0f, 0.0f, 0.0f, // no shadow false, 0.0f, 0.0f, 0.0f, 0.0f, // no shadow
false, 255, 255, 255, 255, 0.0f); // no stroke 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 fontTintR, int fontTintG, int fontTintB, int fontTintA,
int alignment, int width, int height, int alignment, int width, int height,
boolean shadow, float shadowDX, float shadowDY, float shadowBlur, float shadowOpacity, boolean shadow, float shadowDX, float shadowDY, float shadowBlur, float shadowOpacity,
boolean stroke, int strokeR, int strokeG, int strokeB, int strokeA, float strokeSize) { 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 horizontalAlignment = alignment & 0x0F;
final int verticalAlignment = (alignment >> 4) & 0x0F; final int verticalAlignment = (alignment >> 4) & 0x0F;