ZXing Android Embedded 是一个非常好用的开源库,对于 ZXing 在 Android 上的应用方法进行了精简。这里简单地用一个小 demo 来练习对此库的使用。
引用开源库
Android Studio 引用方法相当简单,直接在 build.gradle
文件里加入如下代码:
repositories {
jcenter()
}
dependencies {
compile 'com.journeyapps:zxing-android-embedded:3.4.0'
compile 'com.android.support:appcompat-v7:23.1.0' // Version 23+ is required
}
android {
buildToolsVersion '23.0.2' // Older versions may give compile errors
}
扫描二维码
新建一个项目,在布局文件里设置简单的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:contex