weimingfei
3 小时以前 338e7ed513d21d1468c0908fdca2ea4097ee0621
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
package com.doumee.keyCabinet.ui.face;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;
 
import com.baidu.idl.main.facesdk.model.BDFaceSDKCommon;
import com.doumee.keyCabinet.MApplication;
import com.doumee.keyCabinet.R;
import com.doumee.keyCabinet.base.MyBaseActivity;
import com.doumee.keyCabinet.dao.DaoManager;
import com.doumee.keyCabinet.dao.FingerPrintDo;
import com.doumee.keyCabinet.databinding.FaceActivityBinding;
import com.doumee.keyCabinet.event.FaceStatusChangeEvent;
import com.doumee.keyCabinet.event.HttpEvent;
import com.doumee.keyCabinet.event.JiujinBeginEvent;
import com.doumee.keyCabinet.event.JiujinResultEvent;
import com.doumee.keyCabinet.event.RefreshFingerEvent;
import com.doumee.keyCabinet.event.TimeClockEvent;
import com.doumee.keyCabinet.ui.keyCabinet.KeyCabinetActivity;
import com.doumee.keyCabinet.utils.BraceletLogUtils;
import com.doumee.keyCabinet.utils.face.FaceUtils;
import com.doumee.keyCabinet.utils.face.model.SingleBaseConfig;
import com.doumee.keyCabinet.utils.usb.DevComm;
import com.doumee.keyCabinet.utils.usb.IUsbConnState;
import com.doumee.lib_coremodel.bean.event.ActionEventData;
import com.doumee.lib_coremodel.util.SpUtil;
import com.doumee.lib_coremodel.util.StringUtil;
import com.doumee.lib_coremodel.view.ToastView;
import com.example.datalibrary.api.FaceApi;
import com.example.datalibrary.callback.CameraDataCallback;
import com.example.datalibrary.callback.FaceDetectCallBack;
import com.example.datalibrary.db.DBManager;
import com.example.datalibrary.gatecamera.CameraPreviewManager;
import com.example.datalibrary.gl.view.GlMantleSurfacView;
import com.example.datalibrary.manager.FaceSDKManager;
import com.example.datalibrary.model.BDFaceCheckConfig;
import com.example.datalibrary.model.BDFaceImageConfig;
import com.example.datalibrary.model.LivenessModel;
import com.example.datalibrary.model.User;
import com.example.datalibrary.utils.FaceOnDrawTexturViewUtil;
import com.example.datalibrary.utils.ToastUtils;
 
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
 
import dagger.hilt.android.AndroidEntryPoint;
 
@AndroidEntryPoint
public class FaceActivity extends MyBaseActivity<FaceVM, FaceActivityBinding> implements View.OnClickListener{
 
    /*图片越大,性能消耗越大,也可以选择640*480, 1280*720*/
    private static final int PREFER_WIDTH = SingleBaseConfig.getBaseConfig().getRgbAndNirWidth();
    private static final int PERFER_HEIGH = SingleBaseConfig.getBaseConfig().getRgbAndNirHeight();
    private Context mContext;
    private boolean isCompareCheck = true;
 
    private User mUser;
 
    private GlMantleSurfacView glMantleSurfacView;
    private BDFaceImageConfig bdFaceImageConfig;
    private BDFaceCheckConfig bdFaceCheckConfig;
    //0:取,1:还
    private int flag ;
    private int status;
 
    @Override
    public int getLayoutId() {
        return R.layout.face_activity;
    }
 
    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        isToGuild = false;
        normalConfig();
        getDB().setModel(getVM());
        mContext = this;
        flag = MApplication.getLoginBean().getFlag();
        FaceSDKManager.getInstance().initDataBases(this);
        initFaceCheck();
        initView();
        if(flag==0){
            if(MApplication.getConfigBean().getDoubleAuth()==1){
                statusFsm(0);
            }else {
                statusFsm(2);
            }
        }else {
            statusFsm(7);
        }
        getDB().tvDjs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                statusFsm(status);
            }
        });
    }
 
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        //getPermission();
        initDev();
    }
 
    @Override
    protected void timeChange(String djs) {
        getDB().tvDjs.setText(djs);
    }
 
    private SpannableString getErrPhoneText(){
        String phone = MApplication.getConfigBean().getLinkPhone();
        if(phone==null){
            String text = "如有问题请联系管理员";
            SpannableString styledText = new SpannableString(text);
            styledText.setSpan(new TextAppearanceSpan(this, R.style.style_tip3), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return styledText;
        }
        String text = "如有问题请联系管理员"+phone;
        SpannableString styledText = new SpannableString(text);
        styledText.setSpan(new TextAppearanceSpan(this, R.style.style_tip3), 0, text.length()-phone.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        styledText.setSpan(new TextAppearanceSpan(this,R.style.style_tip1), text.length()-phone.length()+1, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return styledText;
    }
 
    private void statusFsm(int toStatus){
        status = toStatus;
        isToChose = false;
        getDB().etEwm.requestFocus();
        switch (toStatus){
            case 0:
                //取-管理员人脸验证
                getDB().tvTitle.setText("管理员身份验证");
                getDB().clCard.setVisibility(View.GONE);
                getDB().clZw.setVisibility(View.GONE);
                getDB().clFace.setVisibility(View.VISIBLE);
                setFaceModel(0);
                setFingerStatus(1);
                break;
            case 10:
                //取-管理员指纹验证
                getDB().tvTitle.setText("管理员身份验证");
                getDB().clCard.setVisibility(View.GONE);
                getDB().clFace.setVisibility(View.GONE);
                getDB().clZw.setVisibility(View.VISIBLE);
                setFaceModel(2);
                setFingerStatus(0);
                break;
            case 1:
                //取-管理员刷卡验证
                getDB().etEwm.setText("");
                getDB().tvTitle.setText("管理员身份验证");
                getDB().clFace.setVisibility(View.GONE);
                getDB().clZw.setVisibility(View.GONE);
                getDB().clCard.setVisibility(View.VISIBLE);
                setFaceModel(2);
                setFingerStatus(1);
                break;
            case 5:
                //取-司机验证方式选择页
                getDB().tvTitle.setText("司机身份验证");
                getDB().clFace.setVisibility(View.GONE);
                getDB().clZw.setVisibility(View.GONE);
                getDB().clCard.setVisibility(View.GONE);
                getDB().clTip1.setVisibility(View.GONE);
                getDB().clSjXuan.setVisibility(View.VISIBLE);
                getDB().clSjXuan.invalidate();
                getDB().clSjXuan.requestLayout();
                break;
            case 2:
                //取-司机人脸验证
                getDB().tvTitle.setText("司机身份验证");
                getDB().clCard.setVisibility(View.GONE);
                getDB().clZw.setVisibility(View.GONE);
                getDB().clSjXuan.setVisibility(View.GONE);
                getDB().clFace.setVisibility(View.VISIBLE);
                setFaceModel(1);
                setFingerStatus(1);
                break;
            case 11:
                //取-司机指纹验证
                getDB().tvTitle.setText("司机身份验证");
                getDB().clCard.setVisibility(View.GONE);
                getDB().clFace.setVisibility(View.GONE);
                getDB().clSjXuan.setVisibility(View.GONE);
                getDB().clZw.setVisibility(View.VISIBLE);
                setFaceModel(2);
                setFingerStatus(0);
                break;
            case 3:
                //取-司机刷卡验证
                getDB().etEwm.setText("");
                getDB().etEwm.requestFocus();
                getDB().tvTitle.setText("司机身份验证");
                getDB().clFace.setVisibility(View.GONE);
                getDB().clZw.setVisibility(View.GONE);
                getDB().clSjXuan.setVisibility(View.GONE);
                getDB().clCard.setVisibility(View.VISIBLE);
                setFaceModel(2);
                setFingerStatus(1);
                break;
            case 4:
                //取-司机酒精检测
                getDB().tvTitle.setText("酒精检测");
                getDB().clFace.setVisibility(View.GONE);
                getDB().clCard.setVisibility(View.GONE);
                getDB().clZw.setVisibility(View.GONE);
                getDB().clSjXuan.setVisibility(View.GONE);
                getDB().clTip1.setVisibility(View.GONE);
                getDB().clJiu.setVisibility(View.VISIBLE);
                getDB().clJiu.invalidate();
                getDB().clJiu.requestLayout();
                if(MApplication.getConfigBean()!=null&&MApplication.getConfigBean().getCabinetConfigDataVO()!=null&&
                        MApplication.getConfigBean().getCabinetConfigDataVO().getConcentration()!=null){
                    //开始检测
                    EventBus.getDefault().post(new JiujinBeginEvent());
                }else {
                    ToastView.show(this,"未获取到酒精浓度报警值");
                    statusFsm(6);
                }
                break;
            case 6:
                //取-司机酒精检测失败
                //Toast.makeText(mContext, "司机酒精检测失败", Toast.LENGTH_SHORT).show();
                getDB().clJiu.setVisibility(View.GONE);
                getDB().tvJg1.setText("酒精检测操作不当,请重新检测");
                getDB().clTip1.setVisibility(View.VISIBLE);
                break;
            case 7:
                //还-司机人脸验证
                getDB().tvTitle.setText("司机身份验证");
                getDB().clCard.setVisibility(View.GONE);
                getDB().clZw.setVisibility(View.GONE);
                getDB().clFace.setVisibility(View.VISIBLE);
                setFaceModel(1);
                setFingerStatus(1);
                break;
            case 12:
                //还-司机指纹验证
                getDB().tvTitle.setText("司机身份验证");
                getDB().clCard.setVisibility(View.GONE);
                getDB().clFace.setVisibility(View.GONE);
                getDB().clZw.setVisibility(View.VISIBLE);
                setFaceModel(2);
                setFingerStatus(0);
                break;
            case 8:
                //还-司机刷卡验证
                getDB().etEwm.setText("");
                getDB().etEwm.requestFocus();
                getDB().tvTitle.setText("司机身份验证");
                getDB().clFace.setVisibility(View.GONE);
                getDB().clCard.setVisibility(View.VISIBLE);
                setFaceModel(2);
                setFingerStatus(1);
                break;
            case 9:
                //取-司机酒精检测失败
                getDB().clJiu.setVisibility(View.GONE);
                getDB().tvJg1.setText("酒精检测超标");
                getDB().clTip1.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }
 
    private void setFaceModel(int faceModel){
        if(faceModel==0){
            //管理员
            FaceSDKManager.getInstance().setGroupId("0");
            isToChose = false;
        }else if(faceModel==1){
            //司机
            FaceSDKManager.getInstance().setGroupId("1");
            isToChose = false;
        }else {
            isToChose = true;
        }
    }
 
    private void setFingerStatus(int fs){
        if(fs==0){
            //开始检测
            identify();
        }else {
            //关闭检测
            mBCancel = true;
        }
    }
 
    private Bitmap bitmap;
    private byte[] secondFeature = new byte[512];
    @Override
    protected void doRegister(int type, ActionEventData data) {
        switch (type){
            case 1:
                if(flag==0){
                    //切换到管理员刷卡
                    if(status==0||status==10){
                        statusFsm(1);
                    }else if(status==2||status==11){
                        statusFsm(3);
                    }
                }else {
                    //还-司机刷卡验证
                    statusFsm(8);
                }
                break;
            case 9:
                //指纹验证
                if(status==0) {
                    statusFsm(10);
                }else {
                    statusFsm(12);
                }
                break;
            case 10:
                //司机指纹验证
                statusFsm(11);
                break;
            case 11:
                //刷卡-->指纹验证
                if(flag==0){
                    if(status==1){
                        statusFsm(10);
                    }else {
                        statusFsm(11);
                    }
                }else {
                    //还-司机指纹
                    statusFsm(12);
                }
                break;
            case 2:
                //切换司机刷卡
                statusFsm(3);
                break;
            case 3:
                //切换司机人脸
                statusFsm(2);
                break;
            case 4:
                //切换到人脸
                if(flag==0){
                    if(status==1||status==10){
                        statusFsm(0);
                    }else if(status==3||status==11){
                        statusFsm(2);
                    }
                }else {
                    statusFsm(7);
                }
                break;
            case 5:
                statusFsm(4);
                break;
            case 6:
                //管理员ic卡成功
                statusFsm(5);
                break;
            case 7:
                //司机ic卡成功
                if(flag==0) {
                    //取
                    if(MApplication.getConfigBean()!=null&&MApplication.getConfigBean().getAlcoholStatus()==1) {
                        //非酒精检测
                        startActivity(KeyCabinetActivity.class);
                        finish();
                    }else {
                        //酒精检测
                        statusFsm(4);
                    }
                }else {
                    //还
                    startActivity(KeyCabinetActivity.class);
                    finish();
                }
                break;
            case 8:
                getDB().message.setText(data.getData().get("obj").toString());
                getDB().message.setVisibility(View.VISIBLE);
                handler.sendEmptyMessageDelayed(6,2000);
                getDB().message.invalidate();
                getDB().message.requestLayout();
                EventBus.getDefault().post(new HttpEvent("ic卡+++++++"+data.getData().get("type").toString()));
                break;
 
            default:
                break;
        }
    }
 
    private void initFaceConfig(int height , int width){
        bdFaceImageConfig = new BDFaceImageConfig(height , width ,
                SingleBaseConfig.getBaseConfig().getRgbDetectDirection(),
                SingleBaseConfig.getBaseConfig().getMirrorDetectRGB() ,
                BDFaceSDKCommon.BDFaceImageType.BDFACE_IMAGE_TYPE_YUV_NV21);
    }
    private void initFaceCheck(){
        bdFaceCheckConfig = FaceUtils.getInstance().getBDFaceCheckConfig();
    }
 
    /**
     * View
     */
    private void initView() {
        getDB().faceDetectImageView.setVisibility(View.GONE);
 
        glMantleSurfacView = findViewById(R.id.camera_textureview);
        glMantleSurfacView.initSurface(SingleBaseConfig.getBaseConfig().getRgbRevert(),
                SingleBaseConfig.getBaseConfig().getMirrorVideoRGB() , SingleBaseConfig.getBaseConfig().isOpenGl());
        CameraPreviewManager.getInstance().startPreview(glMantleSurfacView,
                SingleBaseConfig.getBaseConfig().getRgbVideoDirection() , PREFER_WIDTH, PERFER_HEIGH);
        getDB().tvJg2.setText(getErrPhoneText(), TextView.BufferType.SPANNABLE);
 
        //ic卡读取
        getDB().etEwm.requestFocus();
        getDB().etEwm.setFocusable(true);
        getDB().etEwm.setShowSoftInputOnFocus(false);
        hideSoftKeyboard(getDB().etEwm);
        getDB().etEwm.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                hideSoftKeyboard(getDB().etEwm);
            }
        });
 
        getDB().etEwm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
 
            }
 
            @Override
            public void afterTextChanged(Editable s) {
                String m = s.toString();
                if(TextUtils.isEmpty(m)){
                    return;
                }
                if(status!=1&&status!=3&&status!=8){
                    getDB().etEwm.setText("");
                    getDB().etEwm.requestFocus();
                    return;
                }
                if(handler.hasMessages(5)){
                    handler.removeMessages(5);
                }
                handler.sendEmptyMessageDelayed(5,500);
            }
        });
    }
 
    private void hideSoftKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
 
 
    @Override
    protected void onResume() {
        super.onResume();
        startTestOpenDebugRegisterFunction();
    }
 
    private void startTestOpenDebugRegisterFunction() {
        if(SingleBaseConfig.getBaseConfig().getRBGCameraId()==-1){
            if(!TextUtils.isEmpty(SpUtil.getString("rbgCameraId"))) {
                SingleBaseConfig.getBaseConfig().setRBGCameraId(Integer.parseInt(SpUtil.getString("rbgCameraId")));
            }
        }
        if (SingleBaseConfig.getBaseConfig().getRBGCameraId() != -1) {
            BraceletLogUtils.saveLog("使用相机("+SingleBaseConfig.getBaseConfig().getRBGCameraId()+")");
            CameraPreviewManager.getInstance().setCameraFacing(SingleBaseConfig.getBaseConfig().getRBGCameraId());
        } else {
            BraceletLogUtils.saveLog("使用相机("+CameraPreviewManager.CAMERA_USB+")");
            CameraPreviewManager.getInstance().setCameraFacing(CameraPreviewManager.CAMERA_USB);
        }
        int[] cameraSize =  CameraPreviewManager.getInstance().initCamera(new CameraPreviewManager.ErrInfoCallBack() {
            @Override
            public void err(String errInfo) {
                BraceletLogUtils.saveLog(errInfo);
            }
        });
        initFaceConfig(cameraSize[1] , cameraSize[0]);
        isPause = true;
        count = 60;
        CameraPreviewManager.getInstance().setmCameraDataCallback(dataCallback);
    }
 
    private CameraDataCallback dataCallback = new CameraDataCallback() {
        @Override
        public void onGetCameraData(byte[] data, Camera camera, int width, int height) {
            if(!isFinishing()){
                //isCheckFace = true;
                handler.sendEmptyMessageDelayed(1,4000);
                glMantleSurfacView.setFrame();
                bdFaceImageConfig.setData(data);
                FaceSDKManager.getInstance().onDetectCheck(bdFaceImageConfig, null, null,
                        bdFaceCheckConfig, faceDetectCallBack);
            }
        }
    };
    //private boolean isCheckFace;
    private FaceDetectCallBack faceDetectCallBack = new FaceDetectCallBack() {
        @Override
        public void onFaceDetectCallback(LivenessModel livenessModel) {
            //System.out.println("==isOk==>onFaceDetectCallback");
            //System.out.println("==rgbInstance==>onFaceDetectCallback   "+livenessModel);
            // 开发模式
            //isCheckFace = false;
            try {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(handler==null){
                            return;
                        }
                        if(handler.hasMessages(1)){
                            handler.removeMessages(1);
                        }
                        checkOpenDebugResult(livenessModel);
                    }
                });
            }catch (Exception e){
                e.printStackTrace();
                ToastView.show(MApplication.mContext,"人脸报错2:"+e.getMessage());
            }
 
        }
 
        @Override
        public void onTip(int code, String msg) {
            if(isToChose){
                return;
            }
            /*getDB().clFail.setVisibility(View.VISIBLE);
            getDB().tv4.setText(msg);
            handler.sendEmptyMessageDelayed(0,1000);*/
            //System.out.println("==isOk==>onTip:"+msg);
        }
 
        @Override
        public void onFaceDetectDarwCallback(LivenessModel livenessModel) {
            //System.out.println("==rgbInstance==>onFaceDetectDarwCallback");
            if(isToChose){
                return;
            }
            if (isCompareCheck) {
                /*getDB().clFail.setVisibility(View.VISIBLE);
                getDB().tv4.setText(getString(R.string.face_tip2));
                handler.sendEmptyMessageDelayed(0,1000);*/
            }
            // 绘制人脸框
            showFrame(livenessModel);
 
        }
    };
 
    private boolean isPause = false;
    public void showFrame(LivenessModel livenessModel){
        if(isFinishing()){
            return;
        }
        if (livenessModel == null){
            return;
        }
        try {
            if (isPause){
                glMantleSurfacView.onGlDraw(livenessModel.getTrackFaceInfo() ,
                        livenessModel.getBdFaceImageInstance() ,
                        FaceOnDrawTexturViewUtil.drawFaceColor(mUser , livenessModel));
            }
        }catch (Exception e){
            e.printStackTrace();
            ToastView.show(MApplication.mContext,"人脸报错1:"+e.getMessage());
        }
    }
 
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 0:
                    getDB().clFail.setVisibility(View.GONE);
                    break;
                case 1:
                    //isCheckFace = false;
                    break;
                case 3:
                    getDB().clJiujinProgress.setVisibility(View.GONE);
                    //Toast.makeText(mContext, "酒精检查返回", Toast.LENGTH_SHORT).show();
                    if(new BigDecimal(MApplication.getConfigBean().getCabinetConfigDataVO().getConcentration())
                            .compareTo(resultEvent.getConcentration())>=0){
                        //酒精通过
                        getVM().alcoholTestAlarm(resultEvent.getConcentration().toString());
                        getDB().clJiujinOk.setVisibility(View.VISIBLE);
                        handler.sendEmptyMessageDelayed(4,1000);
                    }else {
                        //酒精不通过
                        getVM().alcoholTestAlarm(resultEvent.getConcentration().toString());
                        //Toast.makeText(mContext, "酒精检测超标", Toast.LENGTH_SHORT).show();
                        statusFsm(9);
                        //再检测,将声音关闭
                        EventBus.getDefault().post(new JiujinBeginEvent());
                    }
                    break;
                case 4:
                    startActivity(KeyCabinetActivity.class);
                    finish();
                    break;
                case 5:
                    String m = getDB().etEwm.getText().toString();
                    int index = m.indexOf("\r");
                    if(index!=-1){
                        m = m.substring(0,index);
                    }
                    if(!TextUtils.isEmpty(m)){
                        getVM().getMemberIdByCode(m,status==1?"0":"1");
                    }
                    getDB().etEwm.setText("");
                    getDB().etEwm.requestFocus();
                    break;
                case 6:
                    getDB().message.setVisibility(View.GONE);
                    break;
                case 7:
                    identify();
                    break;
                default:
                    break;
            }
        }
    };
 
    @Override
    protected void onRestart() {
        super.onRestart();
        isTimePause = false;
        isToChose = false;
        count = 120;
    }
 
    private int count = 120;
    private boolean isTimePause = false;
    @Subscribe
    public void TimeClockEvent(TimeClockEvent event) {
        if (!isFinishing()&&!isTimePause) {
            if (count > 0) {
                count--;
                if (count == 0) {
                    finish();
                    return;
                }
            }
        }
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
        handler = null;
        dataCallback = null;
        CameraPreviewManager.getInstance().destroy();
        faceDetectCallBack = null;
        glMantleSurfacView = null;
        FaceSDKManager.getInstance().destroy();
        mBCancel = true;
        if(mDevComm!=null){
            mDevComm.CloseComm();
        }
    }
 
    private int resultCount = 0;
    // ***************开发模式结果输出*************
    private void checkOpenDebugResult(final LivenessModel livenessModel) {
        try {
            if (isFinishing()) {
                return;
            }
            if (handler == null) {
                return;
            }
            // 当未检测到人脸UI显示
            if (isToChose) {
                return;
            }
            if (livenessModel == null) {
                if (isCompareCheck) {
                        /*getDB().clFail.setVisibility(View.VISIBLE);
                        getDB().tv4.setText(getString(R.string.face_tip2));
                        handler.sendEmptyMessageDelayed(0,1000);*/
                }
                //System.out.println("======>人脸识别失败");
                return;
            }
            if (livenessModel.isQualityCheck()) {
                //是否通过质量检测
                if (isCompareCheck) {
                        /*getDB().clFail.setVisibility(View.VISIBLE);
                        getDB().tv4.setText(getString(R.string.face_tip2));
                        handler.sendEmptyMessageDelayed(0,1000);*/
                }
            } else {
                User user = livenessModel.getUser();
                if (user == null) {
                    mUser = null;
                    //EventBus.getDefault().post(new HttpEvent("人脸用户检测失败------->"+(status==0?"管理员人脸检测:":"司机人脸检测:")));
                    if (isCompareCheck) {
                            /*getDB().clFail.setVisibility(View.VISIBLE);
                            getDB().tv4.setText(getString(R.string.face_tip2));
                            handler.sendEmptyMessageDelayed(0,1000);*/
                    }
 
                } else {
                    mUser = user;
                        /*EventBus.getDefault().post(new HttpEvent("人脸用户------->"+(status==0?"管理员人脸检测:":"司机人脸检测:")
                                +(user.getGroupId().equals("0")?"管理员":"司机")));*/
                    if (isCompareCheck) {
                        getDB().clFail.setVisibility(View.GONE);
                        if (handler.hasMessages(0)) {
                            handler.removeMessages(0);
                        }
                            /*textHuanying.setVisibility(View.GONE);
                            userNameLayout.setVisibility(View.VISIBLE);
                            nameImage.setImageResource(R.mipmap.ic_tips_gate_success);
                            nameText.setTextColor(Color.parseColor("#0dc6ff"));
                            nameText.setText(FileUtils.spotString(user.getUserName()) + " 欢迎您");*/
                    }
                    isToChose = true;
                    //识别成功,跳转
                    if (status == 0) {
                        //取-管理员
                        MApplication.getLoginBean().setAutoMemberId(Integer.parseInt(user.getUserId()));
                        //handler.sendEmptyMessageDelayed(7,0);
                        EventBus.getDefault().post(new FaceStatusChangeEvent(5));
                    } else if (status == 2) {
                        //取-司机
                        CameraPreviewManager.getInstance().stopPreview();
                        MApplication.getLoginBean().setMemberId(Integer.parseInt(user.getUserId()));
                        MApplication.getLoginBean().setAuthType(0);
                        //handler.sendEmptyMessageDelayed(8,0);
                        //Toast.makeText(mContext, "是否酒精检测:"+MApplication.getConfigBean().getAlcoholStatus(), Toast.LENGTH_SHORT).show();
                        if (MApplication.getConfigBean() != null && MApplication.getConfigBean().getAlcoholStatus() == 1) {
                            //非酒精检测
                            startActivity(KeyCabinetActivity.class);
                            finish();
                        } else {
                            //酒精检测
                            EventBus.getDefault().post(new FaceStatusChangeEvent(4));
                        }
                    } else if (status == 7) {
                        //还-司机
                        MApplication.getLoginBean().setMemberId(Integer.parseInt(user.getUserId()));
                        MApplication.getLoginBean().setAuthType(0);
                        startActivity(KeyCabinetActivity.class);
                        finish();
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            ToastView.show(MApplication.mContext,"人脸报错:"+e.getMessage());
        }
    }
 
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void FaceStatusChangeEvent(FaceStatusChangeEvent e){
        if(!isFinishing()){
            statusFsm(e.getStatus());
        }
    }
 
    boolean isToChose = false;
 
    @Override
    public void onClick(View v) {
 
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        isPause = false;
        isTimePause = true;
        CameraPreviewManager.getInstance().stopPreview();
    }
 
    private JiujinResultEvent resultEvent;
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void JiujinResultEvent(JiujinResultEvent e){
        if(!isFinishing()){
            if(status!=4&&status!=6){
                return;
            }
            //Toast.makeText(mContext, "酒精检测返回:"+e.isOk(), Toast.LENGTH_SHORT).show();
            if(!e.isOk()){
                getDB().clJiujinProgress.setVisibility(View.GONE);
                statusFsm(6);
            }else {
                getDB().clJiujinProgress.setVisibility(View.VISIBLE);
                if(resultEvent==null){
                    resultEvent = e;
                }else if(resultEvent.getConcentration().compareTo(e.getConcentration())<0){
                    //值更大
                    resultEvent = e;
                }
                if(handler.hasMessages(3)){
                    handler.removeMessages(3);
                }
                handler.sendEmptyMessageDelayed(3,1000);
            }
        }
    }
 
    /**------------------------指纹模块--------------------------*/
 
    private void getUserIdByFingerId(int fingerId){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                FingerPrintDo printDo = DaoManager.getFingerPrintDao().getByFingerId(fingerId);
                if(printDo!=null){
                    List<User> users = FaceApi.getInstance().getAllUserList();
                    HashMap<String,String> map = new HashMap<>();
                    for(User u:users){
                        if(u.getUserId().equals(printDo.getMemberId()+"")){
                            map.put(u.getGroupId(),"");
                        }
                    }
                    if(status==10){
                        //取-管理员指纹验证
                        if(!map.containsKey("0")){
                            showToast("您当前不是管理员");
                            handler.sendEmptyMessageDelayed(7,2000);
                            return;
                        }
                        MApplication.getLoginBean().setAutoMemberId(printDo.getMemberId());
                        EventBus.getDefault().post(new FaceStatusChangeEvent(5));
                    }else if(status==11){
                        //取-司机指纹验证
                        if(!map.containsKey("1")){
                            showToast("您当前不是司机");
                            handler.sendEmptyMessageDelayed(7,2000);
                            return;
                        }
                        MApplication.getLoginBean().setMemberId(printDo.getMemberId());
                        MApplication.getLoginBean().setAuthType(3);
                        if(MApplication.getConfigBean()!=null&&MApplication.getConfigBean().getAlcoholStatus()==1) {
                            //非酒精检测
                            startActivity(KeyCabinetActivity.class);
                            finish();
                        }else {
                            //酒精检测
                            EventBus.getDefault().post(new FaceStatusChangeEvent(4));
                        }
                    }else if(status==12){
                        //还-司机
                        if(!map.containsKey("1")){
                            showToast("您当前不是司机");
                            handler.sendEmptyMessageDelayed(7,2000);
                            return;
                        }
                        MApplication.getLoginBean().setMemberId(printDo.getMemberId());
                        MApplication.getLoginBean().setAuthType(3);
                        startActivity(KeyCabinetActivity.class);
                        finish();
                    }
                }else {
                    //找不到,删除指纹
                    if(deleteID(fingerId)) {
                        //继续查指纹
                        handler.sendEmptyMessageDelayed(7,2000);
                    }else {
                        showToast("未查询到用户");
                    }
                }
            }
        });
    }
 
    private static DevComm mDevComm;
    //是否结束录入
    private boolean mBCancel = true;
    private byte[] m_binImage = new byte[1024 * 100];
    private final IUsbConnState m_IConnectionHandler = new IUsbConnState() {
        @Override
        public void onUsbConnected() {
            //String[] w_strInfo = new String[1];
            /*if (mDevComm.Run_TestConnection() == DevComm.ERR_SUCCESS) {
                if (mDevComm.Run_GetDeviceInfo(w_strInfo) == DevComm.ERR_SUCCESS) {
 
                    getVM().addInfo("连接usb成功");
                }else {
                    getVM().addInfo("连接设备失败1," + mDevComm.Run_GetDeviceInfo(w_strInfo));
                }
            } else {
                getVM().addInfo("连接设备失败2," + mDevComm.Run_TestConnection());
            }*/
        }
 
        @Override
        public void onUsbPermissionDenied() {
            Toast.makeText(mContext, "无设备权限", Toast.LENGTH_SHORT).show();
        }
 
        @Override
        public void onDeviceNotFound() {
            Toast.makeText(mContext, "未发现指纹设备", Toast.LENGTH_SHORT).show();
        }
    };
 
    private void initDev(){
        mDevComm = new DevComm(this, m_IConnectionHandler);
        openDevice();
    }
    /**
     * 开启设备
     */
    private void openDevice() {
        String[] w_strInfo = new String[1];
 
        if (mDevComm != null) {
            if (!mDevComm.IsInit()) {
                if (mDevComm.OpenComm("USB", 19200) == false) {
                    EventBus.getDefault().post(new HttpEvent("初始化设备失败"));
                    return;
                }
            }
            if (mDevComm.Run_TestConnection() == DevComm.ERR_SUCCESS) {
                if (mDevComm.Run_GetDeviceInfo(w_strInfo) == DevComm.ERR_SUCCESS) {
                    EventBus.getDefault().post(new HttpEvent("开启设备成功"));
                } else {
                    EventBus.getDefault().post(new HttpEvent("连接设备失败3," + mDevComm.Run_GetDeviceInfo(w_strInfo)));
                }
            } else {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        EventBus.getDefault().post(new HttpEvent("连接设备失败4,"+mDevComm.Run_TestConnection()));
                    }
                });
                mDevComm.CloseComm();
            }
        }
    }
 
    private void identify() {
        if (!mDevComm.IsInit())
            return;
        mBCancel = false;
        new Thread(new Runnable() {
            int w_nRet;
            int[] w_nID = new int[1];
            int[] w_nLearned = new int[1];
            int[] w_nWidth = new int[1];
            int[] w_nHeight = new int[1];
 
            @Override
            public void run() {
 
                while (true) {
                    if(mBCancel){
                        //停止
                        return;
                    }
                    if (capturing() < 0)
                        return;
                    //松开手指
 
                    // Up Cpatured Image
                    if (mDevComm.m_nConnected == 2) {
                        w_nRet = mDevComm.Run_UpImage(0, m_binImage, w_nWidth, w_nHeight);
 
                        if (w_nRet != DevComm.ERR_SUCCESS) {
                            showToast(DevComm.GetErrorMsg(w_nRet));
                            return;
                        }
                    }
 
                    // Create template
                    w_nRet = mDevComm.Run_Generate(0);
 
                    if (w_nRet != DevComm.ERR_SUCCESS) {
                        if (w_nRet == DevComm.ERR_CONNECTION) {
                            showToast(DevComm.GetErrorMsg(w_nRet));
                            return;
                        } else {
                            SystemClock.sleep(1000);
                            continue;
                        }
                    }
 
                    // Identify
                    w_nRet = mDevComm.Run_Search(0, 1, 500, w_nID, w_nLearned);
                    if(mBCancel){
                        //停止
                        return;
                    }
                    if (w_nRet == DevComm.ERR_SUCCESS) {
                        //找到了
                        //showToast("找到了,"+w_nID[0]);
                        getUserIdByFingerId(w_nID[0]);
                        break;
                        //m_strPost = String.format("Result : Success\r\nTemplate No : %d, Learn Result : %d\r\nMatch Time : %dms", w_nID[0], w_nLearned[0], m_nPassedTime);
                    } else {
                        //没找到
                        showToast("未检测到该指纹");
                        /*m_strPost = String.format("\r\nMatch Time : %dms", m_nPassedTime);
                        m_strPost = GetErrorMsg(w_nRet) + m_strPost;*/
                    }
                }
            }
        }).start();
    }
 
    /**
     * 获取指纹
     * @return
     */
    private int capturing() {
        int w_nRet;
        while (true) {
            if(isFinishing()){
                break;
            }
            w_nRet = mDevComm.Run_GetImage();
 
            if (w_nRet == DevComm.ERR_CONNECTION) {
                showToast("通信错误!");
                return -1;
            } else if (w_nRet == DevComm.ERR_SUCCESS) {
                break;
            }
            if (mBCancel) {
                return -1;
            }
        }
        return 0;
    }
 
    private boolean deleteID(int fingerId) {
        int m_nUserID = fingerId;
        int w_nRet;
 
        if (!mDevComm.IsInit())
            return false;
 
        w_nRet = mDevComm.Run_DelChar(m_nUserID, m_nUserID);
        if (w_nRet != DevComm.ERR_SUCCESS&&w_nRet!=DevComm.ERR_TMPL_EMPTY) {
            //删除失败
            return false;
        }
        return true;
    }
 
    private void showToast(String msg){
        if(!isFinishing()){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ToastView.show(MApplication.mContext,msg);
                }
            });
        }
    }
}