net.facelib.mtfsdk.FaceApiMtfV2Android Maven / Gradle / Ivy
package net.facelib.mtfsdk;
import static net.facelib.mtfsdk.V2AndroidBridge.V2_PRODUCT_ID;
import static net.facelib.mtfsdk.V2AndroidBridge.V2_LICENSE_MANAGER;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import net.facelib.akcore.LicenseManager;
import net.facelib.akcore.ProductInitStatus;
import net.facelib.jni.BaseJniBridge;
import net.facelib.jni.BridgePool;
import net.facelib.jni.FacelibRuntimeParam;
import net.gdface.image.MatType;
import net.gdface.sdk.CodeInfo;
import net.gdface.sdk.ContextLoader.ContextField;
/**
* MTFSDK V2 android/arm平台 {@link net.gdface.sdk.FaceApi}接口实现抽象类(线程安全)
* @author guyadong
*
*/
public class FaceApiMtfV2Android extends BaseFaceApiMtfAndroid{
/**
* 资源池参数配置
*/
private static GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
public static final FaceApiMtfV2Android INSTANCE = new FaceApiMtfV2Android();
static final Map CONTEXT;
/**
* 特征提取算法实例资源
*/
private final BridgePool poolWrap = new BridgePool<>(V2AndroidBridge.class,poolConfig);
protected FaceApiMtfV2Android() {
super();
capacity.put(C_SDK_VERSION, V2AndroidBridge.SDK_VERSION);
}
public static FaceApiMtfV2Android getInstance(){
return INSTANCE;
}
/**
* 获取授权管理对象
* @return 授权管理对象
*/
public LicenseManager getLicenseManager(){
return BaseJniBridge.getLicenseManager(V2_PRODUCT_ID, V2_LICENSE_MANAGER);
}
static {
try {
// MTFSDK JNI接口静态区初始化
Class.forName(net.facelib.mtfsdk.V2AndroidBridge.class.getName());
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
HashMap m = new HashMap<>();
m.put(ContextField.INSTANCE, getInstance());
m.put(ContextField.LICENCE_MANAGER, BaseJniBridge.getLicenseManager(V2_PRODUCT_ID, V2_LICENSE_MANAGER));
CONTEXT = Collections.unmodifiableMap(m);
}
@Override
protected double nativeCompareCode(byte[] code1, byte[] code2) {
return V2AndroidBridge.FFSimilarityByteV2(code1,code2);
}
@Override
protected byte[] nativeGetFaceFeature(MatType matType,byte[] imgMatrix, int width, int height, CodeInfo faceInfo) {
NativeFaceInfo nativeFaceInfo = NativeFaceInfo.toNative(faceInfo);
V2AndroidBridge instance = poolWrap.applyInstance();
try{
sdkRuntime.setInstanceParam(instance,FacelibRuntimeParam.featureThreadNumber.name());
return instance.feaExtractByte(matType, imgMatrix, width, height, nativeFaceInfo.nativeData);
}finally{
poolWrap.returnInstance(instance);
}
}
@Override
public void bindNativeFeatureInstanceForCurentThread() {
poolWrap.bindInstanceForCurentThread();
}
@Override
public void unbindNativeFeatureInstanceForCurentThread() {
poolWrap.unbindInstanceForCurentThread();
}
/**
* 测试是否可以正常初始化特征提取算法实例
* @param initMills [out]如果不为{@code null}返回返回初始化耗时(毫秒)
* @return 产品初始化状态
*/
public ProductInitStatus testV2Init(AtomicLong initMills) {
return BaseJniBridge.testInit(initMills, V2AndroidBridge.class);
}
/**
* @return 返回V2特征提取算法实例资源池对象
*/
public GenericObjectPool getFeaturePool() {
return poolWrap.getPool();
}
/**
* 返回V2特征提取算法资源池对象参数配置
* @return poolConfig
*/
public static GenericObjectPoolConfig getPoolConfig() {
return poolConfig;
}
/**
* 设置V2特征提取算法资源池对象参数配置
* @param poolConfig 要设置的 poolConfig
*/
public static void setPoolConfig(GenericObjectPoolConfig poolConfig) {
if(null != poolConfig){
FaceApiMtfV2Android.poolConfig = poolConfig;
}
}
}