JNI

用 JNI 进行 Java 编程(2)

徘徊边缘 提交于 2019-12-03 23:47:55
从 Java 程序调用 C/C++ 代码 概述 当无法用 Java 语言编写整个应用程序时,JNI 允许您使用本机代码。在下列典型情况下,您可能决定使用本机代码: 希望用更低级、更快的编程语言去实现对时间有严格要求的代码。 希望从 Java 程序访问旧代码或代码库。 需要标准 Java 类库中不支持的依赖于平台的特性。 从 Java 代码调用 C/C++ 的六个步骤 从 Java 程序调用 C 或 C ++ 代码的过程由六个步骤组成。 我们将在下面几页中深入讨论每个步骤,但还是先让我们迅速地浏览一下它们。 编写 Java 代码 。我们将从编写 Java 类开始,这些类执行三个任务:声明将要调用的本机方法;装入包含本机代码的共享库;然后调用该本机方法。 编译 Java 代码 。在使用 Java 类之前,必须成功地将它们编译成字节码。 创建 C/C++ 头文件 。C/C++ 头文件将声明想要调用的本机函数说明。然后,这个头文件与 C/C++ 函数实现(请参阅步骤 4)一起来创建共享库(请参阅步骤 5)。 编写 C/C++ 代码 。这一步实现 C 或 C++ 源代码文件中的函数。C/C++ 源文件必须包含步骤 3 中创建的头文件。 创建共享库文件 。从步骤 4 中创建的 C 源代码文件来创建共享库文件。 运行 Java 程序 。运行该代码,并查看它是否有用

Bluedroid的结构和代码分布

醉酒当歌 提交于 2019-12-03 15:32:38
在android4.2中,Google更换了android的蓝牙协议栈,从Bluez换成Bluedroid,我也是初涉这个方面,顺便记录一下。 http://source.android.com/devices/bluetooth.html android development对于4.3蓝牙的介绍: android提供BlueDroid作为默认的协议栈,BlueDroid分为两个部分: 1、Bluetooth Embedded System(BTE),它实现了BT的核心功能。 2、Bluetooth Application Layer (BTA),用于和android framework层交互。 BT 系统服务通过JNI与BT stack交互,并且通过Binder IPC通信与应用交互。这个系统服务同时也提供给RD获取不同的BT profiles;下面的图标展示BT stack的一个大体的结构: 一、application Framework 这个层的代码主要是利用android.bluetooth APIS 和 bluetooth hardware进行交互。 也就是通过Binder IPC机制调用bluetooth 进程; 代码位于framework/base/core/java/android.bluetooth/下。 比如A2DP的连接:framework/base

JniLibs和Jni的区别

旧街凉风 提交于 2019-12-03 11:20:47
有个项目是需要和Native有交互 然后原工程时有个文件夹叫JniLibs 但是我自己新建的工程给他取了个名字叫Jni 而且是使用系统的自建功能建立的Jni文件夹 天真的我以为这样就可以了,但是在使用的时候却遇到了各种问题 最烦的就是 java.lang.UnsatisfiedLinkError: dlopen failed: library 当时跑到真机上报了这个错误 大概的意思就是我的so找不到了 网上很多说就是要满足armeabi文件夹内和你真机的架构要一致 当然这个肯定是要满足的 但是还有最重要的一点就是 这个文件夹的名字也很重要哦 Jni文件夹和jniLibs文件夹,如果是so的话,一定要放在jniLibs文件夹里哦 来源: https://www.cnblogs.com/fengfenghuifei/p/11793368.html

Opencv ANN_MLP training in Android

匿名 (未验证) 提交于 2019-12-03 10:24:21
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have implemented an ANN character classifier in opencv C++. I have created a model: cv::Mat layers(3,1,CV_32S); layers.at<int>(0,0) = ATTRIBUTES;//400 layers.at<int>(1,0)=25;//hidden layer layers.at<int>(2,0) =CLASSES;// eg. 10 CvANN_MLP nnetwork(layers, CvANN_MLP::SIGMOID_SYM,0.6,1); CvANN_MLP_TrainParams params( cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 1000, 0.000001), CvANN_MLP_TrainParams::BACKPROP, 0.1, 0.1); int iterations = nnetwork.train(training_set, training_set_classifications,cv::Mat(),cv::Mat(),params); CvFileStorage*

JNI call convert jstring to char*

匿名 (未验证) 提交于 2019-12-03 09:18:39
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I my cpp code contains a jni function that i wish to convert to const char*. This is the code i am using extern "C" { void Java_com_sek_test_JNITest_printSomething(JNIEnv * env, jclass cl, jstring str) { const char* mystring = env->GetStringUTFChars(env, str, 0); PingoScreen::notify(); } I get an error that no matching function for call to '_JNIEnv::GetStringUTFChars(JNIEnv*&, _jstring*&, int) What am i doing wrong ? 回答1: According to the documentation, GetStringUTFChars const jbyte* GetStringUTFChars(JNIEnv *env, jstring string, jboolean

JNI code crash with core dump when throw is called under 64 bit

匿名 (未验证) 提交于 2019-12-03 09:14:57
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm working with java + c++ ( using JNI ) and i must load own native libraries but application failed with core dump when throw is called lthough the code is surrounded by an exception handler (see code below). It's the same code working without problem on linux 64bit, sparc 64bit and i386 32bit. The problem occurred when trying to run an application under java 8 on intel SunOs. The flag -m64 has been added to the Makefile and library was added to the LD_PRELOAD_64 and LD_LIBRARY_PATH_64 has been set correctly. The java starting,

Android JNI bridge Toast C++ not working - How to fix it?

匿名 (未验证) 提交于 2019-12-03 09:14:57
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to solve a little problem with my hpp unit. I found some examples on the internet showing how to Toast an information on screen with an Android JNI bridge, but it was just in pascal (delphi), so I decided to use the same pas file but with it an hpp file for conversion. Till now I got it: #ifndef Android_Jni_ToastHPP #define Android_Jni_ToastHPP #pragma delphiheader begin #pragma option push #pragma option -w- // All warnings off #pragma option -Vx // Zero-length empty class member #pragma pack(push,8) #include <FMX.Helpers.Android

Android JNI - Reliable way to convert jstring to wchar_t

匿名 (未验证) 提交于 2019-12-03 09:06:55
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In my Android JNI code, I need to convert jstring to wchar_t. The closest reference I found was How do I convert jstring to wchar_t * . One can obtain jchar* and the length using the following code: const jchar *raw = env->GetStringChars(string, 0); jsize len = env->GetStringLength(string); wchar_t* wStr = new wchar_t[len+1]; It seems I cannot use wcncpy to copy "raw" into "wStr." Although jchar is 2-bytes long, wchar_t is 4 bytes long on all modern versions of Android OS. One option is to copy one character at a time in a for loop: for(int

Call to C++ JNI NewStringUTF crashes android app when using many different kinds of emoji and languages (beyond ascii, but still valid modified utf-8)

匿名 (未验证) 提交于 2019-12-03 09:06:55
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to solve a Cocos2d-x Keyboard input crash on Android 5.x when I create CCImage from the text with many emoji found on the keyboard (some work though, but most don't.) On Android 4.x several of the devices just display mangled text/extra characters. The source of the crash is the JNI's NewStringUTF() call. It simply does not support all of the 2, 3 and 4 byte utf-8 characters in Android 5/Lollipop. This crash happens on cocos2d-x v2.2.6 (and confirmed on 3.x) using NDK 10e with Toolchain 4.8 (not sure if any of that makes too much

Android JNI get Two Fields from Java class

匿名 (未验证) 提交于 2019-12-03 08:54:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Before I post any code or anything, let me explain what I'm trying to accomplish: what I'm trying to do is get load shaders from an assets folder, and pass an array of PongDroidShader objects to C++ so it can compile and load them. In my Java class, I have a function which goes through each shader file in the shaders folder and parses it loads it into a String object, while also checking for what kind of shader type it is via its file extension (.e.g, a vertex would have a .vert extension). Once I've accomplished that, I then send