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

使用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(); } } }

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

时间: 2024-07-30 01:57:32

详解Android 扫描条形码(Zxing插件)的相关文章

Android开发:详解Android SDK与ADT插件

据权威数据显示:目前最受用户关注的智能手机手机操作系统是安卓手机操作系统,获得了70%.1的关注比例,ios和阿里云的"山寨安卓"分别为15.9%和2.9%.对于这款刚刚推出就深受广大开发者喜爱的产品.相信还有不少朋友有所疑问,今天我就把我总结的Android开发经验给大家分享下! 首先需要下载Android SDK和MyEclipse集成的Android开发插件ADT,这些官网都有提供下载,不过,我试过了,官网的下载速度巨慢,我无法忍受,于是google找到一个压缩版. [Andro

Android开发经验总结:我来详解Android SDK与ADT插件

据权威数据显示:目前最受用户关注的智能手机手机操作系统是安卓手机操作系统,获得了70%.1的关注比例,ios和阿里云的"山寨安卓"分别为15.9%和2.9%.对于这款刚刚推出就深受广大开发者喜爱的产品.相信还有不少朋友有所疑问,今天我就把我总结的Android开发经验给大家分享下! 首先需要下载Android SDK和MyEclipse集成的Android开发插件ADT,这些官网都有提供下载,不过,我试过了,官网的下载速度巨慢,我无法忍受,于是google找到一个压缩版. [Andro

详解Android 目录结构

详解Android 目录结构 工欲善其事,必先利其器.在开发Android项目之前,让我们先对Android的目录结构有一个清楚的认识. 在Eclipse中新建一个Android Project,名称为"AndroidTest",则项目结构如图: 1.src目录 文件夹下放置项目的所有包及源文件(.java). 2.gen目录 文件夹中包含了一个R.java类文件,通过R.java类中的注释可以看出,R.java文件是由aapt工具根据项目中的资源文件来自动生成的.R.java文件是项

基于 SurfaceView 详解 android 幸运大转盘,附带实例app

基于 SurfaceView 详解 android 幸运大转盘,附带实例app       首先说一下,幸运大转盘,以及SurfaceView是在看了也为大神的博客,才有了比较深刻的理解,当然这里附上这位大神的博客地址:博客地址,有兴趣的话你可以去看看,里面有很多的例子.至于我为什么要写这篇博客?,原因之一:加强自己的理解,原因之二:大神的博客就是大神的博客,跳转的太快,基础不好的,很难理解.还有就是一天在实验室太无聊了,没事写写东西.这里我再来更加基础的分析一下.写的不好,原谅.有什么写的不对

详解Android中Intent对象与Intent Filter过滤匹配过程_Android

如果对Intent不是特别了解,可以参见博文<详解Android中Intent的使用方法>,该文对本文要使用的action.category以及data都进行了详细介绍.如果想了解在开发中常见Intent的使用,可以参见<Android中Intent习惯用法>. 本文内容有点长,希望大家可以耐心读完. 本文在描述组件在manifest中注册的Intent Filter过滤器时,统一用intent-filter表示. 一.概述 我们知道,Intent是分两种的:显式Intent和隐式

详解Android中Handler的内部实现原理_Android

本文主要是对Handler和消息循环的实现原理进行源码分析,如果不熟悉Handler可以参见博文<详解Android中Handler的使用方法>,里面对Android为何以引入Handler机制以及如何使用Handler做了讲解. 概括来说,Handler是Android中引入的一种让开发者参与处理线程中消息循环的机制.我们在使用Handler的时候与Message打交道最多,Message是Hanlder机制向开发人员暴露出来的相关类,可以通过Message类完成大部分操作Handler的功

详解Android主流框架不可或缺的基石

探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Android多分辨率适配框架(3)- 使用指南 自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View

实例详解Android Selector和Shape的用法_Android

shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和selector在美化控件中的作用是至关重要的. 1:Selector drawable的item中可以有以下属性: android:drawable="@[package:]drawable/drawable_resource" android:state_pressed=["

详解Android更改APP语言模式的实现过程_Android

一.效果图 二.描述 更改Android项目中的语言,这个作用于只用于此APP,不会作用于整个系统 三.解决方案 (一)布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" a