【Android Hybrid App】Cordova 之扫码(二维码)功能
一、基于Cordova 插件实现(
未安装成功
)
插件:cordova-plugin-barcodescanner
1.1 cordova-plugin-barcodescanner 插件安装
1.1.1. 远程安装
phonegap plugin add phonegap-plugin-barcodescanner
cordova plugin add https://github.com/phonegap/phonegap-plugin-barcodescanner.git
未成功,提示内容:
Error: Failed to fetch plugin https://github.com/phonegap/phonegap-plugin-barcodescanner.git via registry.
Probably this is either a connection problem, or plugin spec is incorrect.
Check your connection and plugin name/version/URL.
Error: cmd: Command failed with exit code 1 Error output:
npm ERR! code ENOGIT
npm ERR! No git binary found in $PATH
npm ERR!
npm ERR! Failed using git.
npm ERR! Please check if you have git installed and in your PATH.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\yuhl\AppData\Roaming\npm-cache\_logs\2017-08-08T08_25_43_986Z-debug.log
1.1.2. 本地安装
下载 phonegap-plugin-barcodescanner-master.zip
E:\workspace\xxxapp>cordova plugin add C:\Users\yuhl\Downloads\phonegap-plugin-barcodescanner-master
Installing "phonegap-plugin-barcodescanner" for android
Plugin dependency "cordova-plugin-compat@1.0.0" already fetched, using that version.
Dependent plugin "cordova-plugin-compat" already installed on android.
Subproject Path: CordovaLib
Adding phonegap-plugin-barcodescanner to package.json
Saved plugin info for "phonegap-plugin-barcodescanner" to config.xml
安装后,打开工程,缺少 com.google.zxing 库
import com.google.zxing.client.android.CaptureActivity;
import com.google.zxing.client.android.encode.EncodeActivity;
import com.google.zxing.client.android.Intents;
/**
* This calls out to the ZXing barcode reader and returns the result.
*
* @sa https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaPlugin.java
*/
public class BarcodeScanner extends CordovaPlugin {
public static final int REQUEST_CODE = 0x0ba7c0de;
1.1.3 解决上述问题
下载 zxing-master,参见:Android笔记之用eclipse给zxing打jar包
未成功构建Zxing-master工程,所以为解决上述问题,卸载插件。
1.1.4. 删除插件
cordova plugin remove phonegap-plugin-barcodescanner
卸载过程中遇到问题:
Uninstalling phonegap-plugin-barcodescanner from android
cp: copyFileSync: could not write to dest file (code=ENOENT):E:\workspace\zxing-master\zxing-client-android\zxing-lib\build.gradle
Subproject Path: CordovaLib
Subproject Path: ../../../zxing-master/zxing-client-android/zxing-lib
Error: ENOENT: no such file or directory, open 'E:\workspace\zxing-master\zxing-client-android\zxing-lib\build.gradle'
解决:
下载 zxing-client-android,解压后部署 E:\workspace\zxing-master\zxing-client-android
zxing-lib
zxing-lib-example
再删除插件成功。
E:\workspace\ispecapp>cordova plugin remove phonegap-plugin-barcodescanner
Uninstalling phonegap-plugin-barcodescanner from android
Subproject Path: CordovaLib
Subproject Path: ../../../zxing-master/zxing-client-android/zxing-lib
Removing "phonegap-plugin-barcodescanner"
Removing plugin phonegap-plugin-barcodescanner from config.xml file...
Removing phonegap-plugin-barcodescanner from package.json
二、基于zxing-client-android 组件lib及example 实现
2.1 eclipse 导入lib 工程(CaptureActivity)
2.1.1 Android 工程设置
设置 CaptureActivity 工程为Library。
2.1.2 参数配置
AndroidManifest.xml
权限配置
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.VIBRATE"/>
SDK配置
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" />
重点
:引用该Library 的Android 工程需要同样的配置,否则会报错。
3.Android 工程项目属性配置
3.2 调用 CaptureActivity 的Android 工程配置及Api调用
3.2.1 添加依赖(zxing-clinet-lib CaptureActivity工程)
引用 CaptureActivity Library。
配置(AndroidManifest.xml) users-sdk 参数,同CaptureActivity Library配置
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" />
待续...
特别说明:文章内容不以技术研究与分享为目的,不保证准确性,作者本意只是知识(与思路)的梳理。