1. 程式人生 > >從java層向jni中傳遞GLSurfaceView的方法

從java層向jni中傳遞GLSurfaceView的方法

bool 很多 code http codec android class ble extern

從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.實現函數接口,聲明接口SetCamPreviewWnd void WINAPI SelectCameraFace(BOOL bFrontFace); JNIEXPORT void JNICALL Java_com_jni_conf_ConfMgr_SetPreviewWnd(JNIEnv *env, jclass instance,jobject glview) { SetCamPreviewWnd(glview); } 4.在SDK庫文件中實現和聲明接口SetCamPreviewWnd UTIL_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的方法