ard.io开源的银行卡扫描的三方库真的是很好用啊。
首先需要在你的module的gradle的依赖文件中添加依赖
compile 'io.card:android-sdk:5.5.1'
2 清单文件中加入如下Activity
3 xml文件中
<&#63;xml version="1.0" encoding="utf-8"&#63;>
全部代码:
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地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。