简单二维码扫描demo

本文详细介绍如何在Android应用中集成二维码扫描功能,包括必要的依赖库添加、权限申请及具体使用方法。

效果图(默认横屏,哪位大牛知道怎么改的记得告诉我,感激不尽):

首先在build.gradle(Moudle:app)中添加下列依赖:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. repositories {  
  2.     mavenCentral()  
  3.     maven {  
  4.         url "http://dl.bintray.com/journeyapps/maven"  
  5.     }  
  6. }  

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. dependencies {  
  2.     // Supports Android 4.0.3 and later (API level 15)  
  3.     compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar'  
  4.   
  5.     // Supports Android 2.1 and later (API level 7), but not optimal for later Android versions.  
  6.     // If you only plan on supporting Android 4.0.3 and up, you don't need to include this.  
  7.     compile 'com.journeyapps:zxing-android-legacy:2.0.1@aar'  
  8.   
  9.     // Convenience library to launch the scanning and encoding Activities.  
  10.     // It automatically picks the best scanning library from the above two, depending on the  
  11.     // Android version and what is available.  
  12.     compile 'com.journeyapps:zxing-android-integration:2.0.1@aar'  
  13.   
  14.     // Version 3.0.x of zxing core contains some code that is not compatible on Android 2.2 and earlier.  
  15.     // This mostly affects encoding, but you should test if you plan to support these versions.  
  16.     // Older versions e.g. 2.2 may also work if you need support for older Android versions.  
  17.     compile 'com.google.zxing:core:3.0.1'  
  18. }  

然后在AndroidManifest.xml文件<manifest>标签内部,<application>标签前面加入以下代码,申请权限

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <uses-permission android:name="android.permission.CAMERA"/>  
  2. <uses-permission android:name="android.permission.VIBRATE"/>  
  3. <uses-permission android:name="android.permission.INTERNET"/>  

这样就行了!

接下来是用法:

在按钮的onClick事件中添加代码,点击后会跳转到扫描界面:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2.     public void onClick(View v) {  
  3.         //扫描操作  
  4.         IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);  
  5.         integrator.initiateScan();  
  6.     }  
然后重写下列函数,用来处理解码后的信息:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  3.     IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);  
  4.     if (scanResult != null) {  
  5.         String result = scanResult.getContents();  
  6.         Log.d("code", result);  
  7.         Toast.makeText(this,result, Toast.LENGTH_LONG).show();  
  8.     }  
  9. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值