IDetection.java
5.59 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.megvii.idcardlib.util;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import com.megvii.idcardlib.R;
import com.megvii.livenessdetection.Detector;
import com.megvii.livenessdetection.Detector.DetectionType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
/**
* 实体验证工具类
*/
public class IDetection {
private View rootView;
private Context mContext;
public View[] mAnimViews;
private int num = 3;// 动作数量
private HashMap<Integer, Drawable> mDrawableCache;
public int mCurShowIndex = -1;// 现在底部展示试图的索引值
public ArrayList<Detector.DetectionType> mDetectionSteps;// 活体检测动作列表
public IDetection(Context mContext, View view) {
this.mContext = mContext;
this.rootView = view;
mDrawableCache = new HashMap<Integer, Drawable>();
}
public void animationInit() {
int[] reses = { R.drawable.liveness_head_pitch, R.drawable.liveness_head_yaw,
R.drawable.liveness_mouth_open_closed, R.drawable.liveness_eye_open_closed };
for (int oneRes : reses) {
mDrawableCache.put(oneRes, (mContext.getResources().getDrawable(oneRes)));
}
}
public void viewsInit() {
mAnimViews = new View[2];
mAnimViews[0] = (rootView.findViewById(R.id.liveness_layout_first_layout));
mAnimViews[1] = (rootView.findViewById(R.id.liveness_layout_second_layout));
for (View tmpView : mAnimViews) {
tmpView.setVisibility(View.INVISIBLE);
}
}
public void changeType(final Detector.DetectionType detectiontype, long timeout) {
Animation animationIN = AnimationUtils.loadAnimation(mContext, R.anim.liveness_rightin);
Animation animationOut = AnimationUtils.loadAnimation(mContext, R.anim.liveness_leftout);
if (mCurShowIndex != -1) // 已经存在layout 需要移除之
{
mAnimViews[mCurShowIndex].setVisibility(View.INVISIBLE);
mAnimViews[mCurShowIndex].setAnimation(animationOut);
} else {
mAnimViews[0].setVisibility(View.INVISIBLE);
mAnimViews[0].startAnimation(animationOut);
}
mCurShowIndex = mCurShowIndex == -1 ? 0 : (mCurShowIndex == 0 ? 1 : 0);
initAnim(detectiontype, mAnimViews[mCurShowIndex]);
mAnimViews[mCurShowIndex].setVisibility(View.VISIBLE);
mAnimViews[mCurShowIndex].startAnimation(animationIN);
}
TextView detectionNameText;
String detectionNameStr;
private void initAnim(Detector.DetectionType detectiontype, View layoutView) {
ImageView tmpImageView = (ImageView) layoutView.findViewById(R.id.detection_step_image);
tmpImageView.setImageDrawable(getDrawRes(detectiontype));
((AnimationDrawable) tmpImageView.getDrawable()).start();
detectionNameText = (TextView) layoutView.findViewById(R.id.detection_step_name);
detectionNameStr = getDetectionName(detectiontype);
detectionNameText.setText(detectionNameStr);
}
public void checkFaceTooLarge(boolean isLarge) {
if (detectionNameStr != null && detectionNameText != null) {
if (isLarge && !detectionNameText.getText().toString().equals(mContext.getString(R.string.face_too_large))) {
detectionNameText.setText(R.string.face_too_large);
} else if(!isLarge && detectionNameText.getText().toString().equals(mContext.getString(R.string.face_too_large))){
detectionNameText.setText(detectionNameStr);
}
}
}
private Drawable getDrawRes(Detector.DetectionType detectionType) {
int resID = -1;
switch (detectionType) {
case POS_PITCH:
case POS_PITCH_UP:
case POS_PITCH_DOWN:
resID = R.drawable.liveness_head_pitch;
break;
case POS_YAW_LEFT:
case POS_YAW_RIGHT:
case POS_YAW:
resID = R.drawable.liveness_head_yaw;
break;
case MOUTH:
resID = R.drawable.liveness_mouth_open_closed;
break;
case BLINK:
resID = R.drawable.liveness_eye_open_closed;
break;
}
Drawable cachedDrawAble = mDrawableCache.get(resID);
if (cachedDrawAble != null)
return cachedDrawAble;
else {
Drawable drawable = mContext.getResources().getDrawable(resID);
mDrawableCache.put(resID, (drawable));
return drawable;
}
}
private String getDetectionName(Detector.DetectionType detectionType) {
String detectionName = null;
switch (detectionType) {
case POS_PITCH:
detectionName = mContext.getString(R.string.meglive_pitch);
break;
case POS_YAW:
detectionName = mContext.getString(R.string.meglive_yaw);
break;
case MOUTH:
detectionName = mContext.getString(R.string.meglive_mouth_open_closed);
break;
case BLINK:
detectionName = mContext.getString(R.string.meglive_eye_open_closed);
break;
case POS_YAW_LEFT:
detectionName = mContext.getString(R.string.meglive_pos_yaw_left);
break;
case POS_YAW_RIGHT:
detectionName = mContext.getString(R.string.meglive_pos_yaw_right);
break;
}
return detectionName;
}
/**
* 初始化检测动作
*/
public void detectionTypeInit() {
ArrayList<DetectionType> tmpTypes = new ArrayList<Detector.DetectionType>();
tmpTypes.add(Detector.DetectionType.BLINK);// 眨眼
tmpTypes.add(Detector.DetectionType.MOUTH);// 张嘴
tmpTypes.add(Detector.DetectionType.POS_PITCH);// 缓慢点头
tmpTypes.add(Detector.DetectionType.POS_YAW);// 左右摇头
Collections.shuffle(tmpTypes);// 打乱顺序
mDetectionSteps = new ArrayList<DetectionType>(num);
for (int i = 0; i < num; i++) {
mDetectionSteps.add(tmpTypes.get(i));
}
}
public void onDestroy() {
rootView = null;
mContext = null;
if (mDrawableCache != null) {
mDrawableCache.clear();
}
}
}