zhangzengfei
2022-01-10 4496b59ab27d569df1da7ef634e02273b3a9618a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.basic.security.utils;
 
import android.graphics.Bitmap;
 
import com.basic.security.manager.BaseSettingManager;
import com.basic.security.manager.HintRecognizeManager;
 
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
 
public class FacePosition implements Serializable {
    public int x1;
    public int y1;
    public int x2;
    public int y2;
    public int yaw; // 左右角度
    public int pitch; //
    public int roll; //
    public float score;
    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 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 * (x2 - x1));
        int enlargeHeight = (int) (ratio * (y2 - y1));
        x1 = x1 - enlargeWidth / 2;
        x2 = x2 + enlargeWidth / 2;
        y1 = y1 - enlargeHeight / 2;
        y2 = 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) {
            width = detectedResult.width;
        }
        if (height > detectedResult.height) {
            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 && score >= 0.80;
    }
 
 
    public boolean detectScoreCanRecognize1() { // 已注册人员是否识别出来
        return score >= 0.80;
    }
 
    public boolean detectScoreCanSignUp() { // 得分是否可以注册
//        System.out.println("FacePosition.detectScoreCanSignUp score="+score);
        if (Constants.isNewIDCardReader) {
            boolean b = haveFaceAngleHintForSignUp() == null && score >= 0.80;
//            System.out.println("FacePosition.detectScoreCanSignUp 1 " + b + " " + score);
            return b;
        }
        boolean detectScoreCanSignUp = haveFaceAngleHintForSignUp() == null && score >= 0.95 && featureData != null;
        if (detectScoreCanSignUp) {
            System.out.println("FacePosition.detectScoreCanSignUp detectScoreCanSignUp=" + detectScoreCanSignUp + ", && featureData =" + featureData);
        }
//        System.out.println("FacePosition.detectScoreCanSignUp 1 " + detectScoreCanSignUp + " " + score);
        return detectScoreCanSignUp;
    }
 
    public void readFaceFeature() {
        try {
            if (featureName != null && !"".equals(featureName)) {
                featureData = FileUtil.readFile1(featureName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public String haveFaceAngleHintForSignUp() {
        String haveFaceAngleHint = 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;
    }
}