net.facelib.mtfsdk.V1AndroidBridge Maven / Gradle / Ivy
package net.facelib.mtfsdk;
import net.facelib.jni.BaseJniBridge;
import net.facelib.jni.SdkInitException;
import net.facelib.jni.SdkRuntime.RuntimeParam;
import net.facelib.jni.SdkRuntimeException;
import net.gdface.utils.Assert;
import net.gdface.utils.SimpleLog;
import static net.facelib.mtfsdk.MtfsdkConstant.*;
import static net.facelib.mtfsdk.V1LicenseManager.V1_LICENSE_MANAGER;
import net.facelib.akcore.LicenseManager;
import net.facelib.akcore.ProductInitStatus;
/**
* MTFSDK for android/arm JNI 接口
* 此类为非线程安全的,不可多线程共享调用,需要在外部进一步封装实现线程安全
* @author guyadong
*
*/
public class V1AndroidBridge extends BaseJniBridge{
public static final String SDK_VERSION = "MTFSDKARM512";
static final String V1_PRODUCT_ID = "MTFSDKARM512_android";
final long[] featureHandle = new long[1];
/**
* 初始化状态
*/
private ProductInitStatus status=ProductInitStatus.SDK_UNINITIALIZED;
static {
try {
// 加载算法动态库
System.loadLibrary("FS_AndroidFeaV1SDK");
} catch (Exception e) {
SimpleLog.log(e.getMessage());
throw new ExceptionInInitializerError(e);
}
}
public V1AndroidBridge() {
}
/**
* 返回当前SDK初始化状态
* @return SDK初始化状态
*/
protected ProductInitStatus getStatus() {
return status;
}
/**
* 执行SDK(识别模块)初始化
* @param licenseFile 授权文件路径
* @return 初始化状态
*/
static ProductInitStatus ffInit(String licenseFile, V1AndroidBridge instance) {
if(ProductInitStatus.SDK_INIT_OK == instance.status){
return instance.status;
}
int ffFlag = FFInit(
"\0".getBytes(),
"\0".getBytes(),
licenseFile.getBytes(),
"\0",
instance.featureHandle);
return ProductInitStatus.jniCode(ffFlag);
}
@Override
protected void sdkInit(String licenseFile) throws SdkInitException {
if(ProductInitStatus.SDK_INIT_OK == status){
return;
}
ProductInitStatus ffStatus = ProductInitStatus.SDK_UNINITIALIZED;
try{
ffStatus = ffInit(licenseFile, this);
if(ffStatus !=ProductInitStatus.SDK_INIT_OK){
status = ffStatus;
SimpleLog.log("feature module: {}", status.msg);
throw new SdkInitException(status);
}
// 检测模块和识别模块都初始化成功则置状态为成功
status = ProductInitStatus.SDK_INIT_OK;
}finally {
// 如果没有完全初始化成功,则destroy已经初始化模块
if(status != ProductInitStatus.SDK_INIT_OK){
if(ffStatus !=ProductInitStatus.SDK_INIT_OK){
FFDestroy(featureHandle[0]);
}
}
}
}
/**
* 人脸识别SDK资源释放
* @see #FFDestroy(long)
*/
@Override
protected void destroy() {
if(ProductInitStatus.SDK_INIT_OK == status){
status = ProductInitStatus.SDK_UNINITIALIZED;
FFDestroy(featureHandle[0]);
}
}
@Override
protected LicenseManager licenseManager() {
return BaseJniBridge.getLicenseManager(V1_PRODUCT_ID, V1_LICENSE_MANAGER);
}
public byte[] feaExtractByte(byte[] BGR, int width, int height, double[] rect){
byte[] buffer = new byte[FEATURE_BYTES];
int ret = FFFeaExtractByte(featureHandle[0], BGR, width, height, buffer, rect);
if(ret<0){
throw new SdkRuntimeException(ProductInitStatus.jniCode(ret));
}
return buffer;
}
@Override
public V1AndroidBridge setRuntimeParam(RuntimeParam name,Object value) throws SdkRuntimeException{
Assert.notNull(name, "name");
switch (name) {
case featureThreadNumber:
int featureThreadNumber = value != null ? (Integer) value : MtfAndroidConfigProvider.DEFAULT_FEATURE_THREAD_NUMBER;
if(featureThreadNumber > 0){
int code = FFSetThreadsNumber(featureHandle[0], featureThreadNumber);
if(code != 0){
status = ProductInitStatus.jniCode(code);
throw new SdkRuntimeException(status);
}
}
break;
default:
throw new IllegalArgumentException(SimpleLog.logString("INVALID parameter name: {}",name));
}
return this;
}
////////////////////////////////////////特征提取系列///////////////////////////////////////////
/**
* 人脸识别初始化函数
* @param licenseCode 授权码
* @param licenseKey 授权关键字
* @param path 授权码文件本地路径
* @param faceDetectionModelPath 模型路径
* @param handle [out]句柄
* @return 成功(0)或错误代码(< 0),see also {@link ProductInitStatus}
*/
private static native int FFInit(byte[] licenseCode, byte[] licenseKey, byte[] path, String faceDetectionModelPath, long[] handle);
/**
* 人脸识别模块资源释放函数
* @param handle 句柄
*/
static native void FFDestroy(long handle);
/**
* 对输入图像中检测到的人脸提取特征
* @param handle 操作句柄
* @param BGR 待检测图像BGR格式
* @param width 图像宽度
* @param height 图像高度
* @param fea [out] 人脸特征 空间在外部申请,长度4096
* @param rect 人脸位置及关键点信息 由人脸检测SDK 产生,参见 {@link #FDDetect(long, byte[], int, int, int, double[])}
* @return 成功(0)或错误代码(< 0),see also {@link ProductInitStatus}
*/
private static native int FFFeaExtractByte(long handle, byte[] BGR, int width, int height, byte[] fea, double[] rect);
/**
* 人脸相似度比对函数
* 对人脸特征feaA和feaB进行相似度比对,返回相似度结果
* @param feaA 1024 bytes
* @param feaB 1024 bytes
* @return 相似度(0.0~1.0)
*/
public static native double FFSimilarityByte(byte[] feaA, byte[] feaB);
/**
* @return 人脸识别模块版本
*/
public static native String FFgetVersion();
private static native int FFSetThreadsNumber(long handle, int threadsNumbers);
// public static native int FDDetectMaxFaceTrack(long handle, byte[] BGR, int width, int height, double[] rect); //直接返回61个元素的人脸信息
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy