一切有为法,如梦幻泡影,如露亦如电,应作如是观

从java层向jni中传递GLSurfaceView的方法

从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);
}
}
}

posted @ 2018-02-07 17:00  liuxt  阅读(608)  评论(0编辑  收藏  举报
天行键,君子以自强不息