测试例子源地址: https://github.com/roman10/android-ffmpeg-tutorial
int add(int a, int b) {
return a + b;
}
Java 类型 |
类型签名 |
boolean |
Z |
byte |
B |
char |
C |
short |
S |
int |
I |
long |
L |
float |
F |
double |
D |
类 |
L全限定名;,比如String, 其签名为Ljava/lang/util/String; |
数组 |
[类型签名, 比如 [B |
(类型签名1类型签名2...)返回值类型签名
如String toString() 函数:"()Ljava/lang/String;"
jint naMain(JNIEnv *pEnv, jobject pObj, jobject pMainAct, jstring pFileName, jint pNumOfFrames)
Java 声明:
private static native int naMain(MainActivity pObject, String pVideoFileName, int pNumOfFrames);
Java类型 |
本地类型 |
说明 |
boolean |
jboolean |
无符号,8位 |
byte |
jbyte |
无符号,8位 |
char |
jchar |
无符号,16位 |
short |
jshort |
有符号,16位 |
int |
jint |
有符号,32位 |
long |
jlong |
有符号,64位 |
float |
jfloat |
32位 |
double |
jdouble |
|
void |
void |
N/A |
String 对应 jstring
其它 对应 jobject
typedef struct {
const char* name;
const char* signature;
void* fnPtr;
} JNINativeMethod;
JNINativeMethod nm[1];
nm[0].name = "naMain";
nm[0].signature = "(Lroman10/tutorial/android_ffmpeg_tutorial01/MainActivity;Ljava/lang/String;I)I";
nm[0].fnPtr = (void*)naMain;
jclass cls = (*env)->FindClass(env, "roman10/tutorial/android_ffmpeg_tutorial01/MainActivity");
//Register methods with env->RegisterNatives.
(*env)->RegisterNatives(env, cls, nm, 1);
jclass cls = (*env)->FindClass(env, "roman10/tutorial/android_ffmpeg_tutorial01/MainActivity");
//Register methods with env->RegisterNatives.
(*env)->RegisterNatives(env, cls, nm, 1);
jclass javaBitmapClass = (jclass)(*pEnv)->FindClass(pEnv, "android/graphics/Bitmap");
jmethodID mid = (*pEnv)->GetStaticMethodID(pEnv, javaBitmapClass, "createBitmap", "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
。。。
jclass bitmapConfigClass = (*pEnv)->FindClass(pEnv, "android/graphics/Bitmap$Config");
jobject javaBitmapConfig = (*pEnv)->CallStaticObjectMethod(pEnv, bitmapConfigClass,
(*pEnv)->GetStaticMethodID(pEnv, bitmapConfigClass, "valueOf", "(Ljava/lang/String;)Landroid/graphics/Bitmap$Config;"), jConfigName);
//create the bitmap
return (*pEnv)->CallStaticObjectMethod(pEnv, javaBitmapClass, mid, pWidth, pHeight, javaBitmapConfig);
void SaveFrame(JNIEnv *pEnv, jobject pObj, jobject pBitmap, int width, int height, int iFrame) {
char szFilename[200];
jmethodID sSaveFrameMID;
jclass mainActCls;
sprintf(szFilename, "/sdcard/android-ffmpeg-tutorial01/frame%d.jpg", iFrame);
mainActCls = (*pEnv)->GetObjectClass(pEnv, pObj);
sSaveFrameMID = (*pEnv)->GetMethodID(pEnv, mainActCls, "saveFrameToPath", "(Landroid/graphics/Bitmap;Ljava/lang/String;)V");
LOGI("call java method to save frame %d", iFrame);
jstring filePath = (*pEnv)->NewStringUTF(pEnv, szFilename);
(*pEnv)->CallVoidMethod(pEnv, pObj, sSaveFrameMID, pBitmap, filePath);
LOGI("call java method to save frame %d done", iFrame);
}