Cordova適用插件 之 二維碼Barcode

tags:    時間:2014-03-23 13:53:00
Cordova實用插件 之 二維碼Barcode
插件地址:https://github.com/wildabeast/BarcodeScanner

(1)創建工程
引用
cordova create HelloBarcode com.rensanning.cordova HelloBarcode
cd HelloBarcode
cordova platform add android


(2)安裝plugin
引用
cordova plugin add https://github.com/wildabeast/BarcodeScanner.git


(3)修改index.html后編譯執行

<a href="#" class="btn" onclick="scanCode();">Scan Code</a> <script type="text/javascript"> function scanCode() {     cordova.plugins.barcodeScanner.scan(       function (result) {           alert("We got a barcode\n" +                 "Result: " + result.text + "\n" +                 "Format: " + result.format + "\n" +                 "Cancelled: " + result.cancelled);       },        function (error) {           alert("Scanning failed: " + error);       }    ); } </script>




<a href="#" class="btn enc" onclick="encodeText();">Encode Text</a> <script type="text/javascript"> function encodeText() {     cordova.plugins.barcodeScanner.encode(       "TEXT_TYPE",        "http://www.baidu.com",        function(success) {         alert("encode success: " + success);       }, function(fail) {         alert("encoding failed: " + fail);       }     ); } </script>



<a href="#" class="btn enc" onclick="encodeEmail();">Encode Email</a> <script type="text/javascript"> function encodeEmail() {     cordova.plugins.barcodeScanner.encode(       "EMAIL_TYPE",        "rensanning@gmail.com",        function(success) {         alert("encode success: " + success);       }, function(fail) {         alert("encoding failed: " + fail);       }     ); } </script>



<a href="#" class="btn enc" onclick="encodePhone();">Encode Phone</a> <script type="text/javascript"> function encodePhone() {     cordova.plugins.barcodeScanner.encode(       "PHONE_TYPE",        "135-1234-5678",        function(success) {         alert("encode success: " + success);       }, function(fail) {         alert("encoding failed: " + fail);       }     ); } </script>



<a href="#" class="btn enc" onclick="encodeSMS();">Encode SMS</a> <script type="text/javascript"> function encodeSMS() {     cordova.plugins.barcodeScanner.encode(       "SMS_TYPE",        "An important message for someone.",        function(success) {         alert("encode success: " + success);       }, function(fail) {         alert("encoding failed: " + fail);       }     ); } </script>



注意:

a) 沒有實現CONTACT_TYPE 和 LOCATION_TYPE的encode。
b) 如果encode出錯:
java.lang.NullPointerException    at com.google.zxing.client.android.encode.EncodeActivity.onCreateOptionsMenu(EncodeActivity.java:89)

下載最新的encode.xml文件,覆蓋 platforms\android\res\menu\encode.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">   <item android:id="@+id/menu_share"         android:title="@string/menu_share"         android:icon="@android:drawable/ic_menu_share"         android:orderInCategory="1"         android:showAsAction="withText|ifRoom"/>   <item android:id="@+id/menu_encode"         android:title="@string/menu_encode_vcard"         android:icon="@android:drawable/ic_menu_sort_alphabetically"         android:orderInCategory="2"         android:showAsAction="withText|ifRoom"/> </menu>

推薦閱讀文章

Bookmark the permalink ,來源:互聯網