如何在Androidsdutio项目中利用Zxing实现一个扫码功能

今天就跟大家聊聊有关如何在Android sdutio项目中利用Zxing实现一个扫码功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

平山网站建设公司创新互联,平山网站设计制作,有大型网站制作公司丰富经验。已为平山近1000家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的平山做网站的公司定做!

最快的调用Zxing方法

1.关联第三方库

2.调用基础的扫码

3.获取返回值

具体代码如下:

//1.默认选项启动意图
new IntentIntegrator(MainActivity.this).initiateScan(); // `this` is the current Activity
//2.获取得到的结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
 if(result != null) {
  if(result.getContents() == null) {
   Toast.makeText(this, "取消扫码", Toast.LENGTH_LONG).show();
  } else {
   Toast.makeText(this, "扫码结果:" + result.getContents(), Toast.LENGTH_LONG).show();
  }
 } else {
  super.onActivityResult(requestCode, resultCode, data);
 }
}

MainActivity.java

apply plugin: 'com.android.application'
android {
 compileSdkVersion 25
 buildToolsVersion "25.0.2"
 defaultConfig {
  applicationId "wei.shm.zxingscancode"
  minSdkVersion 15
  targetSdkVersion 25
  versionCode 1
  versionName "1.0"
  testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
 }
 buildTypes {
  release {
   minifyEnabled false
   proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  }
 }
}
dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  exclude group: 'com.android.support', module: 'support-annotations'
 })
 compile 'com.android.support:appcompat-v7:25.2.0'
 compile 'com.android.support.constraint:constraint-layout:1.0.1'
 testCompile 'junit:junit:4.12'
 //新建项目只增加这个
 compile 'com.journeyapps:zxing-android-embedded:3.5.0'
 //需要核对的有:
 //1.备置仓库:repositories里的jcenter()
 //2.com.android.support:appcompat-v7:版本号必须大于23以上
 //3.buildToolsVersion:版本需要大于等于23.0.2,旧版本可能会导致编译错误
 //以上条件都满足则只需要添加 compile 'com.journeyapps:zxing-android-embedded:3.5.0'
}

IntentIntegrator相关方法注解翻译

setCaptureActivity:设置活动类使用。它可以是任何活动,但应处理的意图额外使用这里。

setPrompt:设置一个提示显示在捕捉屏幕上,而不是使用默认。

setOrientationLocked:默认情况下,方向锁定。设置为false不锁定。

setCameraId:使用指定的相机ID。

setBeepEnabled:设置为false禁用扫描的哔哔声。

setBarcodeImageEnabled:设置为true,以便在结果意图中保存条形码图像并发送其路径。

setDesiredBarcodeFormats:设置所需的条码格式扫描。

initiateScan:启动扫描所有已知的条形码类型与默认相机。

setTimeout:启动扫描所有已知的条形码类型与默认相机。并启动计时器超时完成

createScanIntent:使用指定选项创建扫描意图。

看完上述内容,你们对如何在Android sdutio项目中利用Zxing实现一个扫码功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


新闻标题:如何在Androidsdutio项目中利用Zxing实现一个扫码功能
本文链接:http://myzitong.com/article/jiciei.html