package com.ectrip.cyt.ui; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.concurrent.atomic.AtomicBoolean; import android.annotation.SuppressLint; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.ectrip.cyt.config.MyApp; import com.ectrip.cyt.shield_home.LockLayer.MToast; import com.ectrip.cyt.utils.LogUtil; import com.ectrip.cyt.utils.SharedPreferences2Obj; import com.ectrip.trips.check.R; import com.fri.idcread.idcread; /** * 手持机身份证扫描页面 * * @author jigo */ public class ScanerIdCardActivity extends BaseActivity { private Button topBtns; private TextView topBack; private Dialog dialog = null; private String shuju; private Button readIDButton; private String titleName; private AtomicBoolean isStop = new AtomicBoolean(false); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.read_idcard); initView(); idcread.init(); } private void initView() { try { SharedPreferences2Obj.getInstance(ScanerIdCardActivity.this) .setName("SelectAction").setObject("isStatistic", "0"); // 非统计 } catch (Exception e) { e.printStackTrace(); } titleName = getIntent().getStringExtra("titleName"); if (titleName != null) { ((TextView) findViewById(R.id.title)).setText(titleName); } else { ((TextView) findViewById(R.id.title)).setText(R.string.scan_id); } ((TextView) findViewById(R.id.title)).setVisibility(View.VISIBLE); topBack = (TextView) findViewById(R.id.topBack); topBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { isStop.set(true); setpower("/sys/class/gpio/gpio28/value", false); idcread.CloseComm(); finish(); } }); topBtns = (Button) findViewById(R.id.topBtns); topBtns.setVisibility(View.GONE); readIDButton = (Button) findViewById(R.id.read); readIDButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { if (dialog == null) { Builder builder = new Builder(ScanerIdCardActivity.this); builder.setTitle(R.string.scan_id); builder.setMessage(R.string.loadiing); dialog = builder.create(); if (MyApp.getInstance().isHandset) { dialog.getWindow().setType( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); } dialog.setCanceledOnTouchOutside(true); dialog.setCancelable(true); dialog.show(); // readIdCard(0); new readTread(0).start(); } else { dialog.show(); // readIdCard(0); new readTread(0).start(); } } }); } /** * 读取身份证线程 * * @author jigo */ class readTread extends Thread { public int num; public readTread(int num) { this.num = num; } @Override public void run() { super.run(); try { readIdCard(num); } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { // TODO: handle exception } } } /** * 读取身份证 */ @SuppressLint("SdCardPath") public void readIdCard(int num) throws Throwable { if (idcread.Authenticate() == 1 && !isStop.get())/* 身份证认证 */ { LogUtil.d(TAG,getString(R.string.id_auth_success)); if (idcread.ReadContent("/mnt/sdcard/") == 1)/* 读取身份证数据 */// { LogUtil.d(TAG,getString(R.string.read_id_info_success)); try { testRead(); } catch (Exception e) { e.printStackTrace(); if (dialog != null && dialog.isShowing()) { try { dialog.dismiss(); } catch (Exception e1) { e1.printStackTrace(); } catch (Throwable e1) { e1.printStackTrace(); } } } } else { if (dialog != null && dialog.isShowing()) { try { dialog.dismiss(); } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } } LogUtil.d(TAG,getString(R.string.read_id_info_fail)); MToast(ScanerIdCardActivity.this, getString(R.string.read_id_info_fail), MToast.LENGTH_SHORT); } } else { num++; if (num <= 3) { readIdCard(num); } else { if (dialog != null && dialog.isShowing()) { try { dialog.dismiss(); dialog = null; } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } } num = 0; LogUtil.d(TAG,getString(R.string.id_auth_fail)); MToast(ScanerIdCardActivity.this, getString(R.string.id_stick_to_the_back), MToast.LENGTH_SHORT); } } } public final int ReadHandler = 10; @SuppressLint("HandlerLeak") public final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case ReadHandler: try { String id = msg.getData().getString("id"); String pwd = msg.getData().getString("pwd"); MToast(ScanerIdCardActivity.this, getString(R.string.id_number) + id, Toast.LENGTH_SHORT); trunto(id, pwd); } catch (Exception e) { e.printStackTrace(); } break; default: break; } } }; @Override public void onBackPressed() { super.onBackPressed(); isStop.set(true); setpower("/sys/class/gpio/gpio28/value", false); idcread.CloseComm(); this.finish(); } @Override protected void onResume() { super.onResume(); copyDatabase(); } @Override protected void onDestroy() { isStop.set(true); try { Thread.sleep(600); } catch (InterruptedException e) { } // closeDrive(); setpower("/sys/class/gpio/gpio28/value", false); idcread.CloseComm(); if (dialog != null) { dialog.dismiss(); dialog = null; } super.onDestroy(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_HOME) { isStop.set(true); // 在这里的处理和back一样 setpower("/sys/class/gpio/gpio28/value", false); idcread.CloseComm(); this.finish(); // return super.onKeyDown(keyCode, event); } else if (keyCode == KeyEvent.KEYCODE_BACK) { isStop.set(true); setpower("/sys/class/gpio/gpio28/value", false); idcread.CloseComm(); this.finish(); } return false; } public void setpower(String path, boolean on) { File file; FileWriter fr; try { file = new File(path); fr = new FileWriter(file); if (on) fr.write("1"); else fr.write("0"); fr.close(); } catch (IOException e) { Log.e(TAG, e.toString()); } } public void testRead() throws IOException { // file1.createNewFile(); FileInputStream in = new FileInputStream(idcread.file1); FileChannel channel = in.getChannel(); ByteBuffer buffer = ByteBuffer.allocate((int) idcread.file1.length()); channel.read(buffer); getRestore(buffer); if (in != null) { in.close(); } } public void getRestore(ByteBuffer oldBuf) { // System.out.println("hehe"); oldBuf.rewind(); int size = oldBuf.capacity(); ByteBuffer reBuffer = ByteBuffer.allocate(size + 2); byte[] heard = new byte[] { (byte) 0xFE, (byte) 0xEF }; reBuffer.put(heard); byte[] oldbyte = new byte[2]; while (oldBuf.position() < size) { oldBuf.get(oldbyte); reBuffer.put(getswap(oldbyte)); } try { shuju = new String(reBuffer.array(), "utf-16"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } } catch (Exception e) { e.printStackTrace(); if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } } chuli(); } private byte[] getswap(byte[] bb) { byte[] returnb = new byte[2]; returnb[0] = bb[1]; returnb[1] = bb[0]; return returnb; } public void chuli() { if (shuju == null) { return; } int a = shuju.length(); char[] temp = shuju.toCharArray(); shuju = null; StringBuffer nian = new StringBuffer(); StringBuffer yue = new StringBuffer(); StringBuffer ri = new StringBuffer(); StringBuffer id = new StringBuffer(); for (int i = 0; i < a; i++) { if (i >= 19 && i <= 22) { nian.append(temp[i]); } if (i >= 23 && i <= 24) { yue.append(temp[i]); } if (i >= 25 && i <= 26) { ri.append(temp[i]); } if (i >= 62 && i <= 79) { id.append(temp[i]); } } String pwd = nian.toString() + yue.toString() + ri.toString(); // trunto(id.toString(), pwd); Message message = new Message(); message.what = ReadHandler; Bundle data = new Bundle(); data.putString("id", id.toString()); data.putString("pwd", pwd); message.setData(data); handler.sendMessage(message); } /** * 跳转页面 * * @param id * @param pwd */ private void trunto(String id, String pwd) { if (id == null) { MToast(ScanerIdCardActivity.this, getString(R.string.get_id_fail), Toast.LENGTH_SHORT); return; } Intent intent = new Intent(ScanerIdCardActivity.this, IDOrderListActivity.class); intent.putExtra("mode", 0); intent.putExtra("idcardNumber", id); intent.putExtra("titleName",getString(R.string.order_list)); if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } startActivity(intent); isStop.set(true); setpower("/sys/class/gpio/gpio28/value", false); idcread.CloseComm(); finish(); } @Override protected void onPause() { isStop.set(true); try { Thread.sleep(600); } catch (InterruptedException e) { } // closeDrive(); setpower("/sys/class/gpio/gpio28/value", false); idcread.CloseComm(); if (dialog != null) { dialog.dismiss(); dialog = null; } super.onPause(); } // 把数据库复制到指定目录 public void copyDatabase() { try { // 获得li.db 绝对路径 String DATABASEFN = idcread.databasepath + idcread.databasefn; File dir = new File(idcread.databasepath); // 如果/sdcard/li目录中存在,创建这个目录 if (!dir.exists()) { // System.out.print("1"); dir.mkdir(); } // dir.mkdir(); // 如果在目录中不存在 // 文件,则从res\raw目录中复制这个文件到 if (!(new File(DATABASEFN).exists())) { // 获得封装 文件的InputStream对象 InputStream is = getResources().openRawResource(R.raw.license); // System.out.println("2"); FileOutputStream fos = new FileOutputStream(DATABASEFN); // System.out.println("2.1"); // 下面这一段代码不是很理解,有待研究 int length = is.available(); byte[] buffer = new byte[length]; // 8192 // System.out.println("3"); int count = 0; // 开始复制db文件 while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); // System.out.println("4"); } fos.close(); is.close(); } } catch (Exception e) { e.printStackTrace(); } } }