程序员

JNI开发:cpp与java通信

2020-07-01  本文已影响0人  世外大帝

java

MainActivity.java

package com.example.administrator.myapplication;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity
{

    static
    {
        System.loadLibrary("demo");
    }

    private Button conn_and_start;

    // cpp方法
    private native int Start();


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initViewListener();
    }

    private void initViewListener()
    {
        // TODO Auto-generated method stub
        btn.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Start();
            }
        });
    }

    private void initView()
    {
        btn = (Button) findViewById(R.id.btn);
    }

    public void callBack(String p_result)
    {
        Log.e("callBack", "callBack: "+p_result);
    }
}

cpp

demo.cpp

#include"demo.h"
#include"staticJNI.h"
#include <jni.h>
#include <android/log.h>

#define  LOG_TAG    "GLRender"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

#ifdef __cplusplus
extern "C" {
#endif
    
void detect_face_cb(char* p_result)
{
    if (p_result != NULL)
    {
        char *picInfo = p_result;

        JNIEnv *env = NULL;
        JavaVM *javaVm = staticJNI::getJavaVM();
        if (javaVm == NULL)
            return;
        int nStatus = (javaVm)->GetEnv((void **) &env, JNI_VERSION_1_4);
        if (nStatus != JNI_OK)
        {
            nStatus = (javaVm)->AttachCurrentThread(&env, NULL);
        }

        if (nStatus != JNI_OK)
        {
            return;
        }

        jclass clazz = staticJNI::getMainActivity_cls();

        if (clazz != NULL)
        {
            jobject obj = env->AllocObject(clazz);
            jmethodID mid = env->GetMethodID(clazz, "callBack","(Ljava/lang/String;)V");
            if (picInfo != NULL)
                env->CallVoidMethod(obj, mid,env->NewStringUTF(picInfo));
        }

    }
}


jint Java_com_example_administrator_myapplication_MainActivity_Start(JNIEnv *env, jobject obj)
{

    char * c_result = "callBack";
    detect_face_cb(c_result);

    return 0;
}


#ifdef __cplusplus
}
#endif

staticJNI.h

#ifndef _STATICJNI_H_
#define _STATICJNI_H_

#include <jni.h>
#ifdef __cplusplus
extern "C" {
#endif

static JavaVM *javaVm;
static jclass main_cls;

class staticJNI
{
public:

    static JavaVM* getJavaVM();
    static jclass getMainActivity_cls();
};

#ifdef __cplusplus
}
#endif
#endif

staticJNI.cpp

#ifdef ANDROID
#include "staticJNI.h"
#include <stdlib.h>
#include <android/log.h>

#define  LOG_TAG    "staticJNIJava"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

JavaVM* staticJNI::getJavaVM()
{
    return javaVm;
}
jclass staticJNI::getMainActivity_cls()
{
    return main_cls;
}
jint  JNI_OnLoad(JavaVM* vm, void* reserved)
{
    JNIEnv *env;
    javaVm = vm;
    int nRet = -1;

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK)
    {
        if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK)
        {
            jclass main_clazz = env->FindClass("com/example/administrator/myapplication/MainActivity");

            if(main_clazz != NULL)
            {
                main_cls = (jclass)env->NewGlobalRef(main_clazz);
                nRet = JNI_VERSION_1_4;
            }

        }
        nRet = JNI_VERSION_1_4;
    }
    return nRet;
}

void JNI_OnUnload(JavaVM* vm, void* reserved)
{
    javaVm = NULL;
    int nRet = -1;
}

#endif


上一篇下一篇

猜你喜欢

热点阅读