怎么用NFC手机读身份证信息


下面是步骤:

 

1.获取管理权限:这个开发包里已经集成了,只需要调用一句代码就可以搞定,这个还是挺方便的。

 

        PermissionUtil.grantNeedPermission(this);

 2.打开NFC,并监听NFC事件,这段如果不懂,直接在网上搜安卓使用NFC就可以看到介绍

 

 

private NfcAdapter      mAdapter = null; // 定义NFC适配器

mAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());

pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())

                .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);//.ACTION_TAG_DISCOVERED);

tagDetected.addCategory(Intent.CATEGORY_DEFAULT);

mTechLists = new String[][]{new String[]{NfcB.class.getName()}, new String[]{NfcA.class.getName()}};

if (mAdapter != null && !mAdapter.isEnabled()) {

    Toast.makeText(this, "NFC尚未开启", Toast.LENGTH_SHORT).show();

}

        3.因为我的NFC事件是在子线程中的,所以new了一个handle

 

private static class MyHandler extends Handler {

        private final WeakReference<IDCardScannerActivity> mActivity;

 

        public MyHandler(IDCardScannerActivity activity) {

            mActivity = new WeakReference<IDCardScannerActivity>(activity);

        }

 

        @Override

        public void handleMessage(Message msg) {

            System.out.println(msg);

            if (mActivity.get() == null) {

                return;

            }

            if (msg.what == 1002)

                throw new RuntimeException();

            else {

                try {

                    mActivity.get().todo(msg);

                } catch (FileNotFoundException e) {

                    e.printStackTrace();

                }

            }

        }

    }

        4.初始化类:

 

private OTGReadCardAPI  ReadCardAPI; // 定义读取类

 

ReadCardAPI = new OTGReadCardAPI(getApplicationContext(), this, false); // 初始化类,第三个是回调地址,第三个直接传false就可以了

 

// 设置服务器地址及端口

ArrayList<Serverinfo> twoCardServerlist = new ArrayList<Serverinfo>();

twoCardServerlist.add(new Serverinfo("id.yzfuture.cn", 8848));

ReadCardAPI.setServerInfo(twoCardServerlist, null, bTestServer);

        5.当手机检测到芯片靠近的时候,会回调onNewIntent,在这个里面直接 读取即可。但是为避免出现问题,我直接发送消息到 Handle里面处理。

 

@Override

    protected void onNewIntent(Intent intent) {

        super.onNewIntent(intent);

        inintent = intent;

        mHandler.sendEmptyMessageDelayed(1, 0);

    }

6.MyHandler 中会调到todo里面,这里面是具体的操作

 

 

    public void todo(Message msg) throws FileNotFoundException {

        int tt = 0;

        m_szProcess = "";

        process.setText("");

        m_szAppKey = sharedPreferences.getString("AppKey", "");

        int     nnum = ReadCardAPI.GetAppKeyUseNum(m_szAppKey);

        if (nnum <= 0) nnum = 0;

        numtxt.setText(String.valueOf(nnum));

        if (m_szAppKey.isEmpty() || nnum<=0)

        {

            regeditAppkey(true);

        }

        appkeyTxt.setText(m_szAppKey);

        if (!m_szAppKey.isEmpty() && nnum>0)

        {

            if (msg.what == 1 || msg.what==1005)

            {

                if (msg.what == 1005)

                {

                    inintent = null;

                }

                tt = ReadCardAPI.NfcReadCard(m_szAppKey, null, inintent, eCardType.eTwoGeneralCard, "", m_bAuthon);

            }

            if (m_bshow)

            {

                userInfo.setText(m_szUserInfo);

            }

            if (tt == 41)

            {

                if (m_berror)

                {

                    setdialog("读卡失败!" + ReadCardAPI.GetErrorInfo());

                }

                else

                {

                    int     nerr = ReadCardAPI.GetErrorCode();

                    if (nerr==-24997)

                    {

                        setdialog("此设备没有解码权限!");

                    }

                    else setdialog("读卡失败!");

                }

 

                edtype.setText("");

                edname.setText("");

                edenname.setText("");

                edid.setText("");

 

                addTrue.setText("");

                sexTrue.setText("");

                mzTrue.setText("");

                jgTrue.setText("");

                birthTrue.setText("");

 

                yxqTrue.setText("");

                gjTrue.setText("");

                process.setText("");

            }

            if (tt == 90)

            {

                if (ReadCardAPI.GetTwoCardInfo().arrTwoIdPhoto != null) {

                    idimg.setBackground(new BitmapDrawable(Bytes2Bimap(ReadCardAPI.GetTwoCardInfo().arrTwoIdPhoto)));

                }

                if (ReadCardAPI.GetTwoCardInfo().szTwoType.equals("J")) {

                    edtype.setText("港澳居民居住证");

                    othernoTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoOtherNO);

                    signTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoSignNum);

                } else {

                    edtype.setText("居民二代证");

                    othernoTrue.setText("");

                    signTrue.setText("");

                }

                edid.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdNo.trim());

                edname.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdName.trim());

                addTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdAddress.trim());

                sexTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdSex.trim());

                mzTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdNation.trim() + "");

                gjTrue.setText("中国");

                jgTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdSignedDepartment.trim());

                birthTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdBirthday.substring(0, 4) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdBirthday.substring(4, 6) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdBirthday.substring(6, 8));

 

                String startDate = ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodBegin.substring(0, 4) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodBegin.substring(4, 6) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodBegin.substring(6, 8);

                String endDate = "";

                int nlen = ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodEnd.indexOf("长期");

                if (nlen != -1) {

                    endDate = "长期";

                } else {

                    endDate = ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodEnd.substring(0, 4) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodEnd.substring(4, 6) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodEnd.substring(6, 8);

                }

                yxqTrue.setText(startDate + "~" + endDate);

                //process.setText("100");

                ReadCardAPI.release();

            }

        }

    }

到此就读取完了。

如果需要原始的SDK和文档,可以去以下链接下载。

 

https://download.csdn.net/user/cdyzfuture/uploads

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM