首先想到的是安装cordova plugin add cordova-plugin-barcodescanner插件
插件安装的比较顺利,但是打包时总出现问题,按下面解决方法ok
This is how I've solved it.
1. Open plugins/cordova-plugin-barcode-scanner/plugin.xml and delete all records xmlns:android=""
2. Open platforms/android/android.json and delete all xmlns:android=\"\"
3. Do the same in platforms/android/AndroidManifest.xml
打包完成后,装到手机上,打开app ,弹出alert scanning failed:write settings:false ,不能扫描。
最后在platforms\android\AndroidManifest.xml 文件中修改配置文件目标sdk为22,扫描ok
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" />
但是发现扫描不能竖屏使用,横屏很不方便。
后来发现phonegap-plugin-barcodescanner插件可以竖屏扫描,于是赶紧安装这个插件试试
安装步骤
(1)首先按正常流程初始化项目,默认安装的是cordova-build 是6.1.2版本,安装它需要6.3及以上版本
更新cordova-build版本 cordova platform update android@6.3.0
(2)cordova plugin add phonegap-plugin-barcodescanner
在安装的时候会下载gradle-3.3-all.zip,估计网速慢的原因下载不下来,需要手动下载
然后放到E:\cordova\hello\platforms\android\gradle目录里
同时修改E:\cordova\hello\platforms\android\cordova\lib\builders 下的GradleBuilder.js
var distributionUrl =process.env['CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL'] || '../gradle-3.3-all.zip';
(3)<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="26" /> targetSdkVersion 改成26
Android SDK Manager内同时下载SDK Platform Android 8.0.0(API26)
顺利安装完成!!!
附上前端代码
<!DOCTYPE html>
<html>
<head>
<title>take Photo</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript">
function scanCode() {
alert("hi code")
cordova.plugins.barcodeScanner.scan(
function (result) {
alert("We got a barcode\n" +
"Result: " + result.text + "\n" +
"Format: " + result.format + "\n" +
"Cancelled: " + result.cancelled);
document.getElementById("text").value=result.text;
},
function (error) {
alert("Scanning failed: " + error);
}
);
}
</script>
</head>
<body style="padding-top:50px">
<input type="text" id="text" />
<button href="#" class="btn" onclick="scanCode();">扫描</button>
</body>
</html>