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

    本博客所有内容均为原创,转载请说明出处。欢迎音视频多媒体领域的朋友来人来函交流心得。
  • 相关阅读:
    emacs 探索之六:latex中文支持
    One网络模拟器探索之六:Report类的扩展
    emacs 探索之五:latex配置
    emacs 探索之三:基本操作
    DataSet数据传送性能比较
    SQL 2008 附加数据库报5120的错误的解决办法
    软件工程师不可不知的10个概念
    在日期上加上相应天数,并在GridView上显示
    SQL 跨表更新
    SQLSERVER 处理两个日期相减
  • 原文地址:https://www.cnblogs.com/liuxt/p/8144679.html
Copyright © 2011-2022 走看看