Java Native Interface 基于JNI的嵌入式手机软件开发实例

时间:2023-02-16 11:52:52

1、通过JNI和c/c++的库组件、其他代码交互

2、java和c不能互通的原因时数据类型问题

Introduction https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/intro.html#java_native_interface_overview

This chapter introduces the Java Native Interface (JNI). The JNI is a native programming interface. It allows Java code that runs inside a Java Virtual Machine (VM) to interoperate with applications and libraries written in other programming languages, such as C, C++, and assembly.

The most important benefit of the JNI is that it imposes no restrictions on the implementation of the underlying Java VM. Therefore, Java VM vendors can add support for the JNI without affecting other parts of the VM. Programmers can write one version of a native application or library and expect it to work with all Java VMs supporting the JNI.

This chapter covers the following topics:

Java Native Interface Overview

While you can write applications entirely in Java, there are situations where Java alone does not meet the needs of your application. Programmers use the JNI to write Java native methods to handle those situations when an application cannot be written entirely in Java.

The following examples illustrate when you need to use Java native methods:

  • The standard Java class library does not support the platform-dependent features needed by the application.
  • You already have a library written in another language, and wish to make it accessible to Java code through the JNI.
  • You want to implement a small portion of time-critical code in a lower-level language such as assembly.

By programming through the JNI, you can use native methods to:

  • Create, inspect, and update Java objects (including arrays and strings).
  • Call Java methods.
  • Catch and throw exceptions.
  • Load classes and obtain class information.
  • Perform runtime type checking.

You can also use the JNI with the Invocation API to enable an arbitrary native application to embed the Java VM. This allows programmers to easily make their existing applications Java-enabled without having to link with the VM source code.

Java Native Interface  基于JNI的嵌入式手机软件开发实例

Java Native Interface  基于JNI的嵌入式手机软件开发实例

Java Native Interface  基于JNI的嵌入式手机软件开发实例

Java Native Interface  基于JNI的嵌入式手机软件开发实例

JNI_百度百科 https://baike.baidu.com/item/JNI/9412164?fr=aladdin

调用问题

编辑

在首次使用JNI的时候有些疑问,后来在使用中一一解决,下面就是这些问题的备忘:
1.java和c是如何互通的?
其实不能互通的原因主要是数据类型的问题,jni解决了这个问题,例如那个c文件中的jstring数据类型就是java传入的String对象,经过jni函数的转化就能成为c的char*。
对应数据类型关系如下表:
Java 类型 本地 C 类型
实际表示的 C 类型
(Win32)
说明
boolean jboolean unsigned char 无符号,8 位
byte jbyte signed char 有符号,8 位
char jchar unsigned short 无符号,16 位
short jshort short 有符号,16 位
int jint long 有符号,32 位
long jlong __int64 有符号,64 位
float jfloat float 32 位
double jdouble double 64 位
void void N/A N/A
JNI 还包含了很多对应于不同 Java 对象的引用类型如下图:
2. 如何将java传入的String参数转换为c的char*,然后使用?
java传入的String参数,在c文件中被jni转换为jstring的数据类型,在c文件中声明char* test,然后test = (char*)(*env)->GetStringUTFChars(env, jstring, NULL);注意:test使用完后,通知虚拟机平台相关代码无需再访问:(*env)->ReleaseStringUTFChars(env, jstring, test);
3. 将c中获取的一个char*的buffer传递给java?
这个char*如果是一般的字符串的话,作为string传回去就可以了。如果是含有’\0’的buffer,最好作为bytearray传出,因为可以制定copy的length,如果copy到string,可能到’\0’就截断了。
有两种方式传递得到的数据:
一种是在jni中直接new一个byte数组,然后调用函数(*env)->SetByteArrayRegion(env, bytearray, 0, len, buffer);将buffer的值copy到bytearray中,函数直接return bytearray就可以了。
一种是return错误号,数据作为参数传出,但是java的基本数据类型是传值,对象是传递的引用,所以将这个需要传出的byte数组用某个类包一下,如下:
class RetObj { public byte[] bytearray; } 这个对象作为函数的参数retobj传出,通过如下函数将retobj中的byte数组赋值便于传出。代码如下:
jclass cls;
jfieldID fid;
jbyteArray bytearray;
bytearray = (*env)->NewByteArray(env,len);
(*env)->SetByteArrayRegion(env, bytearray, 0, len, buffer);
cls = (*env)->GetObjectClass(env, retobj);
fid = (*env)->GetFieldID(env, cls, "retbytes", "[B"]);
(*env)->SetObjectField(env, retobj, fid, bytearray);
4. 不知道占用多少空间的buffer,如何传递出去呢?
在jni的c文件中new出空间,传递出去。java的数据不初始化,指向传递出去的空间即可。
 
 
 
Java Native Interface  基于JNI的嵌入式手机软件开发实例
基于JNI的嵌入式手机软件开发实例 下面通过一个实例来描述运用JNI技术在手机上操纵摄像头,捕捉视频并存储图片的过程。
 

活动状态

图2为捕捉视频并存储图片的活动/状态图
 
根据图2的活动/状态,具体的对应步骤如下:
①发起该流程。
②发起流程后,建立文件用于存储图片。
③用指针获得分配的缓冲器,用于存储获得的帧。
④将指针压栈(序列化缓冲器)。由于手机的内存较小,为了防止内存泄漏,Symbian操作系统有一个Cleanupstack的要求,即在使用指针时,用PushL把指针压入栈中,使用完后再用Pop弹出栈.如果在中间调用导致崩溃的函数时果真出现了问题,那么Cleanupstack可以通过调用该指针的析构函数回收占用的空间。
⑤操纵摄像头,捕捉视频,并将图像流从摄像头端传到缓冲器
⑥将摄像头内的图像流存入缓冲器内,并将缓冲器内的流转化为文件流,存为jpg格式的文件,将指向缓冲器的指针弹栈。
⑦在过程⑥中,如果使用完了序列化的缓冲器,则要重新序列化缓冲器,以备后面使用。
⑧当接收到停止视频捕捉的信号后,关闭文件。
⑨流程结束。
 

视频捕捉

子功能捕捉视频的实现是由操纵摄像头、视频播放(解码器准备)以及建立摄像头和手机之间的连接会话三个活动组成的。其中操纵摄像头是通过调用底层设备的驱动来实现的,需要利用JNI来实现,完成的方法包括准备、建立、删除、销毁摄像头等。视频播放的一系列过程也是通过c++代码来实现的,除了准备、建立、删除、销毁解码器外,还有开始、暂停、停止解码等。建立摄像头和手机之间的连接类似建立客户端和服务器连接,视频流从摄像头传到手机界面是通过多媒体会话来完成的。多媒体会话的建立、关闭、摧毁以及会话建立后的发送、取消、读取数据等也是JNI的应用范畴
 
 

Java Native Interface 基于JNI的嵌入式手机软件开发实例的更多相关文章

  1. Java Native Interface Specification(JNI)

    Java Native Interface Specification(JNI) 使用场景: 需要的功能,标准的java不能提供 有了一个用其他的语言写好的工具包,希望用java去访问它 当需要高性能 ...

  2. Java Native Interface 六JNI中的异常

    本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 在这里只讨论调用JNI方法可能会出现的异常, ...

  3. Java Native Interface 五 JNI里的多线程与JNI方法的注册

    本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 JNI里的多线程 在本地方法里写有关多线程的 ...

  4. Java Native Interface 四--JNI中引用类型

    本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 JNI支持将类实例和数组类型(如jobjec ...

  5. Java Native Interface 二 JNI中对Java基本类型和引用类型的处理

    本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 Java编程里会使用到两种类型:基本类型(如 ...

  6. Java Native Interface(JNI)

    JNI能让Java代码在Java虚拟机里调用其他编程语言(例如C.C++)写的应用或库,且不会影响任何Java虚拟机的实现. 什么时候用JNI? 1.应用程序所需的平台相关功能,标准的Java类库不支 ...

  7. 【详解】JNI &lpar;Java Native Interface&rpar; (二)

    案例二:传递参数给C代码,并从其获取结果 注:这里传递的参数是基本类型的参数,在C代码中有直接的映射类型. 此案例所有生成的所有文件如下: (1)编写案例二的Java代码,如下: 这里我们定义了一个n ...

  8. Java Native Interface Specification Contents 翻译

    https://docs.oracle.com/en/java/javase/12/docs/specs/jni/index.html Google翻译 第1章:简介 本章介绍Java Native ...

  9. android 学习随笔二十七(JNI:Java Native Interface&comma;JAVA原生接口 )

    JNI(Java Native Interface,JAVA原生接口) 使用JNI可以使Java代码和其他语言写的代码(如C/C++代码)进行交互. 问:为什么要进行交互? 首先,Java语言提供的类 ...

随机推荐

  1. docker nginx1&period;7&period;6&plus;keepalived实现双机热备

    0.前提条件 环境两台ubuntu版本14.04 64位系统(并获取root权限) 假设两台服务器ip为:172.16.34.214(master),172.16.34.215(backup),kee ...

  2. oracle 特殊符号

    http://hi.baidu.com/wind_stay/blog/item/85113a6f6553a5d680cb4a0e.html oracle通配符,运算符的使用 用于where比较条件的有 ...

  3. HAL中通过JNI调用java方法【转】

    转载请注明本文出处:http://www.cnblogs.com/xl19862005 作者:Xandy 由于工作的需要,最近一直在研究HAL.JNI.Java方法之间互调的问题,并做了如下一些记录和 ...

  4. swfit-小知识Demo

    知识点: 重写方法.属性,自动引用计数,throws异常抛出,滚动视图,扩展语法,协议,计时器,UserDefaultsgit项目地址: https://github.com/lu459700780/ ...

  5. dia 在Linux&lpar;ubuntu&rpar;下无法输入中文的解决办法 &period;

    我是执行一下命令安装的 sudo apt-get install dia sudo apt-get install dia 打开软件后发现不能输入中文,网上搜索一圈后找到以下解决方案 sudo vim ...

  6. JQuery-常用小组件

    常用的小组件记录 1. 单选框.复选框重置样式效果 参考: http://www.cnblogs.com/sanshi/p/4369375.html 三生石上 参考: http://www.jq22. ...

  7. 7&period;11js的总结

    <!DOCTYPE html> <html> <head> <title>js的内置全局函数</title> <script type ...

  8. SqlServer获取字符串中数字&comma;中文及字符部分数据

    --获取英文字符数据 Create function [dbo].[Fun_GetChar] ( ) ) ) AS BEGIN BEGIN ,'') --删掉一个非数字的字符,循环结束,剩余的为数字部 ...

  9. 【PL&sol;SQL编程】循环语句

    1. loop语句 loop plsql_sentence; exit when end_condition_exp; end loop; loop语句会先执行一次循环体,然后再判断“exit whe ...

  10. 正割、余割、正弦、余弦、正切、余切之间的关系的公式 sec、csc与sin、cos、tan、cot之间的各种公式

    1.倒数关系 tanα ·cotα=1 sinα ·cscα=1 cosα ·secα=1 2.商数关系 tanα=sinα/cosα cotα=cosα/sinα 3.平方关系 sinα²+cosα ...