欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

详解Android 扫描条形码(Zxing插件)

程序员文章站 2024-02-24 18:21:46
使用android studio 一、在build.gradle(module:app)添加代码  下载,调用插件 apply plugin: 'co...

使用android studio

一、在build.gradle(module:app)添加代码  下载,调用插件

apply plugin: 'com.android.application'
android {
 compilesdkversion 24
 buildtoolsversion "24.0.1"

 defaultconfig {
  applicationid "com.example.ly.scanrfid"
  minsdkversion 19
  targetsdkversion 24
  versioncode 1
  versionname "1.0"
 }
 buildtypes {
  release {
   minifyenabled false
   proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'
  }
 }
 repositories {
  mavencentral()
  maven {
  url "http://dl.bintray.com/journeyapps/maven"
  }
 }
}
dependencies {
 compile filetree(dir: 'libs', include: ['*.jar'])
 testcompile 'junit:junit:4.12'
 compile 'com.android.support:appcompat-v7:24.2.1'
 // supports android 4.0.3 and later (api level 15)
 compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar'
 // supports android 2.1 and later (api level 7), but not optimal for later android versions.
 // if you only plan on supporting android 4.0.3 and up, you don't need to include this.
 compile 'com.journeyapps:zxing-android-legacy:2.0.1@aar'
 // convenience library to launch the scanning and encoding activities.
 // it automatically picks the best scanning library from the above two, depending on the
 // android version and what is available.
 compile 'com.journeyapps:zxing-android-integration:2.0.1@aar'
 // version 3.0.x of zxing core contains some code that is not compatible on android 2.2 and earlier.
 // this mostly affects encoding, but you should test if you plan to support these versions.
 // older versions e.g. 2.2 may also work if you need support for older android versions.
 compile 'com.google.zxing:core:3.0.1'
}

二、添加权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.ly.scanrfid">
 <uses-permission android:name="android.permission.camera"/>
 <uses-permission android:name="android.permission.vibrate"/>
 <uses-permission android:name="android.permission.internet"/>
 <application
  android:allowbackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:supportsrtl="true"
  android:theme="@style/apptheme">
  <activity android:name=".mainactivity">
   <intent-filter>
    <action android:name="android.intent.action.main"/>
    <category android:name="android.intent.category.launcher"/>
   </intent-filter>
  </activity>
 </application>
</manifest>

三、activity代码

package com.example.ly.scanrfid;
import android.content.intent;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.toast;
import com.google.zxing.integration.android.intentintegrator;
import com.google.zxing.integration.android.intentresult;
public class mainactivity extends appcompatactivity {
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
 }
 // 扫描按钮点击监听事件
 public void clickscan(view view) {
  //扫描操作
  intentintegrator integrator = new intentintegrator(mainactivity.this);
  integrator.initiatescan();
 }
 @override
 protected void onactivityresult(int requestcode, int resultcode, intent data) {
  // 跳转扫描页面返回扫描数据
  intentresult scanresult = intentintegrator.parseactivityresult(requestcode, resultcode, data);
  //  判断返回值是否为空
  if (scanresult != null) {
   //返回条形码数据
   string result = scanresult.getcontents();
   log.d("code", result);
   toast.maketext(this, result, toast.length_long).show();
  }
 }
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!