首先想到的是安裝cordova plugin add cordova-plugin-barcodescanner插件javascript
插件安裝的比較順利,可是打包時總出現問題,按下面解決方法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.xmlhtml
打包完成後,裝到手機上,打開app ,彈出alert scanning failed:write settings:false ,不能掃描。前端
最後在platforms\android\AndroidManifest.xml 文件中修改配置文件目標sdk爲22,掃描okjava
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" />android
可是發現掃描不能豎屏使用,橫屏很不方便。json
後來發現phonegap-plugin-barcodescanner插件能夠豎屏掃描,因而趕忙安裝這個插件試試app
安裝步驟gradle
(1)首先按正常流程初始化項目,默認安裝的是cordova-build 是6.1.2版本,安裝它須要6.3及以上版本ui
更新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>