当前位置:   article > 正文

如何使用zxing扫描QR code(二维码)

qrcode和zxing扫码

1.简介:使用Google的开源库 Zxing,但是网上多半的使用教程都是比较早的,这里给出我总结的一些基础代码和使用规则:

首先要一定要先去官网看看:

github-Zxing官方库的地址
github-zxing-android-embedded 一个非常好用的android工具

 

2.导入

如果是使用android studio, 那么在gradle文件里添加以下:

  1. compile 'com.google.zxing:core:3.2.1'
  2. 或者
  3. compile group: 'com.google.zxing', name: 'core', version: '3.2.1'

 再导入 ZXing Android Embedded

 compile 'com.journeyapps:zxing-android-embedded:3.3.0'

 

3.扫描:

使用扫描的时候,是用到系统的服务的,是从当前的 MainActivity 跳转到 CustomScanActivity

扫描样式可以自定义

MainActivity中:

  1. // 你也可以使用简单的扫描功能,但是一般扫描的样式和行为都是可以自定义的,这里就写关于自定义的代码了
  2. // 你可以把这个方法作为一个点击事件
  3. public void customScan(){
  4. new IntentIntegrator(this)
  5. .setOrientationLocked(false)
  6. .setCaptureActivity(CustomScanActivity.class) // 设置自定义的activity是CustomActivity
  7. .initiateScan(); // 初始化扫描
  8. }

 这样就跳转到CustomActivity扫描,如果不跳转,则不需要.setCaptureActivity(CustomScanActivity.class),直接在当前页面扫描

 

以下方法获取结果:

  1. @Override
  2. // 通过 onActivityResult的方法获取 扫描回来的 值
  3. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  4. IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
  5. if(intentResult != null) {
  6. if(intentResult.getContents() == null) {
  7. Toast.makeText(this,"内容为空",Toast.LENGTH_LONG).show();
  8. } else {
  9. Toast.makeText(this,"扫描成功",Toast.LENGTH_LONG).show();
  10. // ScanResult 为 获取到的字符串
  11. String ScanResult = intentResult.getContents();
  12. }
  13. } else {
  14. super.onActivityResult(requestCode,resultCode,data);
  15. }
  16. }

 

 

下面是xml样式:

  1. <!-- 我这里只是在大局下修改了一些样式,不过其实 扫描框中的 各种激光条,边框都可以改变,有兴趣的同学可以自己去搜一下 -->
  2. <!-- 这个控件就是扫描的窗口了 -->
  3. <com.journeyapps.barcodescanner.DecoratedBarcodeView
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:id="@+id/dbv_custom"
  7. app:zxing_framing_rect_width="200dp"
  8. app:zxing_framing_rect_height="50dp"
  9. app:zxing_preview_scaling_strategy="fitXY"
  10. app:zxing_use_texture_view="true"
  11. >
  12. </com.journeyapps.barcodescanner.DecoratedBarcodeView>

 

参考:

1.http://blog.csdn.net/qq_28057541/article/details/52034988

2.http://blog.csdn.net/u013718120/article/details/51683125

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/715817
推荐阅读
相关标签
  

闽ICP备14008679号