JNI与Boolean和boolean

17 篇文章 0 订阅

JNI里如果定义了一个返回值为jboolean型的函数,那么java端用Boolean是不行的,要用boolean才行


/**
 * 初始化seetaface人脸识别库
 * 参数:
 * vFaceModelDir:人脸识别模型文件所在目录,如/sdcard/
 */
JNIEXPORT jboolean JNICALL Java_seetaface_SeetaFace_init(
		JNIEnv *env, jobject obj, jstring vFaceModelDir)
{
	jboolean tRet = false;
	if(NULL == vFaceModelDir) {
		return tRet;
	}
	
	...
	tRet = true;
	return tRet; 
}


像这样就不行,运行的时候直接就 报【很抱歉,"xxx"已停止运行】

//初始化so库,告诉底层人脸识别模型文件的目录
public native Boolean init(String vModelDir);
	


要这样才行:

//初始化so库,告诉底层人脸识别模型文件的目录
public native boolean init(String vModelDir);




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例代码,演示如何在Java中使用JNI读取CPU和硬盘序列号,并使用它们进行软件加密。 1. 创建一个Java类,例如CryptoUtils,其中包括以下本地方法声明: ``` public class CryptoUtils { public native String getCPUSerial(); public native String getHardDiskSerial(); public native boolean validateLicense(String licenseKey); } ``` 2. 使用javah命令生成头文件 在命令行中进入CryptoUtils类所在的目录,然后输入以下命令: ``` javah -classpath <path to your class files> CryptoUtils ``` 这将在该目录中生成一个名为CryptoUtils.h的头文件。 3. 实现本地方法 在CryptoUtils.c文件中实现本地方法。以下是一个例子: ``` #include "CryptoUtils.h" #include <stdio.h> #include <windows.h> #include <stdlib.h> #include <string.h> JNIEXPORT jstring JNICALL Java_CryptoUtils_getCPUSerial(JNIEnv *env, jobject obj) { char buf[1024]; DWORD dwBufSize = 1024; GetVolumeInformation("C:\\", NULL, NULL, (LPDWORD)&dwVolumeSerialNumber, NULL, NULL, NULL, NULL); sprintf_s(buf, "CPU Serial: %d", dwVolumeSerialNumber); return (*env)->NewStringUTF(env, buf); } JNIEXPORT jstring JNICALL Java_CryptoUtils_getHardDiskSerial(JNIEnv *env, jobject obj) { char buf[1024]; DWORD dwVolumeSerialNumber; GetVolumeInformation("C:\\", NULL, NULL, (LPDWORD)&dwVolumeSerialNumber, NULL, NULL, NULL, NULL); sprintf_s(buf, "Hard Disk Serial: %d", dwVolumeSerialNumber); return (*env)->NewStringUTF(env, buf); } JNIEXPORT jboolean JNICALL Java_CryptoUtils_validateLicense(JNIEnv *env, jobject obj, jstring licenseKey) { const char *key = (*env)->GetStringUTFChars(env, licenseKey, 0); // 根据CPU和硬盘序列号生成加密密钥,并与licenseKey进行比较 // 如果匹配,则返回true,否则返回false if (strcmp(key, "encrypted_key") == 0) { return JNI_TRUE; } else { return JNI_FALSE; } } ``` 4. 编译C代码 在命令行中进入CryptoUtils.c所在的目录,然后输入以下命令: ``` gcc -shared -o CryptoUtils.dll CryptoUtils.c -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" ``` 这将生成一个名为CryptoUtils.dll的动态链接库。 5. 在Java中使用本地方法 在Java中使用本地方法之前,需要将动态链接库加载到Java虚拟机中。可以使用System.loadLibrary方法实现。例如: ``` public class Main { static { System.loadLibrary("CryptoUtils"); } public static void main(String[] args) { CryptoUtils utils = new CryptoUtils(); String cpuSerial = utils.getCPUSerial(); String hardDiskSerial = utils.getHardDiskSerial(); String licenseKey = generateLicenseKey(cpuSerial, hardDiskSerial); boolean isValid = utils.validateLicense(licenseKey); if (isValid) { System.out.println("License is valid"); } else { System.out.println("License is invalid"); } } private static String generateLicenseKey(String cpuSerial, String hardDiskSerial) { // 根据CPU和硬盘序列号生成加密密钥 // ... return "encrypted_key"; } } ``` 这将在控制台输出“License is valid”或“License is invalid”,具体取决于生成的加密密钥是否与validateLicense方法中传递的licenseKey匹配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值