Commit f4dd7153 authored by José Henrique's avatar José Henrique
Browse files

Switch to megvii impl

parent 5ce4fa0d
cc_library {
name: "faceunlock_dependencies",
srcs: [
"faceunlock_dependencies_dummy.cpp",
],
cpp_std: "experimental",
cflags: [
"-Wall",
"-Werror",
"-Wno-unused-parameter",
],
compile_multilib: "64",
required: [
"libFaceDetectCA",
"libmegface",
"libMegviiUnlock",
"libMegviiUnlock-jni-1.2",
],
}
\ No newline at end of file
int main(int argc, char** argv) {
return 0;
}
\ No newline at end of file
java_library {
name: "faceunlock_framework",
installable: true,
installable: false,
srcs: [
"src/**/*.java",
......
sed -i "s/com.motorola.faceunlock.util.MotoUnlockEncryptor/faceunlock\/util\/FaceUnlockEncryptorDependencyLib/" prebuilt/lib64/libarcsoft-lib.so
sed -i "s/android.os.Build/faceunlock\/Build/" prebuilt/lib64/libarcsoft-lib.so
cc_prebuilt_library_shared {
name: "libarcsoft_faceid",
name: "libFaceDetectCA",
strip: {
none: true,
},
target: {
android_arm64: {
srcs: ["lib64/libarcsoft_faceid.so"],
srcs: ["lib64/libFaceDetectCA.so"],
},
},
compile_multilib: "64",
prefer: true,
product_specific: true,
check_elf_files: false,
}
cc_prebuilt_library_shared {
name: "libarcsoft-lib",
name: "libmegface",
strip: {
none: true,
},
target: {
android_arm64: {
srcs: ["lib64/libarcsoft-lib.so"],
srcs: ["lib64/libmegface.so"],
},
},
compile_multilib: "64",
prefer: true,
product_specific: true,
check_elf_files: false,
}
cc_prebuilt_library_shared {
name: "libarcsoftbase",
name: "libMegviiUnlock",
strip: {
none: true,
},
target: {
android_arm64: {
srcs: ["lib64/libarcsoftbase.so"],
srcs: ["lib64/libMegviiUnlock.so"],
},
},
compile_multilib: "64",
prefer: true,
product_specific: true,
check_elf_files: false,
}
cc_prebuilt_library_shared {
name: "libmpbase",
name: "libMegviiUnlock-jni-1.2",
strip: {
none: true,
},
target: {
android_arm64: {
srcs: ["lib64/libmpbase.so"],
srcs: ["lib64/libMegviiUnlock-jni-1.2.so"],
},
},
compile_multilib: "64",
prefer: true,
product_specific: true,
check_elf_files: false,
}
\ No newline at end of file
}
java_library {
name: "faceunlock_vendor_framework",
installable: false,
srcs: [
"src/**/*.java",
"src/**/*.aidl",
],
aidl: {
local_include_dirs: ["src"],
},
}
\ No newline at end of file
package com.megvii.facepp.sdk;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FeatureRestoreHelper {
private static final int BUFFER_SIZE = 8192;
private static final int RESTORE_IMAGE_SIZE = 144;
private static final String TAG = "FeatureRestoreHelper";
public static final byte[] sMagic = {1, 2, 3, 4, 5, 6, 7, 8};
private UnlockEncryptor mEncryptor;
public void setUnlockEncryptor(UnlockEncryptor unlockEncryptor) {
this.mEncryptor = unlockEncryptor;
}
public void saveRestoreImage(byte[] bArr, String str, int i) {
Log.i(TAG, "saveRestoreImage: length: " + bArr.length + " id " + i);
writeFile(getRestoreFile(str, i).getAbsolutePath(), bArr);
}
public void deleteRestoreImage(String str, int i) {
Log.i(TAG, "deleteRestoreImage: id " + i);
getRestoreFile(str, i).delete();
}
public int restoreAllFeature(String str) {
int i;
File[] listFiles = new File(str).listFiles();
if (listFiles == null || listFiles.length == 0) {
return 24;
}
int i2 = 0;
for (File file : listFiles) {
String name = file.getName();
if (name.startsWith("restore_") && name.length() > 8) {
Log.i(TAG, "restoreAllFeature: " + name);
try {
i = Integer.parseInt(name.substring(8));
} catch (NumberFormatException unused) {
i = -1;
}
if (i != -1) {
byte[] readFile = readFile(file.getAbsolutePath());
Log.i(TAG, "restoreAllFeature: update old feature " + i);
if (restoreFeatureAtPosition(i, readFile) == 0) {
i2++;
}
}
}
}
if (i2 == 0) {
return 24;
}
return 0;
}
private int restoreFeatureAtPosition(int i, byte[] bArr) {
return Lite.getInstance().updateFeature(bArr, RESTORE_IMAGE_SIZE, RESTORE_IMAGE_SIZE, 90, true, new byte[Lite.FEATURE_SIZE], new byte[Lite.IMAGE_SIZE], i);
}
private File getRestoreFile(String str, int i) {
return new File(str, "restore_" + i);
}
private void writeFile(String str, byte[] bArr) {
File file = new File(str);
if (file.exists()) {
file.delete();
}
UnlockEncryptor unlockEncryptor = this.mEncryptor;
int i = 0;
if (unlockEncryptor != null) {
byte[] encrypt = unlockEncryptor.encrypt(bArr);
int length = encrypt.length;
byte[] bArr2 = sMagic;
bArr = new byte[(length + bArr2.length)];
System.arraycopy(bArr2, 0, bArr, 0, bArr2.length);
System.arraycopy(encrypt, 0, bArr, sMagic.length, encrypt.length);
}
int length2 = bArr.length;
try {
FileOutputStream fileOutputStream = new FileOutputStream(str);
while (length2 > i) {
int i2 = length2 - i;
if (i2 > BUFFER_SIZE) {
i2 = BUFFER_SIZE;
}
fileOutputStream.write(bArr, i, i2);
i += i2;
}
} catch (IOException e) {
Log.e(TAG, "writeFile failed", e);
}
}
private byte[] readFile(String str) {
File file = new File(str);
if (!file.exists()) {
return null;
}
int length = (int) file.length();
byte[] bArr = new byte[length];
try {
FileInputStream fileInputStream = new FileInputStream(str);
int i = 0;
while (length > i) {
int i2 = length - i;
if (i2 > BUFFER_SIZE) {
i2 = BUFFER_SIZE;
}
i += fileInputStream.read(bArr, i, i2);
}
if (this.mEncryptor == null || !startWithMagic(bArr)) {
return bArr;
}
byte[] bArr2 = new byte[(bArr.length - sMagic.length)];
System.arraycopy(bArr, sMagic.length, bArr2, 0, bArr2.length);
return this.mEncryptor.decrypt(bArr2);
} catch (IOException e) {
Log.e(TAG, "readFile failed", e);
return bArr;
}
}
private boolean startWithMagic(byte[] bArr) {
if (bArr.length < sMagic.length) {
return false;
}
int i = 0;
while (true) {
byte[] bArr2 = sMagic;
if (i >= bArr2.length) {
return true;
}
if (bArr[i] != bArr2[i]) {
return false;
}
i++;
}
}
}
package com.megvii.facepp.sdk;
import android.annotation.TargetApi;
import android.media.Image;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;
import com.megvii.facepp.sdk.jni.LiteApi;
import java.nio.ByteBuffer;
public class Lite {
public static final int FEATURE_SIZE = 10000;
public static final int IMAGE_SIZE = 40000;
public static final int RESULT_SIZE = 20;
private static Lite sInstance;
private long handle = 0;
private final FeatureRestoreHelper mFeatureRestoreHelper = new FeatureRestoreHelper();
private String mPath;
public enum MGULKPowerMode {
MG_UNLOCK_POWER_NONE,
MG_UNLOCK_POWER_LOW,
MG_UNLOCK_POWER_HIGH
}
public int setConfig(float f, float f2, float f3) {
return 0;
}
public static Lite getInstance() {
if (sInstance == null) {
sInstance = new Lite();
}
return sInstance;
}
public Lite() {
}
public void initHandle(String str, UnlockEncryptor unlockEncryptor) {
initHandle(str);
mFeatureRestoreHelper.setUnlockEncryptor(unlockEncryptor);
}
public void initHandle(String str) {
if (handle == 0) {
handle = LiteApi.nativeInitHandle(str);
mPath = str;
}
}
public int initAll(String str, String str2, byte[] bArr) {
return (int) LiteApi.nativeInitAll(handle, str, str2, bArr);
}
public int initAllWithPath(String str, String str2, String str3) {
return (int) LiteApi.nativeInitAllWithPath(handle, str, str2, str3);
}
public int initLive(String str, String str2) {
return (int) LiteApi.nativeInitLive(handle, str, str2);
}
public int initDetect(byte[] bArr) {
return (int) LiteApi.nativeInitDetect(handle, bArr);
}
public int initDetectWithPath(String str) {
return (int) LiteApi.nativeInitDetectWithPath(handle, str);
}
public int releaseLive() {
return (int) LiteApi.nativeReleaseLive(handle);
}
public int releaseDetect() {
return (int) LiteApi.nativeReleaseDetect(handle);
}
public void release() {
LiteApi.nativeRelease(handle);
handle = 0;
}
public int compare(byte[] bArr, int i, int i2, int i3, boolean z, boolean z2, int[] iArr) {
if (iArr.length < RESULT_SIZE) {
return 1;
}
return LiteApi.nativeCompare(handle, bArr, i, i2, i3, z, z2, iArr);
}
public int compare(byte[] bArr, int i, int i2, int i3, int[] iArr) {
if (iArr.length < RESULT_SIZE) {
return 1;
}
return LiteApi.nativeCompare(handle, bArr, i, i2, i3, false, false, iArr);
}
public int compareMultiImages(MGULKImage[] mGULKImageArr, int[] iArr) {
if (iArr.length < RESULT_SIZE) {
return 1;
}
return LiteApi.nativeCompareMultiImages(handle, mGULKImageArr, iArr);
}
public int saveFeature(byte[] bArr, int i, int i2, int i3, boolean z, byte[] bArr2, byte[] bArr3) {
return updateFeature(bArr, i, i2, i3, z, bArr2, bArr3, 0);
}
public int saveFeature(byte[] bArr, int i, int i2, int i3, byte[] bArr2, byte[] bArr3) {
return updateFeature(bArr, i, i2, i3, true, bArr2, bArr3, 0);
}
public int saveFeature(byte[] bArr, int i, int i2, int i3, boolean z, byte[] bArr2, byte[] bArr3, int[] iArr) {
if ((new StatFs(Environment.getDataDirectory().getPath()).getAvailableBlocksLong()) < 256) {
return 33;
}
if (bArr3.length < 40000 || bArr2.length < 10000) {
return 1;
}
int nativeSaveFeature = LiteApi.nativeSaveFeature(handle, bArr, i, i2, i3, z ? 1 : 0, bArr2, bArr3, iArr);
if (nativeSaveFeature == 0) {
mFeatureRestoreHelper.saveRestoreImage(bArr3, mPath, iArr[0]);
}
return nativeSaveFeature;
}
public int saveFeature(byte[] bArr, int i, int i2, int i3, byte[] bArr2, byte[] bArr3, int[] iArr) {
if (bArr3.length < 40000 || bArr2.length < 10000) {
return 1;
}
int nativeSaveFeature = LiteApi.nativeSaveFeature(handle, bArr, i, i2, i3, 1, bArr2, bArr3, iArr);
if (nativeSaveFeature == 0) {
mFeatureRestoreHelper.saveRestoreImage(bArr3, mPath, iArr[0]);
}
return nativeSaveFeature;
}
public int saveFeatureMultiImages(MGULKImage[] mGULKImageArr, byte[] bArr, byte[] bArr2, int[] iArr) {
int nativeSaveFeatureMultiImages = LiteApi.nativeSaveFeatureMultiImages(handle, mGULKImageArr, bArr, bArr2, iArr);
if (nativeSaveFeatureMultiImages == 0) {
mFeatureRestoreHelper.saveRestoreImage(bArr2, mPath, iArr[0]);
}
return nativeSaveFeatureMultiImages;
}
public int updateFeature(byte[] bArr, int i, int i2, int i3, boolean z, byte[] bArr2, byte[] bArr3, int i4) {
if (bArr3.length < 40000 || bArr2.length < 10000) {
return 1;
}
int nativeUpdateFeature = LiteApi.nativeUpdateFeature(handle, bArr, i, i2, i3, z ? 1 : 0, bArr2, bArr3, i4);
if (nativeUpdateFeature == 0) {
mFeatureRestoreHelper.saveRestoreImage(bArr3, mPath, i4);
}
return nativeUpdateFeature;
}
public int deleteFeature() {
return deleteFeature(0);
}
public int deleteFeature(int i) {
int nativeDeleteFeature = LiteApi.nativeDeleteFeature(handle, i);
mFeatureRestoreHelper.deleteRestoreImage(mPath, i);
return nativeDeleteFeature;
}
public int restoreFeature() {
return mFeatureRestoreHelper.restoreAllFeature(mPath);
}
public int setConfig(float f, float f2, float f3, float f4, boolean z, boolean z2) {
return LiteApi.nativeSetConfig(handle, f, f2, f3, f4, z, z2);
}
public int setConfig(float f, float f2, float f3, float f4) {
return LiteApi.nativeSetConfig(handle, f, f2, f3, f4, false, false);
}
public int setConfig(LiteConfig liteConfig) {
if (liteConfig == null) {
return -1;
}
return LiteApi.nativeSetConfigV2(handle, liteConfig);
}
public int reset() {
return LiteApi.nativeReset(handle);
}
public int prepare(MGULKPowerMode mGULKPowerMode) {
int i = mGULKPowerMode.ordinal();
int i2 = 0;
if (i != 1) {
if (i == 2) {
i2 = 1;
} else if (i == 3) {
i2 = 2;
}
}
return LiteApi.nativePrepareWithPower(handle, i2);
}
public int prepare() {
MGULKPowerMode.MG_UNLOCK_POWER_HIGH.ordinal();
return LiteApi.nativePrepare(handle);
}
public int setDetectArea(int i, int i2, int i3, int i4) {
return LiteApi.nativeSetDetectArea(handle, i, i2, i3, i4);
}
public String getVersion() {
return LiteApi.nativeGetVersion(handle);
}
public int getFeature(byte[] bArr, int i, int i2, int i3, byte[] bArr2) {
if (bArr2.length < 10000) {
return 1;
}
return LiteApi.nativeGetFeature(handle, bArr, i, i2, i3, bArr2);
}
public int compareFeatures(byte[] bArr, float[] fArr, int i, boolean z) {
return LiteApi.nativeCompareFeatures(handle, bArr, fArr, i, z);
}
public int checkFeatureValid(int i) {
return LiteApi.nativeCheckFeatureValid(handle, i);
}
public int getFeatureCount() {
return LiteApi.nativeGetFeatureCount();
}
public long setLogLevel(int i) {
return LiteApi.nativeSetLogLevel(i);
}
public LiteConfig getConfig() {
LiteConfig liteConfig = new LiteConfig(this, null);
LiteApi.nativeGetConfig(handle, liteConfig);
return liteConfig;
}
@TargetApi(21)
public static int image2NV21(Image image, byte[] bArr) {
int readImageIntoBuffer = readImageIntoBuffer(image, bArr);
if (readImageIntoBuffer == 1) {
return 1;
}
revertHalf(bArr);
return readImageIntoBuffer;
}
@TargetApi(21)
private static int readImageIntoBuffer(Image image, byte[] bArr) {
int i;
int i2;
if (image == null) {
Log.e("NULL Image", "image is null");
return 1;
}
int width = image.getWidth();
int height = image.getHeight();
Image.Plane[] planes = image.getPlanes();
int i3 = 0;
for (int i4 = 0; i4 < planes.length; i4++) {
ByteBuffer buffer = planes[i4].getBuffer();
int rowStride = planes[i4].getRowStride();
int pixelStride = planes[i4].getPixelStride();
if (i4 == 0) {
i = width;
} else {
i = width / 2;
}
if (i4 == 0) {
i2 = height;
} else {
i2 = height / 2;
}
if (pixelStride == 1 && rowStride == i) {
int i5 = i * i2;
buffer.get(bArr, i3, i5);
i3 += i5;
} else {
byte[] bArr2 = new byte[rowStride];
int i6 = i3;
int i7 = 0;
while (i7 < i2 - 1) {
buffer.get(bArr2, 0, rowStride);
int i8 = i6;
int i9 = 0;
while (i9 < i) {
bArr[i8] = bArr2[i9 * pixelStride];
i9++;
i8++;
}
i7++;