package com.basic.security.utils;
|
|
import android.graphics.Bitmap;
|
|
import com.basic.security.manager.BaseSettingManager;
|
import com.basic.security.manager.HintRecognizeManager;
|
import com.google.gson.Gson;
|
|
import java.io.ByteArrayOutputStream;
|
import java.io.Serializable;
|
|
public class FacePosition implements Serializable {
|
public static Gson gson = new Gson();
|
public int x1;
|
public int y1;
|
public int x2;
|
public int y2;
|
public int sx1;
|
public int sy1;
|
public int sx2;
|
public int sy2;
|
public int yaw; // 左右角度
|
public int pitch; //
|
public int roll; //
|
public float detectScore;
|
public byte[] faceJpgData1;
|
public long trackerId;
|
public boolean liveness;
|
public String featureName;
|
public byte[] featureData;
|
public long createTime;
|
public boolean allowPass;
|
public String allowPassIdentityName;
|
public double faceRatio;
|
public double sfaceRatio;
|
public FaceTitleAndTips compareFeatureResult;
|
public DetectedResult detectedResult;
|
public Bitmap faceBitmap;
|
BitmapHolder bitmapHolder = new BitmapHolder();
|
private byte[] faceJpgData;
|
|
public byte[] faceJpgData() {
|
Bitmap frameBitmap = detectedResult.getFrameBitmap();
|
if (faceJpgData == null) {
|
cropImage();
|
}
|
return faceJpgData;
|
}
|
|
private byte[] cropImage() {
|
float ratio = 0.8f;
|
int enlargeWidth = (int) (ratio * (this.x2 - this.x1));
|
int enlargeHeight = (int) (ratio * (this.y2 - this.y1));
|
int x1 = this.x1 - enlargeWidth / 2;
|
int x2 = this.x2 + enlargeWidth / 2;
|
int y1 = this.y1 - enlargeHeight / 2;
|
int y2 = this.y2 + enlargeHeight / 2;
|
if (x1 < 0) {
|
x1 = 0;
|
}
|
if (y1 < 0) {
|
y1 = 0;
|
}
|
if (x2 > detectedResult.width) {
|
x2 = detectedResult.width;
|
}
|
if (y2 > detectedResult.height) {
|
y2 = detectedResult.height;
|
}
|
int width = (x2 - x1);
|
int height = (y2 - y1);
|
if (width > detectedResult.width) {
|
}
|
if (height > detectedResult.height) {
|
}
|
Bitmap bitmap = detectedResult.getFrameBitmap();
|
byte[] faceData = null;
|
if (bitmap != null) {
|
bitmapHolder.storeBitmap(bitmap);
|
bitmapHolder.cropBitmap(x1, y1, x2, y2);
|
faceBitmap = bitmapHolder.getBitmapAndFree();
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
faceBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
|
faceData = stream.toByteArray();
|
// if (faceBitmap != null && !faceBitmap.isRecycled()) {
|
// faceBitmap.recycle();
|
// }
|
// faceBitmap = null;
|
// bitmap = null;
|
}
|
faceJpgData = faceData;
|
return faceData;
|
}
|
|
public boolean detectScoreCanRecognize() { // 已注册人员是否识别出来
|
return featureData != null && detectScore >= 0.80;
|
}
|
|
public boolean detectScoreCanSignUp() { // 得分是否可以注册
|
boolean detectScoreCanSignUp = false;
|
// System1.out.println("FacePosition.detectScoreCanSignUp score="+score);
|
if (Constants.needIdCardModule) {
|
if (Constants.isNewIDCardReader) {
|
detectScoreCanSignUp = haveFaceAngleHintForSignUp() == null && detectScore >= 0.80;
|
System1.out.println("FacePosition.detectScoreCanSignUp 1 detectScoreCanSignUp=" + detectScoreCanSignUp);
|
return detectScoreCanSignUp;
|
}
|
}
|
// detectScoreCanSignUp = haveFaceAngleHintForSignUp() == null && detectScore >= 0.95 && featureData != null;
|
detectScoreCanSignUp = haveFaceAngleHintForSignUp() == null && detectScore >= 0.8 && featureData != null;
|
if (detectScoreCanSignUp) {
|
// System1.out.println("FacePosition.detectScoreCanSignUp detectScoreCanSignUp="+detectScoreCanSignUp + ", && featureData =" + featureData);
|
}
|
// System1.out.println("FacePosition.detectScoreCanSignUp 2 detectScoreCanSignUp="+detectScoreCanSignUp + " detectScore >= 0.95 " + detectScore);
|
return detectScoreCanSignUp;
|
}
|
|
public boolean detectScoreCanSignUp1() { // 得分是否可以注册
|
String haveFaceAngleHintForSignUp = haveFaceAngleHintForSignUp();
|
// System1.out.println("FacePosition.detectScoreCanSignUp score="+score);
|
if (Constants.needIdCardModule) {
|
if (Constants.isNewIDCardReader) {
|
return haveFaceAngleHintForSignUp == null && detectScore >= 0.80;
|
}
|
}
|
boolean isKnownPerson = false;
|
if (compareFeatureResult != null && compareFeatureResult.person != null) {
|
isKnownPerson = true;
|
}
|
boolean detectScoreCanSignUp =
|
isKnownPerson || (haveFaceAngleHintForSignUp == null &&
|
detectScore >= 0.8 && featureData != null);
|
if (!detectScoreCanSignUp && haveFaceAngleHintForSignUp != null) {
|
// System1.out.println("FacePosition.detectScoreCanSignUp1 haveFaceAngleHintForSignUp=" + haveFaceAngleHintForSignUp);
|
}
|
return detectScoreCanSignUp;
|
}
|
|
public void readFaceFeature() {
|
if (featureData == null) {
|
try {
|
if (featureName != null && !"".equals(featureName)) {
|
featureData = FileUtil.readFile1(featureName);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
public String haveFaceAngleHintForSignUp() {
|
String haveFaceAngleHint = null;
|
if (Constants.isYuYin) {
|
if (detectScore > 80) {
|
return null;
|
}
|
}
|
try {
|
// BaseSettingManager.getFaceRatio() 根据设置的屏幕比,让人脸在设置的屏幕比+_0.2浮动
|
if (faceRatio > Double.parseDouble(BaseSettingManager.getFaceRatio()) + 0.2) {
|
// haveFaceAngleHint = HintRecognizeManager.getFaceMaxAngleMessage();
|
haveFaceAngleHint = "向后站";
|
} else if (faceRatio < Double.parseDouble(BaseSettingManager.getFaceRatio()) - 0.2) {
|
// haveFaceAngleHint = HintRecognizeManager.getFaceMinAngleMessage();
|
haveFaceAngleHint = "向前站";
|
} else if (yaw > HintRecognizeManager.getRightRotationAngle()) { //
|
haveFaceAngleHint = HintRecognizeManager.getRightRotationMessage();
|
} else if (yaw < HintRecognizeManager.getLeftRotationAngle()) {
|
haveFaceAngleHint = HintRecognizeManager.getLeftRotationMessage();
|
} else if (pitch > HintRecognizeManager.getHeadDownAngle()) { //
|
haveFaceAngleHint = HintRecognizeManager.getHeadUpAngleMessage();
|
} else if (pitch < HintRecognizeManager.getHeadUpAngle()) {
|
haveFaceAngleHint = HintRecognizeManager.getHeadDownAngleMessage();
|
} else if (roll > 10) {
|
haveFaceAngleHint = "请正脸对准摄像头";
|
} else if (roll < -10) {
|
haveFaceAngleHint = "请正脸对准摄像头";
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return haveFaceAngleHint;
|
}
|
|
@Override
|
public String toString() {
|
return gson.toJson(this);
|
}
|
}
|