JNI实战

作者: 韭菜鸡蛋炒米线 | 来源:发表于2018-06-28 11:08 被阅读0次

    使用jni完成c++代码调用

    1、https://blog.csdn.net/yanhenyiduo/article/details/72783709这边讲了基础的设置

    2、遇到问题汇总

    Q、需要在target--class文件夹下运行命令行,javah -jni com.rock.water.WindPdfLibHelper,注意使用类的全限定名称,生成后将com_rock_water_WindPdfLibHelper.h加入到c++工程

    Q、C++工程中添加jni.h和jni_md.h这两个文件时,不要直接引用jdk中的文件,把jdk中的文件拷贝到工程中,工程中直接使用#include "jni.h"完成引用,c++工程中随意新建个文件包含com_rock_water_WindPdfLibHelper.h,

    就可以实现java中定义的函数,参数名称可以自己随便加,env用来新增int数组,byte数组,也可以调用java的代码

    Q、static函数和非static函数在c++中的区别是函数有个参数类型是jclass 和jobject

    Q、报错的函数里有个memcpy把mupdf的unsiged char数组转化为char数组,尝试将char数据利用env->jstring NewStringUTF(const char *utf) 转化为string输出给上层的时候,出现以下错误

    #

    # A fatal error has been detected by the Java Runtime Environment:

    #

    #  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000007fef2785632, pid=7008, tid=13384

    #

    # JRE version: Java(TM) SE Runtime Environment (8.0_77-b03) (build 1.8.0_77-b03)

    # Java VM: Java HotSpot(TM) 64-Bit Server VM (25.77-b03 mixed mode windows-amd64 compressed oops)

    # Problematic frame:

    # C  [WindPdfLib.dll+0x5632]  OutputAndFreeBuffer+0x72

    #

    # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows

    #

    # If you would like to submit a bug report, please visit:

    #  http://bugreport.java.com/bugreport/crash.jsp

    # The crash happened outside the Java Virtual Machine in native code.

    # See problematic frame for where to report the bug.

    #

    解决方法,buf里放的是unsiged char,转化为byte数组,将byte数组上传到上层进行utf-8解码,byte是不同编码方式的中间数据,说明出现这个问题跟使用的方法有关系

    jbyteArray bytes=env->NewByteArray(buf->len);

    env->SetByteArrayRegion(bytes, 0, buf->len, (jbyte*)buf->data);//void SetByteArrayRegion(jbyteArray array, jsize start, jsize len,const jbyte *buf)

    fz_close_output(out);

    fz_drop_buffer(app->ctx, buf);

    return bytes;

    3 有问题找官方文档https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html#wp6212

    相关文章

      网友评论

          本文标题:JNI实战

          本文链接:https://www.haomeiwen.com/subject/eftkyftx.html