关于Zxing的使用,网上有大量优秀的文章讲解,本文不再叙述。
用过Zxing的童鞋都知道Zxing扫描识别率较低,速度较慢。这里介绍一个第三方库,github地址为:
https://github.com/BayMax-Yi/BayMaxScanner 该库基于zxing做了修改,优化二维码、条形码识别的速度。以及封装了生成二
维码,条形码功能。话不多说,先上效果图:
该库的使用步骤:
1.添加依赖
在Project的build.gradle下添加
allprojects {
repositories {
……
maven { url 'https://jitpack.io' }
}
}
在app的build.gradle下添加
dependencies {
……
implementation 'com.github.BayMax-Yi:BayMaxScanner:1.0.1'
}
3.在你的CaptureActivity中初始化PreviewHelper
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_capture);previewHelper = new PreviewHelper();previewHelper.setScanType( PreviewHelper.SCANTYPE_QR);previewHelper.setActivity(this);previewHelper.setSurfaceHolder(surfaceView.getHolder());previewHelper.setViewfinderView(viewfinderView);//Set onDecodeListener for PreviewHelper and deal the decoded result in onDecodeListener.onDecodedResult(Result) previewHelper.setOnDecodedResultListener(onDecodeListener);//setScanType PreviewHelper.SCANTYPE_QR : Scan QrCode // PreviewHelper.SCANTYPE_BARCIDE : Scan BarCode previewHelper.onCreate();
}
4.完成生命周期的回调
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);......previewHelper.onCreate();
}@Override
protected void onResume() {super.onResume();previewHelper.onResume();
}@Override
protected void onPause() {super.onPause();previewHelper.onPause();
}@Override
protected void onDestroy() {super.onDestroy();previewHelper.onDestroy();
}
OK,集成就这么简单,大功告成。