怎么用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