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

转载于:https://www.cnblogs.com/liuxt/p/8144679.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值