`
ax003d
  • 浏览: 12269 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Android 集成 Zxing 条码扫描器

阅读更多

参考文章(需XX):http://damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcode-scanner-into-your-android-app-natively-using-eclipse/

 

本文在如上参考文章基础之上,进行了一些修改:

1. 修改扫描 action 的名字,当用户手机上已安装了 Zxing 扫描器,触发扫描操作不会弹出选择框(默认会弹出自己的应用程序和 Zxing 条码扫描器两个选项)

2. 兼容 Zxing 推荐的 IntentIntegrator,如果用户手机上安装了 Zxing 条码扫描器,优先使用安装的扫描器

 

step1. 签出Zxing源代码

svn checkout http://zxing.googlecode.com/svn/trunk/ zxing-read-only

 

step2. 编译Zxing源代码

cd zxing-read-only
ant -f core/build.xml

 

step3. 在Eclipse中导入Zxing的安卓工程

路径是 zxing-read-only/android,并设置它为一个 library 工程,右键工程属性 | Android | Is Library

 

step4. 修改源代码 CaptureActivity/src/com/google/zxing/client/android/Intents.java 第 34 行

 

// public static final String ACTION = "com.google.zxing.client.android.SCAN";
public static final String ACTION = "com.xxx.xxx.SCAN"; // 避免和默认的 Zxing 条码扫描器冲突
 修改源代码 CaptureActivity/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java 第 101 行:
// if (safeMode || prefs.getBoolean(PreferencesActivity.KEY_DISABLE_CONTINUOUS_FOCUS, false)) {
if (safeMode || prefs.getBoolean(PreferencesActivity.KEY_DISABLE_CONTINUOUS_FOCUS, true)) { // 将默认值改为 true
 

step5. 如果编译出错,出现 switch-case 中 R.id.xxx 错误,说 R.id.xxx 不是常量,那是因为 ADT 1.4 之后,对于 library 类型的 project 生成的 R.java 文件内的值都不是常量了,具体原因请见如下链接:

http://stackoverflow.com/questions/15667755/androiderror-case-expressions-must-be-constant-expressions

解决方法是将出现 switch-case 的地方转换为 if-else 结构(光标放在 switch 单词上,然后 Ctrl-1,选择转换为 if-else 即可)

 

step6. 添加刚才导入的工程(默认名为CaptureActivity)作为自己工程的一个 Library 工程,在触发扫描器的代码中添加如下代码:

Intent intent = new Intent("com.google.zxing.client.android"
		+ ".SCAN");
intent.addCategory(Intent.CATEGORY_DEFAULT);
PackageManager pm = activity.getPackageManager();
List<ResolveInfo> availableApps = pm.queryIntentActivities(intent,
		PackageManager.MATCH_DEFAULT_ONLY);
if (availableApps.size() > 0) {
        // 如果用户已安装条码扫描器,使用默认的扫描器
	IntentIntegratorSupportV4 integrator = new IntentIntegratorSupportV4(
			this);
	integrator.initiateScan();
	mBuildInScanner = true;
} else {
        // 否则使用内置扫描器
        // 使用默认的 action 应该是 com.google.zxing.client.android.SCAN
	intent = new Intent("com.xxx.xxx.SCAN");
	startActivityForResult(intent, 0);
}

 

step7. 重写 onActivityResult 方法:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);
	if (mBuildInScanner) {
		IntentResult scanResult = IntentIntegratorSupportV4
				.parseActivityResult(requestCode, resultCode, data);
		if (scanResult != null) {
                        // 处理扫描结果 
                        scanResult.getContents();
		}
		return;
	}

	if (requestCode == 0) {
		if (resultCode == Activity.RESULT_OK) {
			String contents = data.getStringExtra("SCAN_RESULT");
			String format = data.getStringExtra("SCAN_RESULT_FORMAT");
                        // 处理扫描结果
		} else if (resultCode == Activity.RESULT_CANCELED) {
		}
	}
}

 

step8. 修改 AndroidManifest.xml 文件

<activity android:name="com.google.zxing.client.android.CaptureActivity"
     android:screenOrientation="landscape"
     android:configChanges="orientation|keyboardHidden"
     android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
     android:windowSoftInputMode="stateAlwaysHidden">
     <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.DEFAULT"/>
     </intent-filter>
     <intent-filter>
        <!-- 默认的应该是 com.google.zxing.client.android.SCAN -->
        <action android:name="com.xxx.xxx.SCAN"/>
        <category android:name="android.intent.category.DEFAULT"/>
     </intent-filter>
</activity>

 并添加如下权限:

<uses-permission android:name="android.permission.CAMERA"/>

 

step9. 如果要release自己的程序,需在 zxing-read-only/android 路径下添加以下文件,否则在执行 ant release 过程中需要输入 keystore 的密码:

 

# secure.properties
key.store.password=<your pwd>
key.alias.password=<your pwd>

 

# custom_rules.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules" default="help">
  <property file="secure.properties" />
</project>

 

修改 build.xml:

...
<project name="BarcodeScanner" default="help">

  <import file="custom_rules.xml" optional="true" /><!-- 添加此行 -->

...

 

 

step10. 编译运行

 

 

 

 

 

 

分享到:
评论

相关推荐

    Android-Zxing:android google zxing 可配置扫描框、线样式 ,生成二维码(文字、联系人)

    相信Adnroid开发都知道,有四款扫描器, 、 ,、 前二者应用较广泛,至于介绍与区别就在此阐述,网上有很多。此文主要介绍在在使用过程中,官方客户端各种达不到需求。相信很多童鞋都有此体会,所以借此机会就在...

    Android-Barcode_And_Qr_Scanner:Android条码和Qr扫描仪

    在此应用程序中,我们将使用ZXing(斑马线)库在Android应用程序中执行条形码扫描。 我们将在应用程序中的此开源库中调用资源,以检索和处理返回的结果。 由于我们使用的是ZXing库,因此无需担心没有安装条形码...

    QR-Code-Reader:域名

    JavaSE 的客户端代码安卓安卓客户端条码扫描器 安卓测试Android 测试应用,ZXing 测试安卓集成支持通过Intent与 Barcode Scanner 集成安卓核心android 、 androidtest 、 glass之间共享的android相关代码玻璃简单的...

    java开源包1

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    java开源包11

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    java开源包2

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    java开源包3

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    java开源包6

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    java开源包5

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    java开源包10

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    java开源包4

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    java开源包8

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    java开源包7

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    java开源包9

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    java开源包101

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    Java资源包01

    条形码扫描和识别程序 LVBarcode LVBarcode 支持下列的条形码格式:Codabar,I2of5,Code39,ExCode39?,EAN-8,EAN-13,Code128 A,Code128 B,Code128 C,MSI,UPC-A,UPC-E. 中文转拼音库 pinyin4j Pinyin4j是一个流行的...

    JAVA上百实例源码以及开源项目

    此时此景,笔者只专注Android、Iphone等移动平台开发,看着这些源码心中有万分感慨,写此文章纪念那时那景! Java 源码包 Applet钢琴模拟程序java源码 2个目标文件,提供基本的音乐编辑功能。编辑音乐软件的朋友,这...

    JAVA上百实例源码以及开源项目源代码

    Java目录监视器源程序 9个目标文件 内容索引:JAVA源码,综合应用,目录监视 用JAVA开发的一个小型的目录监视系统,系统会每5秒自动扫描一次需要监视的目录,可以用来监视目录中文件大小及文件增减数目的变化。...

Global site tag (gtag.js) - Google Analytics