如何在Android项目中通过扫描银行卡获取银行卡号?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
首先需要在你的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); } } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。