a
554325746@qq.com
2020-01-13 7171bbcdb2859ea93f3af69d817243752b08314a
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
package com.basic.project.idcardservice;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
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 org.apache.commons.io.FileUtils;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
public class ReadIDCardService extends Service {
    private volatile boolean inReader = false;
    public ReadUtil readUtil;
    long lastOnReceive = 0;
    private boolean running = false;
 
    BroadcastReceiver keepLifeReceiver = new BroadcastReceiver() {
 
        @Override
        public void onReceive(Context context, Intent intent) {
            lastOnReceive = System.currentTimeMillis();
//            System.out.println("KeepLifeService.onReceive");
        }
    };
 
    @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);
//        System.out.println("ReadIDCardService.onCreate");
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String type = intent.getStringExtra("type");
//        System.out.println("ReadIDCardService.onStartCommand " + type);
        if (type == null) {
            type = "start_service";
        }
        switch (type) {
            case "start_service":
                startReader();
                break;
            case "stop_service":
                stopReader();
                break;
            case "close_service":
                closeReader();
                break;
            default:
                break;
        }
 
        if (!first) {
            first = true;
            Notification.Builder builder = new Notification.Builder(this.getApplicationContext());
            Intent nfIntent = new Intent(this, MainActivity.class);
            builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0))
                    //设置通知栏大图标
                    .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.back))
                    //设置服务标题
                    .setContentTitle("服务")
                    //设置状态栏小图标
                    .setSmallIcon(R.drawable.back)
                    //设置服务内容
                    .setContentText("服务")
                    //设置通知时间
                    .setWhen(System.currentTimeMillis());
            Notification notification = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                notification = builder.build();
            }
            //设置通知的声音
            notification.defaults = Notification.DEFAULT_SOUND;
            startForeground(110, notification);
        }
 
        return super.onStartCommand(intent, flags, startId);
    }
    boolean first = false;
    private boolean close = false;
    private void closeReader() {
        if (1==1) {
            return;
        }
        synchronized (this) {
            if (!close) {
                close = true;
            } else {
                return;
            }
        }
        inReader = false;
        System.out.println("ReadIDCardService.closeReader");
        readUtil.closeUSBDevice();
        Intent i = new Intent();
        i.putExtra("close", true);
        i.setPackage("com.basic.security");
        i.setAction("read.id.card.data");
        sendBroadcast(i);
        stopSelf();
    }
    ExecutorService executorService = Executors.newCachedThreadPool();
    public long readCardThreadLastRunning;
    private void startReader() {
        if (!inReader) {
//            System.out.println("ReadIDCardService.startReader");
//            Log.e("project.id.card", "start");
            if (System.currentTimeMillis() - readCardThreadLastRunning > 5*1000) {
                inReader = true;
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        while (inReader) {
                            try {
                                readCardThreadLastRunning = System.currentTimeMillis();
                                if (readUtil.isPort()) {
                                    ThreadUtil.threadCreateObserver();
                                    MemoryUtil.monitorMemory();
                                    IDCard idCard = readUtil.readCard();
                                    if (idCard != null) {
                                        String photoFileName =  idCard.getIdPhoto();
                                        FileUtils.copyFile(new File(photoFileName), new File("/sdcard/security_yuyin_attachment/"+idCard.getIdNum()+".bmp"));
//                                        System.out.println("ReadIDCardService.run photoFileName=" + photoFileName);
//                                        Bitmap headBitmap = BitmapFactory.decodeFile(photoFileName);
                                        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();
                                        System.out.println("ReadIDCardService.run " + c.name);
                                        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);
                                    }
                                } else {
//                                    System.out.println("ReadIDCardService.run " + "连接usb设备失败,开始关闭");
                                    closeReader();
                                    SystemClock.sleep(10*1000);
                                }
//                                SystemClock.sleep(2*1000);
                            } catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }
 
        } else {
//            System.out.println("ReadIDCardService.startReader");
        }
    }
    private void stopReader() {
        if (1==1) {
            return;
        }
        if (inReader) {
            System.out.println("ReadIDCardService.stopReader");
            inReader = false;
        } else {
            System.out.println("ReadIDCardService.stopReader");
        }
    }
    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();
    }
 
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(keepLifeReceiver);
        running = false;
        stopForeground(true);
//        System.out.println("KeepLifeService.onDestroy");
    }
}