Blame view

src/com/ectrip/cyt/ui/BlackIdCardActivity.java 12.3 KB
5acca6a8   杜方   畅游通核销app: 1.补漏上次未上传
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
  package com.ectrip.cyt.ui;
  
  import android.content.Intent;
  import android.media.MediaPlayer;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.Message;
  import android.text.TextUtils;
  import android.widget.Toast;
  
  import com.ectrip.cyt.config.DevicTool;
  import com.ectrip.cyt.utils.CountDownTimer;
  import com.ectrip.cyt.utils.LogUtil;
  import com.ectrip.cyt.utils.NationDeal;
  import com.ectrip.cyt.utils.SharedPreferences2Obj;
  import com.ectrip.trips.check.R;
  import com.ivsign.android.IDCReader.IDCReaderSDK;
  
  import java.io.IOException;
  import java.util.Date;
  
  import android_serialport_api.sample.SerialPortActivity;
  
  /**
   * 作者:dufang on 2023/11/10 10:45
   */
  public class BlackIdCardActivity extends SerialPortActivity {
      private Integer type; // 类型
      MediaPlayer player;
  
      /**
       * 执行扫描,扫描后的结果会通过actionPosApi.ACTION_POS_COMM_STATUS的广播发回
       */
      Handler handler = new Handler() {
          @Override
          public void handleMessage(Message msg) {
              super.handleMessage(msg);
  
              switch (msg.what) {
                  case 0:
                      try {
                          if (Readflage > 0) {
                              player.start();
                              Toast.makeText(BlackIdCardActivity.this, "身份号码:" + decodeInfo[5], Toast.LENGTH_SHORT).show();
                              Intent intent = new Intent(BlackIdCardActivity.this,
                                      IDOrderListActivity.class);
                              CountDownTimer.firstTime = new Date().getTime();// 不必要的可以删除
                              intent.putExtra("mode", 0);
                              intent.putExtra("idcardNumber", decodeInfo[5]);
                              intent.putExtra("titleName", getString(R.string.order_list));
                              startActivity(intent);
                          } else if (Readflage == -2) {
                              Toast.makeText(BlackIdCardActivity.this, "连接异常:", Toast.LENGTH_SHORT).show();
                          } else if (Readflage == -3) {
                              Toast.makeText(BlackIdCardActivity.this, "无卡或卡片已读过-3:", Toast.LENGTH_SHORT).show();
                          } else if (Readflage == -4) {
                              Toast.makeText(BlackIdCardActivity.this, "无卡或卡片已读过-4:", Toast.LENGTH_SHORT).show();
                          } else if (Readflage == -5) {
                              Toast.makeText(BlackIdCardActivity.this, "读卡失败:", Toast.LENGTH_SHORT).show();
                          } else if (Readflage == -99) {
                              Toast.makeText(BlackIdCardActivity.this, "操作异常:", Toast.LENGTH_SHORT).show();
                          }
  
                          Thread.sleep(100);
                      } catch (InterruptedException e) {
                          Toast.makeText(BlackIdCardActivity.this, "读取数据异常:", Toast.LENGTH_SHORT).show();
                      }
                      break;
  
                  case 1:
                      Toast.makeText(BlackIdCardActivity.this, "发现身份证:", Toast.LENGTH_SHORT).show();
                      break;
  
                  case 3:
                      try {
                          Bundle bundle = msg.getData();
                          String id = bundle.getString("id");
                          if (TextUtils.isEmpty(id)) return;
                          player.start();
  //                        Toast.makeText(SelectActionActivity.this, "身份号码:" + id, Toast.LENGTH_SHORT).show();
                          Intent intent = new Intent(BlackIdCardActivity.this,
                                  IDOrderListActivity.class);
                          CountDownTimer.firstTime = new Date().getTime();// 不必要的可以删除
                          intent.putExtra("mode", 0);
                          intent.putExtra("idcardNumber", id);
                          intent.putExtra("titleName", getString(R.string.order_list));
                          startActivity(intent);
                      } catch (IllegalStateException e) {
                          e.printStackTrace();
                      }
                      break;
                  default:
                      break;
              }
  
  
          }
      };
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_black_idcard);
          initIDRead();
          init();
      }
  
      @Override
      protected void onDataReceived(byte[] buffer, int size) {
  
      }
  
      private void init() {
          type = SharedPreferences2Obj.getInstance(BlackIdCardActivity.this)
                  .setName("MachineType").getObject("type", Integer.class);
          // 扫描提示音
          player = MediaPlayer.create(getApplicationContext(),
                  R.raw.beep);
      }
  
      private void initIDRead() {
          new Thread(new ThreadRun()).start();
      }
  
      /**
       * 读取身份证线程
       */
      private boolean isRun = true;
      private boolean isOpen = true;
  
      private class ThreadRun implements Runnable {
          @Override
          public void run() {
              while (isRun) {
                  try {
                      Thread.sleep(200);
                      if (isOpen)
                          ReadCard();
                  } catch (InterruptedException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
          }
      }
  
      /**
       * 读取身份证
       */
      private int Readflage = -99;
      byte[] cmd_find = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x03, 0x20, 0x01, 0x22};
      byte[] cmd_selt = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x03, 0x20, 0x02, 0x21};
      byte[] cmd_read = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x03, 0x30, 0x01, 0x32};
      byte[] recData = new byte[1500];
      String[] decodeInfo = new String[10];
  
      private void ReadCard() {
          try {
              LogUtil.d(TAG,"mInputStream"+mInputStream+"\n"+"mOutputStream"+mOutputStream);
              if ((mInputStream == null) || (mOutputStream == null)) {
                  Readflage = -2;// 连接异常
                  return;
              }
              mOutputStream.write(cmd_find);
              Thread.sleep(200);
              int datalen = mInputStream.read(recData);
              if (recData[9] == -97) {
                  mOutputStream.write(cmd_selt);
                  Thread.sleep(200);
                  datalen = mInputStream.read(recData);
                  if (recData[9] == -112) {
                      mOutputStream.write(cmd_read);
                      Thread.sleep(1000);
                      byte[] tempData = new byte[1500];
                      if (mInputStream.available() > 0) {
                          datalen = mInputStream.read(tempData);
                      } else {
                          Thread.sleep(500);
                          if (mInputStream.available() > 0) {
                              datalen = mInputStream.read(tempData);
                          }
                      }
                      int flag = 0;
                      if (datalen < 1294) {
                          for (int i = 0; i < datalen; i++, flag++) {
                              recData[flag] = tempData[i];
                          }
                          Thread.sleep(1000);
                          if (mInputStream.available() > 0) {
                              datalen = mInputStream.read(tempData);
                          } else {
                              Thread.sleep(500);
                              if (mInputStream.available() > 0) {
                                  datalen = mInputStream.read(tempData);
                              }
                          }
                          for (int i = 0; i < datalen; i++, flag++) {
                              recData[flag] = tempData[i];
                          }
                      } else {
                          for (int i = 0; i < datalen; i++, flag++) {
                              recData[flag] = tempData[i];
                          }
                      }
                      tempData = null;
                      if (flag == 1295) {
                          if (recData[9] == -112) {
  
                              byte[] dataBuf = new byte[256];
                              for (int i = 0; i < 256; i++) {
                                  dataBuf[i] = recData[14 + i];
                              }
                              String TmpStr = new String(dataBuf, "UTF16-LE");
                              TmpStr = new String(TmpStr.getBytes("UTF-8"));
                              decodeInfo[0] = TmpStr.substring(0, 15);
                              decodeInfo[1] = TmpStr.substring(15, 16);
                              decodeInfo[2] = TmpStr.substring(16, 18);
                              decodeInfo[3] = TmpStr.substring(18, 26);
                              decodeInfo[4] = TmpStr.substring(26, 61);
                              decodeInfo[5] = TmpStr.substring(61, 79);
                              decodeInfo[6] = TmpStr.substring(79, 94);
                              decodeInfo[7] = TmpStr.substring(94, 102);
                              decodeInfo[8] = TmpStr.substring(102, 110);
                              decodeInfo[9] = TmpStr.substring(110, 128);
                              if (decodeInfo[1].equals("1"))
                                  decodeInfo[1] = "男";
                              else
                                  decodeInfo[1] = "女";
                              try {
                                  int code = Integer.parseInt(decodeInfo[2]
                                          .toString());
                                  decodeInfo[2] = NationDeal.decodeNation(code);
                              } catch (Exception e) {
                                  decodeInfo[2] = "";
                              }
                              // 照片解码
                              try {
                                  int ret = IDCReaderSDK.Init();
                                  if (ret == 0) {
                                      byte[] datawlt = new byte[1384];
                                      byte[] byLicData = {(byte) 0x05,
                                              (byte) 0x00, (byte) 0x01,
                                              (byte) 0x00, (byte) 0x5B,
                                              (byte) 0x03, (byte) 0x33,
                                              (byte) 0x01, (byte) 0x5A,
                                              (byte) 0xB3, (byte) 0x1E,
                                              (byte) 0x00};
                                      for (int i = 0; i < 1295; i++) {
                                          datawlt[i] = recData[i];
                                      }
                                      int t = IDCReaderSDK.unpack(datawlt,
                                              byLicData);
                                      if (t == 1) {
                                          Readflage = 1;// 读卡成功
                                      } else {
                                          Readflage = 6;// 照片解码异常
                                      }
                                  } else {
                                      Readflage = 6;// 照片解码异常
                                  }
                              } catch (Exception e) {
                                  Readflage = 6;// 照片解码异常
                              }
                              handler.sendEmptyMessage(0);
                          } else {
                              Readflage = -5;// 读卡失败!
                          }
                      } else {
                          Readflage = -5;// 读卡失败
                      }
                  } else {
                      Readflage = -4;// 选卡失败
                  }
              } else {
                  Readflage = -3;// 寻卡失败
              }
          } catch (IOException e) {
              // TODO Auto-generated catch block
              Readflage = -99;// 读取数据异常
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              Readflage = -99;// 读取数据异常
          }
      }
  
      @Override
      protected void onDestroy() {
          // TODO Auto-generated method stub
          super.onDestroy();
          if (DevicTool.isHiboryPos()) {
              isRun = false;
          }
      }
  }