zoukankan      html  css  js  c++  java
  • JNI接口的两种实现方式

    1.利用javac和javah生成头文件,网上已有不少例子。
    2.采用注册的方式生成,这里重点介绍本方法。
     
    (a).声明好需要使用的和对象化的全局变量
    static JavaVM* g_jvm = NULL;
    static jclass g_java_capturer_class = NULL; // VideoCaptureAndroid.class.
    static jclass g_java_encode_class = NULL; // VideoCaptureAndroid.class.
    (b).初始化Java变量
    extern "C" int32_t WINAPI InitCaptureAndroidVM(JavaVM* javaVM, jobject context) {
    TRACE_INFO2("InitCaptureAndroidVM");
    if (javaVM)
    {
    TRACE_INFO2("InitCaptureAndroidVM, init");
    g_jvm = javaVM;
    AttachThreadScoped ats(g_jvm);
    g_context = ats.env()->NewGlobalRef(context);
    }
    else
    {
    if (g_jvm)
    {
    AttachThreadScoped ats(g_jvm);
    if (g_context)
    {
    ats.env()->DeleteGlobalRef(g_context);
    g_context = NULL;
    }
    g_jvm = NULL;
    }
    }
    return 0;
    }
    (c).在实现对象的时候,注册需要使用的ndk接口
    extern "C" int32_t WINAPI InitCaptureObject() {
     
    if (g_jvm) {
    TRACE_INFO2("InitCaptureObject, bEnableHwEncoder="<<bEnableHwEncoder);
     
    AttachThreadScoped ats(g_jvm);
    jclass j_capture_class =
    ats.env()->FindClass("com/anbang/videocapture/VideoCaptureAndroid");
    assert(j_capture_class);
    g_java_capturer_class =
    reinterpret_cast<jclass>(ats.env()->NewGlobalRef(j_capture_class));
    assert(g_java_capturer_class);
     
    TRACE_INFO2("Register VideoCaptureAndroid Native Method");
     
    JNINativeMethod native_methods[] = {
    {"GetContext",
    "()Landroid/content/Context;",
    reinterpret_cast<void*>(&GetContext)},
    {"OnOrientationChanged",
    "(JI)V",
    reinterpret_cast<void*>(&OnOrientationChanged)},
    {"ProvideCameraFrame",
    "([BIIJJ)V",
    reinterpret_cast<void*>(&ProvideCameraFrame)}};
    if (ats.env()->RegisterNatives(g_java_capturer_class,native_methods, 3) != 0)
    assert(false);
    }

    本博客所有内容均为原创,转载请说明出处。欢迎音视频多媒体领域的朋友来人来函交流心得。
  • 相关阅读:
    经典SQL语句大全
    《构建高性能Web站点》观后感
    网搜索引擎架构设计
    使用Windows系统的几个好的习惯
    静态页面对seo优化之详解
    让您SEO学习时间缩短一半的高阶秘籍
    java链表
    GAE 数据存储——事务
    GAE 博客——B3log Solo 0.2.0 发布了!
    Wine 1.3.7 发布
  • 原文地址:https://www.cnblogs.com/liuxt/p/8144679.html
Copyright © 2011-2022 走看看