package com.basic.security.utils;
|
|
import android.app.Activity;
|
import android.content.ContextWrapper;
|
import android.graphics.Bitmap;
|
import android.graphics.BitmapFactory;
|
import android.util.Base64;
|
|
import com.basic.security.base.BaseApplication;
|
import com.basic.security.manager.BaseManager;
|
import com.basic.security.manager.DeviceManager;
|
import com.basic.security.manager.DirManager;
|
import com.basic.security.manager.IdCardManager;
|
import com.basic.security.manager.SystemInitManager;
|
import com.basic.security.model.ModelAdapter;
|
import com.basic.security.utils.socket.outdoor.OutdoorGuestSocketServer;
|
import com.basic.security.utils.socket.server.TableRowReceiveSocketServer;
|
import com.basic.security.widget.Preview;
|
|
|
public class FaceId {
|
public static FaceId instance = new FaceId();
|
public static Activity activity = null;
|
|
static {
|
// System.loadLibrary("firefly_api");
|
System.loadLibrary("opencv_java3");
|
// System.loadLibrary("libjpeg");
|
System.loadLibrary("THFaceImage");
|
// System.loadLibrary("THFacialPos");
|
System.loadLibrary("native-lib");
|
System.loadLibrary("THFeature");
|
}
|
|
public boolean sdkInitSuccess = false;
|
|
public static void initSdk(Activity activity, Preview rgbPreview, Preview grayPreview, DetectListener detectListener) {
|
String ip = NetUtil.getIPAddress(true);
|
Constants.deviceId = DeviceManager.getDeviceId();
|
FaceId.activity = activity;
|
AssetHelper.copyAssets(activity.getAssets(), new ContextWrapper(activity).getFilesDir().getAbsolutePath());
|
DirManager.mkdirs();
|
SystemInitManager.initTablesAndDefaultValues();
|
|
if (Constants.isOutdoor) {
|
OutdoorGuestSocketServer.startServer();
|
}
|
TableRowReceiveSocketServer.startServer();
|
|
if (!Constants.stopCameraAndVideo) {
|
if (rgbPreview != null) {
|
rgbPreview.init(activity, Constants.RGB_CAMERA, detectListener);
|
}
|
if (grayPreview != null && Constants.USE_GRAY_CAMERA) {
|
grayPreview.init(activity, Constants.GRAY_CAMERA, detectListener);
|
}
|
}
|
try {
|
// FaceId.instance.initSdk(new ContextWrapper(BaseApplication.getApplication().activity).getFilesDir().getAbsolutePath());
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
ProcessImageAndDrawResults.addDetectedListener(detectListener);
|
}
|
|
public static void testSdk(String jpgFile1, String jpgFile2) {
|
BaseApplication.getApplication().detectLock.lock();
|
Bitmap bitmap1 = BitmapFactory.decodeFile(jpgFile1);
|
if (bitmap1 == null) {
|
BaseApplication.getApplication().detectLock.unlock();
|
return;
|
}
|
byte[] byte1 = IdCardManager.getPixelsBGR(bitmap1);
|
byte[] featureData1 = FaceId.instance.extractFeature1(byte1, bitmap1.getWidth(), bitmap1.getHeight());
|
if (featureData1 == null) {
|
BaseApplication.getApplication().detectLock.unlock();
|
return;
|
}
|
String base64 = Base64.encodeToString(featureData1, Base64.NO_WRAP);
|
ModelAdapter person1 = new ModelAdapter("p1");
|
person1.setString("table", "person");
|
person1.setString("camera_image_feature", base64);
|
BaseManager.save(person1);
|
ModelAdapter dbPerson1 = BaseManager.findById("person", "p1");
|
featureData1 = Base64.decode(dbPerson1.getString("camera_image_feature"), Base64.NO_WRAP);
|
Bitmap bitmap2 = BitmapFactory.decodeFile(jpgFile2);
|
if (bitmap2 == null) {
|
BaseApplication.getApplication().detectLock.unlock();
|
return;
|
}
|
byte[] byte2 = IdCardManager.getPixelsBGR(bitmap2);
|
byte[] featureData2 = FaceId.instance.extractFeature1(byte2, bitmap2.getWidth(), bitmap2.getHeight());
|
if (featureData2 == null) {
|
BaseApplication.getApplication().detectLock.unlock();
|
return;
|
}
|
String base642 = Base64.encodeToString(featureData2, Base64.NO_WRAP);
|
ModelAdapter person2 = new ModelAdapter("p2");
|
person2.setString("table", "person");
|
person2.setString("camera_image_feature", base642);
|
BaseManager.save(person2);
|
ModelAdapter dbPerson2 = BaseManager.findById("person", "p2");
|
featureData2 = Base64.decode(dbPerson2.getString("camera_image_feature"), Base64.NO_WRAP);
|
float score = FaceId.instance.compareFeature(featureData1, featureData2);
|
System.out.println("testSdk=" + score);
|
BaseApplication.getApplication().detectLock.unlock();
|
}
|
|
public native boolean initSdk(String modelPath);
|
|
public native void detectFace(DetectedResult detectedResult);
|
|
public native String rgbDetectFace(String modelPath, String rgbFileName, int width, int height, String baseFeatureName, int shouldExtractFeature, boolean useGrayCamera, int detectFaceCount);
|
|
public native String rgbDetectFace2(String modelPath, String rgbFileName, int width, int height, String baseFeatureName, int shouldExtractFeature, boolean useGrayCamera, int detectFaceCount, byte[] bgrArray);
|
|
public native String rgbDetectFace4(int channel, String modelPath, String rgbFileName, int width, int height, String baseFeatureName, int shouldExtractFeature, boolean useGrayCamera, int detectFaceCount, byte[] bgrArray);
|
|
public native String rgbDetectFace3(int channel, String modelPath, int width, int height, String baseFeatureName,
|
int shouldExtractFeature, boolean useGrayCamera, int detectFaceCount, byte[] nv21Array);
|
|
public native String grayDetectFace(String modelPath, String rgbFileName, int width, int height, int detectFaceCount);
|
|
public native void cropFace(String frameJpgPath, String faceJpgPath, int width, int height, int x1, int y1, int x2, int y2);
|
|
// 返回/baseFeatureName/feature1,/baseFeatureName/feature2. 逗号分隔, 用于身份证
|
public native byte[] extractFeature(String jpgFileName);
|
|
public native String facePosition(String jpgFileName);
|
|
public native byte[] extractFeature1(byte[] bgrData, int width, int height);
|
|
public native String compareFeature(String featureFileName1, String featureFileName2);
|
|
public native float compareFeature(byte[] featureBuffer1, byte[] featureBuffer2);
|
|
// 返回
|
public native String compareFeatureInDb(String featureFileName);
|
|
// 2
|
public native String compareFeatureInDb(byte[] featureBuffer, int minScore);
|
|
// 1 3
|
public native void addFeatureToDb(String id, byte[] featureBuffer);
|
|
public native void removeFeatureFromDb(String id);
|
|
public native void addFeatureToTempDb(String id, byte[] featureBuffer);
|
|
public native String compareFeatureInTempDb(byte[] featureBuffer, int minScore);
|
|
public native void removeFeatureFromTempDb(String id);
|
|
public native String receiveBroadcast(char broadcastPort);
|
|
public native byte[] yuv420p2rgb24(byte[] yuvbuffer, byte[] rgbbuffer, int width, int height);
|
}
|