zoukankan      html  css  js  c++  java
  • 从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);
    }
    }
    }

    本博客所有内容均为原创,转载请说明出处。欢迎音视频多媒体领域的朋友来人来函交流心得。
  • 相关阅读:
    使用阿里云服务器的总结二-----目录权限
    使用阿里云服务器的总结一----修改配置
    thinkphp框架开启页面gzip压缩
    内容页分页代码
    js禁止中文输入 最简洁的【禁止输入中文】
    JS中setTimeout()的用法详解
    面向对象的5条基本设计原则
    C#_面试题1
    问题 E: C语言11.8
    问题 D: C语言11.7
  • 原文地址:https://www.cnblogs.com/liuxt/p/8427471.html
Copyright © 2011-2022 走看看