554325746@qq.com
2019-08-07 2539f53391765abb74b6fe63f46e5a2c701e950f
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
package com.basic.project.idcardservice;
 
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;
 
import com.basic.project.idcardservice.idcard.AppConfig;
import com.basic.project.idcardservice.idcard.FileStorageHelper;
import com.basic.project.idcardservice.idcard.ReadUtil;
import com.byid.android.IDCard;
 
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.Lock;
 
public class ReadIDCardService extends Service {
 
    private volatile boolean inReader = false;
 
    //1是usb模式   0是串口模式   5.1.1
    public ReadUtil readUtil;
 
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
 
    @Override
    public void onCreate() {
        close = false;
        FileStorageHelper.copyFilesFromRaw(this, R.raw.base, "base.dat", AppConfig.RootFile);
        FileStorageHelper.copyFilesFromRaw(this, R.raw.license, "license.lic", AppConfig.RootFile);
        readUtil = new ReadUtil(this, 1);
        Log.e("project.id.card", "onCreate" + Thread.currentThread().getId());
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
 
        String type = intent.getStringExtra("type");
 
        Log.e("project.id.card", "onStartCommand    " + Thread.currentThread().getId() + "   " + type);
 
        switch (type) {
 
            case "start_service":
                startReader();
                break;
 
            case "stop_service":
                stopReader();
                break;
 
            case "close_service":
                closeReader();
                break;
 
            default:
                break;
        }
 
        return super.onStartCommand(intent, flags, startId);
    }
 
    private boolean close = false;
 
    private void closeReader() {
 
        synchronized (this) {
            if (!close) {
                close = true;
            } else {
                return;
            }
        }
 
        inReader = false;
        Log.e("project.id.card", "closeReader    " + Thread.currentThread().getId());
        readUtil.closeUSBDevice();
        Intent i = new Intent();
        i.putExtra("close", true);
        i.setPackage("com.basic.security");
        i.setAction("read.id.card.data");
        sendBroadcast(i);
        stopSelf();
    }
 
    private void startReader() {
        if (!inReader) {
            Log.e("project.id.card", "start");
            inReader = true;
 
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (inReader) {
                        SystemClock.sleep(5*1000);
                        if (readUtil.isPort()) {
 
                            Log.e("project.id.card", "1");
                            IDCard idCard = readUtil.readCard();
                            Log.e("project.id.card", "2");
                            if (idCard != null) {
                                Log.e("project.id.card", idCard.getIdNum());
 
                                Bitmap headBitmap = BitmapFactory.decodeFile(idCard.getIdPhoto());
                                IdCard c = new IdCard();
                                c.birthday = getTimeStamp(idCard.getIdDate(), "yyyyMMdd");
                                c.cardNumber = idCard.getIdNum();
                                c.gender = idCard.getIdSex();
                                c.name = idCard.getIdName();
                                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                headBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                                c.jpgData = stream.toByteArray();
 
                                Intent i = new Intent();
                                i.putExtra("birthday", c.birthday);
                                i.putExtra("cardNumber", c.cardNumber);
                                i.putExtra("gender", c.gender);
                                i.putExtra("name", c.name);
                                i.putExtra("jpgData", c.jpgData);
 
                                i.setPackage("com.basic.security");
                                i.setAction("read.id.card.data");
 
                                sendBroadcast(i);
                            }
                            Log.e("project.id.card", "3");
 
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            Log.e("project.id.card", "4");
 
                        } else {
                            Log.e("project.id.card", "连接usb设备失败,开始关闭");
                            //Toast.makeText(getApplicationContext(), "连接usb设备失败", Toast.LENGTH_SHORT).show();
                            closeReader();
                        }
                    }
                }
            }).start();
        } else {
            Log.e("project.id.card", "inReader = true!");
        }
    }
 
    private void stopReader() {
        if (inReader) {
            Log.e("project.id.card", "stop");
            inReader = false;
        } else {
            Log.e("project.id.card", "inReader = false!");
        }
    }
 
 
    private static long getTimeStamp(String time, String format) {
 
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Date date = new Date();
        try {
            date = dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }
}