a
554325746@qq.com
2019-12-25 84e391f79e4c298e31b990667a54d991d645949f
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
package com.basic.security.manager;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;
 
import com.alfeye.readcardlib.entity.CardInfo;
import com.alfeye.readcardlib.readcard.ReadCardUtil;
import com.basic.security.activity.MainActivity;
import com.basic.security.base.BaseApplication;
import com.basic.security.base.BaseFragment;
import com.basic.security.utils.Constants;
import com.basic.security.utils.DateUtil;
import com.basic.security.utils.FaceId;
import com.basic.security.utils.IdCard;
import com.basic.security.utils.ToastUtil;
 
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
 
import static com.basic.security.utils.Constants.isNewIDCardReader;
 
public class IdCardManager {
 
    private static IntentFilter filter;
    private static BroadcastReceiver receiver;
    private volatile static boolean inRead = false;
    private static boolean receiverRegisted = false;
    private static int second = 0;
 
    public static byte[] getPixelsBGR(Bitmap image) {
        // calculate how many bytes our image consists of
        int bytes = image.getByteCount();
 
        ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new buffer
        image.copyPixelsToBuffer(buffer); // Move the byte data to the buffer
 
        byte[] temp = buffer.array(); // Get the underlying array containing the data.
 
        byte[] pixels = new byte[(temp.length / 4) * 3]; // Allocate for BGR
 
        // Copy pixels into place
        for (int i = 0; i < temp.length / 4; i++) {
            pixels[i * 3] = temp[i * 4 + 2];        //B
            pixels[i * 3 + 1] = temp[i * 4 + 1];    //G
            pixels[i * 3 + 2] = temp[i * 4];        //R
        }
        return pixels;
    }
 
    public static void restart(Context activity) {
        BaseApplication.getApplication().executorService.execute(() -> {
            try {
                second += 2000;
                Thread.sleep(second);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Intent intent = new Intent();
            intent.setPackage("com.basic.project.idcardservice");
            intent.setAction("com.basic.read.IDCard");
            intent.putExtra("type", "start_service");
            intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            activity.startService(intent);
        });
    }
 
    public static void startReadIdCard() {
        if (!Constants.needIdCardModule) {
            return;
        }
        MainActivity activity = BaseApplication.getApplication().activity;
 
//        Log.e("startReadIdCard", "startReadIdCard  只能出现一次。如果多次 要检查代码");
 
//        if (isNewIDCardReader) {
 
        if (activity == null) {
            ToastUtil.show("程序异常, 请重新启动");
            return;
        }
        activity.readCardUtil = new ReadCardUtil(activity, new ReadCardUtil.OnReadCardListener() {
            @Override
            public void onReadCardSucceed(String s, CardInfo cardInfo, Intent intent) {
 
                Log.e("OnReadCardListener", "onReadCardSucceed");
                if (activity != null) {
                    if (activity.currentFragment == activity.fragment_su_auto_ic_wait_idcard || activity.currentFragment == activity.fragment_su_logged_ic_wait_idcard
                            || (activity.currentFragment == activity.fragment_person_manage && activity.fragment_person_manage.needIdCardInfo())) {
 
                        IdCard c = new IdCard();
                        c.birthday = DateUtil.getTimeStamp(cardInfo.getBirthday(), "yyyyMMdd");
                        c.cardNumber = cardInfo.getCardNum();
                        c.gender = cardInfo.getGender();
                        c.name = cardInfo.getName();
                        c.headBitmap = cardInfo.getPhoto();
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        cardInfo.getPhoto().compress(Bitmap.CompressFormat.JPEG, 100, stream);
                        c.jpgData = stream.toByteArray();
 
                        BaseApplication.getApplication().detectLock.lock();
                        c.featureData = FaceId.instance.extractFeature1(getPixelsBGR(c.headBitmap), c.headBitmap.getWidth(), c.headBitmap.getHeight());
                        BaseApplication.getApplication().detectLock.unlock();
 
//                        sendResult(c, activity);
 
                        BaseFragment.idCardReadTime = System.currentTimeMillis();
                        BaseFragment.idCard = c;
                    }
                }
            }
 
            @Override
            public void onReadCardFail(int i, String s) {
 
                Log.e("OnReadCardListener", "onReadCardFail");
            }
        });
 
 
        filter = new IntentFilter();
        filter.addAction("read.id.card.data");
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
 
                Log.e("project.id.card", "receive id card data   " + Thread.currentThread().getId());
 
                if (activity != null) {
                    if (activity.currentFragment == activity.fragment_su_auto_ic_wait_idcard
//                            || activity.currentFragment == activity.fragment_home
                            || activity.currentFragment == activity.fragment_su_logged_ic_wait_idcard
                            || (activity.currentFragment == activity.fragment_person_manage && activity.fragment_person_manage.needIdCardInfo())) {
 
                        if (intent.getBooleanExtra("close", false)) {
                            Toast.makeText(activity, "身份证连接失败", Toast.LENGTH_SHORT).show();
                            restart(activity);
                        } else {
 
                            IdCard c = new IdCard();
                            c.birthday = intent.getLongExtra("birthday", 0);
                            c.cardNumber = intent.getStringExtra("cardNumber");
                            c.gender = intent.getStringExtra("gender");
                            c.name = intent.getStringExtra("name");
                            c.jpgData = intent.getByteArrayExtra("jpgData");
 
                            c.headBitmap = BitmapFactory.decodeByteArray(c.jpgData, 0, c.jpgData.length);
 
 
                            BaseApplication.getApplication().detectLock.lock();
                            c.featureData = FaceId.instance.extractFeature1(getPixelsBGR(c.headBitmap), c.headBitmap.getWidth(), c.headBitmap.getHeight());
                            BaseApplication.getApplication().detectLock.unlock();
 
                            BaseFragment.idCardReadTime = System.currentTimeMillis();
                            Log.e("MaFragments", "read card ");
 
                            BaseFragment.idCard = c;
                        }
 
                    }
                }
            }
        };
        activity.registerReceiver(receiver, filter);
        receiverRegisted = true;
 
 
//            activity.readCardUtil.startReadCard();
 
 
//        } else {
        BaseApplication.getApplication().executorService.execute(() -> {
            SystemClock.sleep(5 * 1000);
 
            while (true) {
                try {
 
//                        Log.e("read_id_card", "score 33333333333333333333333333333333333333");
//                        Thread.sleep(300);
 
//                            if (activity != null) {
                    if (activity.currentFragment == activity.fragment_su_auto_ic_wait_idcard
//                                || activity.currentFragment == activity.fragment_home
                            || activity.currentFragment == activity.fragment_su_logged_ic_wait_idcard
                            || (activity.currentFragment == activity.fragment_person_manage && activity.fragment_person_manage.needIdCardInfo())) {
 
                        if (!inRead) {
                            inRead = true;
                            if (isNewIDCardReader) {
                                activity.readCardUtil.startReadCard();
                            } else {
                                Intent intent = new Intent();
                                intent.setPackage("com.basic.project.idcardservice");
                                intent.setAction("com.basic.read.IDCard");
                                intent.putExtra("type", "start_service");
                                intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                activity.startService(intent);
                            }
                        }
//                            else {
 
//                                if (activity.readUtil.isPort()) {
//
//                                    IDCard idCard = activity.readUtil.readCard();
//                                    if (idCard != null) {
//
//                                        Bitmap headBitmap = BitmapFactory.decodeFile(idCard.getIdPhoto());
//
//                                        IdCard c = new IdCard();
//                                        c.birthday = DateUtil.getTimeStamp(idCard.getIdDate(), "yyyyMMdd");
//                                        c.cardNumber = idCard.getIdNum();
//                                        c.gender = idCard.getIdSex();
//                                        c.name = idCard.getIdName();
//                                        c.headBitmap = headBitmap;
////                                            c.featureData = featureData;
//                                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
//                                        headBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
//                                        c.jpgData = stream.toByteArray();
//
//
//                                        sendResult(c, activity);
//
//                                    }
//                                } else {
//                                    ToastUtil.show("身份证设备未连接");
//                                    SystemClock.sleep(5000);
//                                }
//                            }
                    } else {
                        if (inRead) {
                            inRead = false;
                            if (isNewIDCardReader) {
                                activity.readCardUtil.stopReadCard();
                            } else {
                                Intent intent = new Intent();
                                intent.setPackage("com.basic.project.idcardservice");
                                intent.setAction("com.basic.read.IDCard");
                                intent.putExtra("type", "stop_service");
                                intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                activity.startService(intent);
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
 
                SystemClock.sleep(200);
            }
        });
 
    }
 
 
    public static void unRegisterBroadcast(Context context) {
        Intent intent = new Intent();
        intent.setPackage("com.basic.project.idcardservice");
        intent.setAction("com.basic.read.IDCard");
        intent.putExtra("type", "close_service");
//        intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        context.startService(intent);
        try {
            if (IdCardManager.receiverRegisted) {
                context.unregisterReceiver(receiver);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
 
}