Your SlideShare is downloading. ×
Drive for better visionAndroid JNIFebruary, 2012                         Himax Media                         Solutions    ...
JNI (Java Native Interface)      java 可透過 JNI 呼叫底層用 C/C++ 寫的 function      可以重複利用 opensource library             libjpe...
JNI 流程           1. 建立 java class ,                          3. 用 javah 產生          裡面宣告 native method                    ...
註冊 JNI method - 1           typedef struct {                const char *name;                        Java 函式的名稱          ...
註冊 JNI method - 2       frameworksbasecorejniandroid_view_MotionEvent.cpp             定義對應關係              { "nativeIsTou...
註冊 JNI method - 3             frameworksbasecorejniAndroidRuntime.cpp:                   static const RegJNIRec gRegJNI[...
呼叫 JNI method             frameworksbasecorejavaandroidviewMotionEvent.              java                   在 java class...
Drive for better vision
Upcoming SlideShare
Loading in...5
×

01 android jni

539

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
539
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "01 android jni"

  1. 1. Drive for better visionAndroid JNIFebruary, 2012 Himax Media Solutions 承景科技股份有限公司
  2. 2. JNI (Java Native Interface)  java 可透過 JNI 呼叫底層用 C/C++ 寫的 function  可以重複利用 opensource library  libjpeg, libpng java class java class use call JVM register C/C++ sourceHimax Media Solutions Proprietary & Confidential 2
  3. 3. JNI 流程 1. 建立 java class , 3. 用 javah 產生 裡面宣告 native method header file HelloWorld.java HelloWorld.h 4. 用 C 語言實作 2.compile class native method HelloWorld.class HelloWorld.c 5. 產生 library 6. 執行 java HelloWorld.soHimax Media Solutions Proprietary & Confidential 3
  4. 4. 註冊 JNI method - 1  typedef struct { const char *name; Java 函式的名稱 const char *signature; Java 函式的參數和傳回值 void *fnPtr; 對應的 C 函式指標 } JNINativeMethod;  signature 的格式  "()V" void f()  "(ZIJ)D" double f(boolean, int, long)  "(FBC)S" short f(float, byte, char)  "([F[D)[J" long[] f(float[], double[])  範例  {"hide", "()V", (void*)Surface_hide },  {"native_reset","(I)V", (void*) SkPathGlue::reset},Himax Media Solutions Proprietary & Confidential 4
  5. 5. 註冊 JNI method - 2  frameworksbasecorejniandroid_view_MotionEvent.cpp  定義對應關係 { "nativeIsTouchEvent", "(I)Z", (void*)android_view_MotionEvent_nativeIsTouchEvent }  定義 function 內容 static jboolean android_view_MotionEvent_nativeIsTouchEvent (JNIEnv* env, jclass clazz, jint nativePtr) { MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr); return event->isTouchEvent(); }  jniRegisterNativeMethods(env, "android/view/MotionEvent", gMotionEventMethods, NELEM(gMotionEventMethods));Himax Media Solutions Proprietary & Confidential 5
  6. 6. 註冊 JNI method - 3  frameworksbasecorejniAndroidRuntime.cpp:  static const RegJNIRec gRegJNI[] = { REG_JNI(register_android_debug_JNITest), REG_JNI(register_android_os_SystemClock), ... };  register_jni_procs(gRegJNI, NELEM(gRegJNI), env);Himax Media Solutions Proprietary & Confidential 6
  7. 7. 呼叫 JNI method  frameworksbasecorejavaandroidviewMotionEvent. java  在 java class 中宣告,宣告後會視為 class method private static native boolean nativeIsTouchEvent(int nativePtr);  使用 public final boolean isTouchEvent() { return nativeIsTouchEvent(mNativePtr); }  frameworkdbasecorejavaandroidviewView.java  在其他 class 使用 public final boolean dispatchPointerEvent(MotionEvent event) { event.isTouchEvent()Himax Media Solutions Proprietary & Confidential 7
  8. 8. Drive for better vision

×