詳解Android 掃描條形碼(Zxing插件)
使用Android Studio
一、在build.gradle(Module:app)添加代碼 下載,調(diào)用插件
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'
}
二、添加權(quán)限
<?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);
}
// 掃描按鈕點(diǎn)擊監(jiān)聽(tīng)事件
public void clickScan(View view) {
//掃描操作
IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 跳轉(zhuǎn)掃描頁(yè)面返回掃描數(shù)據(jù)
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
// 判斷返回值是否為空
if (scanResult != null) {
//返回條形碼數(shù)據(jù)
String result = scanResult.getContents();
Log.d("code", result);
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- Android實(shí)現(xiàn)二維碼掃描和生成的簡(jiǎn)單方法
- Android中掃描多媒體文件操作詳解
- Android開(kāi)發(fā)框架之自定義ZXing二維碼掃描界面并解決取景框拉伸問(wèn)題
- Android音樂(lè)播放器制作 掃描本地音樂(lè)顯示在手機(jī)(一)
- Android設(shè)備獲取掃碼槍掃描的內(nèi)容與可能遇到的問(wèn)題解決
- Android編程實(shí)現(xiàn)wifi掃描及連接的方法
- Android studio 實(shí)現(xiàn)手機(jī)掃描二維碼功能
- Android應(yīng)用中使用ContentProvider掃描本地圖片并顯示
- Android 二維碼掃描和生成二維碼功能
- Android自定義View實(shí)現(xiàn)掃描效果
相關(guān)文章
Android Studio不能獲取遠(yuǎn)程依賴包的完美解決方法
這篇文章主要介紹了Android Studio不能獲取遠(yuǎn)程依賴包的解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
Android編程解析XML方法詳解(SAX,DOM與PULL)
這篇文章主要介紹了Android編程解析XML方法,結(jié)合實(shí)例形式詳細(xì)分析了Android解析XML文件的常用方法與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01
Android 邊播邊緩存的實(shí)現(xiàn)(MP4 未加密m3u8)
這篇文章主要介紹了Android 邊播邊緩存的實(shí)現(xiàn)(MP4 未加密m3u8),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Android小知識(shí)之圖片的3種壓縮方式小結(jié)
這篇文章主要給大家介紹了關(guān)于Android小知識(shí)之圖片的3種壓縮方式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
Android ListView彈性效果的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Android ListView彈性效果的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-05-05

