欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android银行卡扫描获取银行卡号

程序员文章站 2022-06-29 09:19:15
ard.io开源的银行卡扫描的三方库真的是很好用啊。 首先需要在你的module的gradle的依赖文件中添加依赖 compile 'io.card:andro...

ard.io开源的银行卡扫描的三方库真的是很好用啊。

首先需要在你的module的gradle的依赖文件中添加依赖

compile 'io.card:android-sdk:5.5.1'

2 清单文件中加入如下activity

<!-- permission to vibrate - recommended, allows vibration feedback on scan -->
 <uses-permission android:name="android.permission.vibrate" />

 <!-- permission to use camera - required -->
 <uses-permission android:name="android.permission.camera" />

 <!-- camera features - recommended -->
 <uses-feature android:name="android.hardware.camera" android:required="false" />
 <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
 <uses-feature android:name="android.hardware.camera.flash" android:required="false" />
<activity
  android:name="io.card.payment.cardioactivity"  
  android:configchanges="keyboardhidden|orientation" />
<activity android:name="io.card.payment.dataentryactivity" />

3 xml文件中

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.example.dell.scanbankdemo.mainactivity">

 <button
  android:id="@+id/btn_scan"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="扫描银行卡" />

 <textview
  android:id="@+id/tv_card_number"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="银行卡号:"
  />
</linearlayout>

全部代码:

public class mainactivity extends appcompatactivity implements view.onclicklistener {
 public static final int my_scan_request_code = 10;

 private button mscanbtn;
 private textview mnumbertv;//银行卡号

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  mscanbtn = findviewbyid(r.id.btn_scan);
  mscanbtn.setonclicklistener(this);
  mnumbertv = findviewbyid(r.id.tv_card_number);
 }

 @override
 public void onclick(view v) {
  switch (v.getid()) {
   case r.id.btn_scan:
    intent scanintent = new intent(this, cardioactivity.class);

    // customize these values to suit your needs.
    scanintent.putextra(cardioactivity.extra_require_expiry, true); // default: false
    scanintent.putextra(cardioactivity.extra_require_cvv, false); // default: false
    scanintent.putextra(cardioactivity.extra_require_postal_code, false); // default: false

    // my_scan_request_code is arbitrary and is only used within this activity.
    startactivityforresult(scanintent, my_scan_request_code);

    break;
  }
 }

 @override
 protected void onactivityresult(int requestcode, int resultcode, intent data) {
  super.onactivityresult(requestcode, resultcode, data);
  if (requestcode == my_scan_request_code) {
   string resultdisplaystr;
   if (data != null && data.hasextra(cardioactivity.extra_scan_result)) {
    creditcard scanresult = data.getparcelableextra(cardioactivity.extra_scan_result);

    // never log a raw card number. avoid displaying it, but if necessary use getformattedcardnumber()
    //resultdisplaystr = "银行卡号: " + scanresult.getredactedcardnumber() + "\n"; //只显示尾号
    resultdisplaystr = "银行卡号: " + scanresult.getformattedcardnumber() + "\n"; //显示银行卡号

    // do something with the raw number, e.g.:
    // myservice.setcardnumber( scanresult.cardnumber );

    if (scanresult.isexpiryvalid()) {
     resultdisplaystr += "有效期:" + scanresult.expirymonth + "/" + scanresult.expiryyear + "\n";
    }

    if (scanresult.cvv != null) {
     // never log or display a cvv
     resultdisplaystr += "cvv has " + scanresult.cvv.length() + " digits.\n";
    }

    if (scanresult.postalcode != null) {
     resultdisplaystr += "postal code: " + scanresult.postalcode + "\n";
    }
   } else {
    resultdisplaystr = "scan was canceled.";
   }
   mnumbertv.settext(resultdisplaystr);
   // do something with resultdisplaystr, maybe display it in a textview
   // resulttextview.settext(resultdisplaystr);
  }
 }
}

附上github地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。