不得不说card.io开源的银行卡扫描的三方库真的是很好用啊,非常稀饭,使用简单,而且给你提供的API很多,满足大部分的需要(反正是满足我的各种需求了)
首先上一个效果图
1 对准银行卡的四角就能够扫描出来银行卡信息,你集成的话首先需要在你的module的gradle的依赖文件中添加一句话,最好使用最新的版本号(后面就是最新的版本号的意思,因为人家辛辛苦苦修复了问题,不用多不合适)
compile 'io.card:android-sdk:latestversioncode'
2 清单文件中加入如下Activity
<activity
android:name="io.card.payment.CardIOActivity"
android:configChanges="keyboardHidden|orientation"/>
<activity android:name="io.card.payment.DataEntryActivity"/>
3 xml文件中
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onScanPress"
android:text="银行卡识别"/>
<TextView
android:id="@+id/card_tv"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="银行卡号"/>
4 代码中
这里面暴露了很多的API,大致查了一下有二十多个,比如是否隐藏LOGO标志,是否需要银行卡有效期的信息,扫描线的颜色(默认是Color.GREEN),是否需要CVV信息,邮政编码等,下面对其介绍一下
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_EXPIRY, false); // default: false不需要 是否需要失效日期等信息
scanIntent.putExtra(CardIOActivity.EXTRA_HIDE_CARDIO_LOGO, false); // default: false 是否隐藏LOGO标记
scanIntent.putExtra(CardIOActivity.EXTRA_USE_CARDIO_LOGO, true); // default: false 是否使用card.io LOGO
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_POSTAL_CODE, false); // default: false不需要 邮政编码
scanIntent.putExtra(CardIOActivity.EXTRA_GUIDE_COLOR, Color.GREEN); // default: Color.GREEN 扫描线的颜色
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_CVV, false); // default: false不需要 CVV信息
全部代码如下
/**
* 银行卡识别
*/
public class CardIdentifyActivity extends BaseActivity {
public static final int MY_SCAN_REQUEST_CODE = 10;
public TextView mTextView;
@Override
protected int getLayoutId() {
return R.layout.activity_card_identify;
}
@Override
protected void initView() {
mTextView = (TextView) findViewById(R.id.card_tv);
}
@Override
protected void afterInitView() {
super.afterInitView();
EventBus.getDefault().register(this);
}
@Subscribe
public void onEventMainThread(UpdateCardNumberEvent event) {
if (!TextUtils.isEmpty(event.getNumber())) {
mTextView.setText(event.getNumber());
}
}
public void onScanPress(View view) {
Intent scanIntent = new Intent(this, CardIOActivity.class);
// customize these values to suit your needs.
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_EXPIRY, false); // default: false不需要 是否需要失效日期等信息
scanIntent.putExtra(CardIOActivity.EXTRA_HIDE_CARDIO_LOGO, false); // default: false 是否隐藏LOGO标记
scanIntent.putExtra(CardIOActivity.EXTRA_USE_CARDIO_LOGO, true); // default: false 是否使用card.io LOGO
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_POSTAL_CODE, false); // default: false不需要 邮政编码
scanIntent.putExtra(CardIOActivity.EXTRA_GUIDE_COLOR, Color.GREEN); // default: Color.GREEN 扫描线的颜色
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_CVV, false); // default: false不需要 CVV信息
// MY_SCAN_REQUEST_CODE is arbitrary and is only used within this activity.
startActivityForResult(scanIntent, MY_SCAN_REQUEST_CODE);
}
@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 = "Card Number: " + scanResult.cardNumber + "\n";
Log.i("aaa", "银行卡号:" + resultDisplayStr);
EventBus.getDefault().post(new UpdateCardNumberEvent(resultDisplayStr));
// Do something with the raw number, e.g.:
// myService.setCardNumber( scanResult.cardNumber );
if (scanResult.isExpiryValid()) {
resultDisplayStr += "Expiration Date: " + scanResult.expiryMonth + "/" + scanResult.expiryYear + "\n";
Log.i("aaa", "银行卡号有效期:" + resultDisplayStr);
}
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.";
}
// do something with resultDisplayStr, maybe display it in a textView
// resultTextView.setText(resultDisplayStr);
}
// else handle other activity results
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
最后扫描结果如下
最后附上github地址
https://github.com/card-io/card.io-Android-SDK