標籤:view sse oba src extc div odi 技術 抽象
從java朝jni中傳遞各種資料,是在android開發中經常需要面對的事情。對於一些典型的資料類型,網上已經有很多文章介紹,這裡列出一些資料類型:
對於GLSurfaceView,則使用:Landroid/opengl/GLSurfaceView;我的程式分為三層,App層,SDK層和capture層,其中capture層是採用c++調用java介面實現。下面將詳細流程敘述如下:1.在抽象java類中聲明native介面public class ConfMgr {static {try {System.loadLibrary("confmgr");}catch (Throwable e){Log.e("conf", "load so error:"+e.getMessage());}}public static native void InitAndroidVM(Context context);public static native void UninitAndroidVM();public static native void SetPreviewWnd(SurfaceView glview);}2.用javah命令產生標頭檔JNIEXPORT void JNICALL Java_com_jni_conf_ConfMgr_SetPreviewWnd(JNIEnv *env, jclass instance,jobject glview);3.實現函數介面,聲明介面SetCamPreviewWndvoid WINAPI SelectCameraFace(BOOL bFrontFace);JNIEXPORT void JNICALL Java_com_jni_conf_ConfMgr_SetPreviewWnd(JNIEnv *env, jclass instance,jobject glview){SetCamPreviewWnd(glview);}4.在SDK庫檔案中實現和聲明介面SetCamPreviewWndUTIL_EXPORT void WINAPI SetCamPreviewWnd(jobject glview);extern "C" void WINAPI SetCamPreviewWnd(jobject glview){TRACE_INFO2("vmgr::SetCamPreview");SetLocalPreview(glview);}5.在Campture庫檔案中聲明和實現SetLocalPreview介面extern "C" int32_t WINAPI SetLocalPreview(jobject glview);class androidcapturedelegate {public:static androidcapturedelegate* Create();static void Destroy(androidcapturedelegate *pDelete); androidcapturedelegate(const int32_t id);androidcapturedelegate();virtual int32_t Init();void InitCapture(int iWidth, int iHeight, int iFps, int iBitrate);static void SetLocalCamPreview(jobject glview);void SetCamPreview(jobject glview);static androidcapturedelegate *g_pAndCapDel;protected:virtual ~androidcapturedelegate();jobject _jCapturer; // Global ref to Java VideoCaptureAndroid object.};void androidcapturedelegate::SetLocalCamPreview(jobject glview){if(androidcapturedelegate::g_pAndCapDel != NULL){TRACE_INFO2("androidcapturedelegate::SetLocalCamPreview, enter");g_pAndCapDel->SetCamPreview(glview);}}void androidcapturedelegate::SetCamPreview(jobject glview){TRACE_INFO2("androidcapturedelegate::SetCamPreview, enter"); AttachThreadScoped ats(g_jvm);JNIEnv* env = ats.env();jmethodID jmethodID =env->GetMethodID(g_java_capturer_class, "setLocalPreview", "(Landroid/opengl/GLSurfaceView;)V");assert(jmethodID);env->CallVoidMethod(_jCapturer, jmethodID,glview);}6.在java檔案中實現setLocalPreview介面public class VideoCaptureAndroid implements GLSurfaceView.Renderer {private GLSurfaceView mLocalPreview = null;public VideoCaptureAndroid(long native_capturer) {}public void setLocalPreview(GLSurfaceView localPreview) {mLocalPreview = (GLSurfaceView) localPreview;mLocalPreview.setEGLContextClientVersion(2);mLocalPreview.setRenderer(mSelf);mLocalPreview.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);if(!mEnableHWCODEC) {mLocalPreview.getHolder().setFixedSize(640, 480);}}}
從java層向jni中傳遞GLSurfaceView的方法