参考文章(需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-onlyant -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.SCANintent = 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.propertieskey.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. 编译运行

更多相关文章

  1. android创建工程时自动创建的V4和V7工程
  2. Android导入自定义的jar包时出现 E/AndroidRuntime(486): java.l
  3. 安卓笔记android 本地Tomcat配置servlet的步骤和注意项
  4. android 系统级别开发环境搭建 .
  5. Eclipse编译Anroid引用程序的出错处理
  6. Android(安卓)亲测源码分享
  7. Android系统中调试动态链接库.so文件的步骤
  8. Kotlin Android(安卓)环境搭建
  9. 推荐一系列优秀的Android开发源码

随机推荐

  1. mysql 5.7.17 安装配置方法图文教程(Cent
  2. SQL Server COALESCE函数详解及实例
  3. windows 10 下安装mysql 5.7.17的简单笔
  4. 详解windows下mysql的主从同步
  5. Mybatis报错: org.apache.ibatis.excepti
  6. SQL Server 数据库的备份详细介绍及注意
  7. 详解MySQL的主从复制、读写分离、备份恢
  8. windows mysql 自动备份的几种方法汇总
  9. 深度解析MySQL 5.7之临时表空间
  10. MySQL 使用 SSL 连接配置详解