BlackIdCardActivity.java 12.3 KB
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;

    /**
     * 执行扫描,扫描后的结果会通过action为PosApi.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;
        }
    }
}