温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-12-04 17:31:11 来源:亿速云 阅读:211 作者:Leah 栏目:移动开发

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

最快的调用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实现一个扫码功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI