Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3422006
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: 系统运维

2012-07-24 16:53:47

可以通过这篇文章熟悉创建phonegap插件来启动另一个应用
转自:http://topmanopensource.iteye.com/blog/1521302
在phonegap中通过二维码实现也是通过扩展phonegap的插件实现。

项目结构如下:

实现如下:

二维码扫码实现的插件类:

 

Java代码  收藏代码
  1. package com.easyway.barcode;  
  2.   
  3.   
  4.   
  5. import org.json.JSONArray;  
  6. import org.json.JSONException;  
  7.   
  8. import android.app.Activity;  
  9. import android.app.AlertDialog;  
  10. import android.content.ActivityNotFoundException;  
  11. import android.content.DialogInterface;  
  12. import android.content.Intent;  
  13. import android.net.Uri;  
  14.   
  15. import com.phonegap.api.PhonegapActivity;  
  16. import com.phonegap.api.Plugin;  
  17. import com.phonegap.api.PluginResult;  
  18.   
  19. /** 
  20.  * 扩展二维码扫描的phonegap类实现 
  21.  * 实现原理如下: 
  22.  *    1.使用phonegap的js类库实现通过插件调用相关的Plugin java类。 
  23.  *    2.plugin调用zxing相关的二维码扫码的方法实现。 
  24.  *    3.如果调用zxing没有安装,到google下载相关的zxing apk安装,并调用对应的intent实现。 
  25.  *  
  26.  * This calls out to the ZXing barcode reader and returns the result. 
  27.  */  
  28. public class BarcodeScanner extends Plugin {  
  29.     public static final int REQUEST_CODE = 0x0ba7c0de;  
  30.   
  31.   
  32.     public static final String defaultInstallTitle = "Install Barcode Scanner?";  
  33.     public static final String defaultInstallMessage = "This requires the free Barcode Scanner app. Would you like to install it now?";  
  34.     public static final String defaultYesString = "Yes";  
  35.     public static final String defaultNoString = "No";  
  36.   
  37.     public String callback;  
  38.   
  39.     /** 
  40.      * Constructor. 
  41.      */  
  42.     public BarcodeScanner() {  
  43.     }  
  44.   
  45.     /** 
  46.      *  
  47.      * 用于plugin相关的方法,用于暴露相关的方法使用。 
  48.      *  
  49.      * Executes the request and returns PluginResult. 
  50.      * 
  51.      * @param action        The action to execute. 
  52.      * @param args          JSONArray of arguments for the plugin. 
  53.      * @param callbackId    The callback id used when calling back into JavaScript. 
  54.      * @return              A PluginResult object with a status and message. 
  55.      */  
  56.     public PluginResult execute(String action, JSONArray args, String callbackId) {  
  57.         this.callback = callbackId;  
  58.   
  59.         try {  
  60.             if (action.equals("encode")) {  
  61.                 String type = null;  
  62.                 if(args.length() > 0) {  
  63.                     type = args.getString(0);  
  64.                 }  
  65.   
  66.                 String data = null;  
  67.                 if(args.length() > 1) {  
  68.                     data = args.getString(1);  
  69.                 }  
  70.   
  71.                 String installTitle = defaultInstallTitle;  
  72.                 if(args.length() > 2) {  
  73.                     installTitle = args.getString(2);  
  74.                 }  
  75.   
  76.                 String installMessage = defaultInstallMessage;  
  77.                 if(args.length() > 3) {  
  78.                     installMessage = args.getString(3);  
  79.                 }  
  80.   
  81.                 String yesString = defaultYesString;  
  82.                 if(args.length() > 4) {  
  83.                     yesString = args.getString(4);  
  84.                 }  
  85.   
  86.                 String noString = defaultNoString;  
  87.                 if(args.length() > 5) {  
  88.                     noString = args.getString(5);  
  89.                 }  
  90.   
  91.                 // if data.TypeOf() == Bundle, then call  
  92.                 // encode(type, Bundle)  
  93.                 // else  
  94.                 // encode(type, String)  
  95.                 this.encode(type, data, installTitle, installMessage, yesString, noString);  
  96.             }  
  97.             else if (action.equals("scan")) {  
  98.                 String barcodeTypes = null;  
  99.                 if(args.length() > 0) {  
  100.                     barcodeTypes = args.getString(0);  
  101.                 }  
  102.   
  103.                 String installTitle = defaultInstallTitle;  
  104.                 if(args.length() > 1) {  
  105.                     installTitle = args.getString(1);  
  106.                 }  
  107.   
  108.                 String installMessage = defaultInstallMessage;  
  109.                 if(args.length() > 2) {  
  110.                     installMessage = args.getString(2);  
  111.                 }  
  112.   
  113.                 String yesString = defaultYesString;  
  114.                 if(args.length() > 3) {  
  115.                     yesString = args.getString(3);  
  116.                 }  
  117.   
  118.                 String noString = defaultNoString;  
  119.                 if(args.length() > 4) {  
  120.                     noString = args.getString(4);  
  121.                 }  
  122.   
  123.                 scan(barcodeTypes, installTitle, installMessage, yesString, noString);  
  124.             } else {  
  125.                 return new PluginResult(PluginResult.Status.INVALID_ACTION);  
  126.             }  
  127.             PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);  
  128.             r.setKeepCallback(true);  
  129.             return r;  
  130.         } catch (JSONException e) {  
  131.             e.printStackTrace();  
  132.             return new PluginResult(PluginResult.Status.JSON_EXCEPTION);  
  133.         }  
  134.     }  
  135.   
  136.   
  137.     /** 
  138.      * 扫描二维码的方法 
  139.      *    备注:在扫描二维码的类型最好不好设置,在前期的zxing可能需要,在后期的版本中不需要, 
  140.      *    zxing会自动检索二维码的类型,并识别相关二维码。 
  141.      *     
  142.      * Initiates a barcode scan. If the ZXing scanner isn't installed, the user 
  143.      * will be prompted to install it. 
  144.      * @param types The barcode types to accept 
  145.      * @param installTitle The title for the dialog box that prompts the user to install the scanner 
  146.      * @param installMessage The message prompting the user to install the barcode scanner 
  147.      * @param yesString The string "Yes" or localised equivalent 
  148.      * @param noString The string "No" or localised version 
  149.      */  
  150.     public void scan(String barcodeFormats, String installTitle, String installMessage, String yesString, String noString ) {  
  151.         Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");  
  152.        // intentScan.addCategory(Intent.CATEGORY_DEFAULT);  
  153.   
  154.         //设置扫描特定类型的二维码  
  155.         //if (barcodeFormats != null) {  
  156.         //      Tell the scanner what types we're after  
  157.         //          intentScan.putExtra("SCAN_FORMATS", barcodeFormats);  
  158.         // }  
  159.         try {  
  160.             this.ctx.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE);  
  161.         } catch (ActivityNotFoundException e) {  
  162.             showDownloadDialog(installTitle, installMessage, yesString, noString);  
  163.         }  
  164.     }  
  165.   
  166.     /** 
  167.      * 用于获取二维码扫描之后获取相关的二维码相关的信息 
  168.      * Called when the barcode scanner exits 
  169.      * 
  170.      * @param requestCode       The request code originally supplied to startActivityForResult(), 
  171.      *                          allowing you to identify who this result came from. 
  172.      * @param resultCode        The integer result code returned by the child activity through its setResult(). 
  173.      * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras"). 
  174.      */  
  175.     public void onActivityResult(int requestCode, int resultCode, Intent intent) {  
  176.         if (requestCode == REQUEST_CODE) {  
  177.             if (resultCode == Activity.RESULT_OK) {  
  178.                 String contents = intent.getStringExtra("SCAN_RESULT");  
  179.                 String format = intent.getStringExtra("SCAN_RESULT_FORMAT");  
  180.                 this.success(new PluginResult(PluginResult.Status.OK, " 条形码为:"+contents+" 条码类型为: "+format), this.callback);  
  181.             } else {  
  182.                 this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);  
  183.             }  
  184.         }  
  185.     }  
  186.   
  187.     /** 
  188.      * 创建相关的对话框,在通过没有安装相关的zxing开源组件时候,调用远程的intent或者下载相关执行类实现相关的 
  189.      * 功能 
  190.      * @param title 
  191.      * @param message 
  192.      * @param yesString 
  193.      * @param noString 
  194.      */  
  195.     private void showDownloadDialog(final String title, final String message, final String yesString, final String noString) {  
  196.         final PhonegapActivity context = this.ctx;  
  197.         Runnable runnable = new Runnable() {  
  198.             public void run() {  
  199.   
  200.                 AlertDialog.Builder dialog = new AlertDialog.Builder(context);  
  201.                 dialog.setTitle(title);  
  202.                 dialog.setMessage(message);  
  203.                 dialog.setPositiveButton(yesString, new DialogInterface.OnClickListener() {  
  204.                     public void onClick(DialogInterface dlg, int i) {  
  205.                         dlg.dismiss();  
  206.                         Intent intent = new Intent(Intent.ACTION_VIEW,  
  207.                                                    Uri.parse("market://search?q=pname:com.google.zxing.client.android")  
  208.                                                    );  
  209.                         try {  
  210.                             context.startActivity(intent);  
  211.                         } catch (ActivityNotFoundException e) {  
  212.                             //  We don't have the market app installed, so download it directly.  
  213.                             Intent in = new Intent(Intent.ACTION_VIEW);  
  214.                             in.setData(Uri.parse(""));  
  215.                             context.startActivity(in);  
  216.   
  217.                         }  
  218.   
  219.                     }  
  220.                 });  
  221.                 dialog.setNegativeButton(noString, new DialogInterface.OnClickListener() {  
  222.                     public void onClick(DialogInterface dlg, int i) {  
  223.                         dlg.dismiss();  
  224.                     }  
  225.                 });  
  226.                 dialog.create();  
  227.                 dialog.show();  
  228.             }  
  229.         };  
  230.         context.runOnUiThread(runnable);  
  231.     }  
  232.   
  233.     /** 
  234.      *  
  235.      *  
  236.      * Initiates a barcode encode. If the ZXing scanner isn't installed, the user 
  237.      * will be prompted to install it. 
  238.      * @param type  The barcode type to encode 
  239.      * @param data  The data to encode in the bar code 
  240.      * @param installTitle The title for the dialog box that prompts the user to install the scanner 
  241.      * @param installMessage The message prompting the user to install the barcode scanner 
  242.      * @param yesString The string "Yes" or localised equivalent 
  243.      * @param noString The string "No" or localised version 
  244.      */  
  245.     public void encode(String type, String data, String installTitle, String installMessage, String yesString, String noString) {  
  246.         Intent intentEncode = new Intent("com.google.zxing.client.android.ENCODE");  
  247.         intentEncode.putExtra("ENCODE_TYPE", type);  
  248.         intentEncode.putExtra("ENCODE_DATA", data);  
  249.   
  250.         try {  
  251.             this.ctx.startActivity(intentEncode);  
  252.         } catch (ActivityNotFoundException e) {  
  253.             showDownloadDialog(installTitle, installMessage, yesString, noString);  
  254.         }  
  255.     }  
  256. }  

 

 

Java代码  收藏代码
  1. package com.easyway.barcode;  
  2.   
  3. import com.phonegap.*;  
  4. import android.os.Bundle;  
  5. /** 
  6.  * phonegap实现相关的二维码的扫码功能 
  7.  *  
  8.  * @Title:  
  9.  * @Description: 实现phonegap实现相关的二维码的扫码功能 
  10.  * @Copyright:Copyright (c) 2011 
  11.  * @Company:易程科技股份有限公司 
  12.  * @Date:2012-5-10 
  13.  * @author  longgangbai 
  14.  * @version 1.0 
  15.  */  
  16. public class PhonegapBarcodeActivity extends DroidGap {  
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         super.loadUrl("file:///android_asset/www/index.html");  
  22.     }  
  23. }  
 

二维码扫码实现的js:

 

Js代码  收藏代码
  1. /** 
  2.  * Phonegap Barcode Scanner plugin 
  3.  * Copyright (c) Matt Kane 2010 
  4.  * 
  5.  */  
  6. var BarcodeScanner = function() {   
  7.   
  8. }  
  9.   
  10. BarcodeScanner.Type = {  
  11.     QR_CODE: "QR_CODE",  
  12.     DATA_MATRIX: "DATA_MATRIX",  
  13.     UPC_E: "UPC_E",  
  14.     UPC_A: "UPC_A",  
  15.     EAN_8: "EAN_8",  
  16.     EAN_13: "EAN_13",  
  17.     CODE_128: "CODE_128",  
  18.     CODE_39: "CODE_39",  
  19.     CODE_93: "CODE_93",  
  20.     CODABAR: "CODABAR",  
  21.     ITF: "ITF",  
  22.     RSS14: "RSS14",  
  23.     PDF417: "PDF417",  
  24.     RSS_EXPANDED: "RSS_EXPANDED",  
  25.     PRODUCT_CODE_TYPES: "UPC_A,UPC_E,EAN_8,EAN_13",  
  26.     ONE_D_CODE_TYPES: "UPC_A,UPC_E,EAN_8,EAN_13,CODE_39,CODE_93,CODE_128",  
  27.     QR_CODE_TYPES: "QR_CODE",  
  28.     ALL_CODE_TYPES: null  
  29. }  
  30.   
  31. BarcodeScanner.Encode = {  
  32.         TEXT_TYPE: "TEXT_TYPE",  
  33.         EMAIL_TYPE: "EMAIL_TYPE",  
  34.         PHONE_TYPE: "PHONE_TYPE",  
  35.         SMS_TYPE: "SMS_TYPE",  
  36.         //  CONTACT_TYPE: "CONTACT_TYPE",  // TODO:  not implemented, requires passing a Bundle class from Javascriopt to Java  
  37.         //  LOCATION_TYPE: "LOCATION_TYPE" // TODO:  not implemented, requires passing a Bundle class from Javascriopt to Java  
  38.     }  
  39. //二维码扫描的方法  
  40. BarcodeScanner.prototype.scan = function(types, success, fail, options) {  
  41.       
  42.     /* These are the strings used in the dialog that appears if ZXing isn't installed */  
  43.     var installTitle = "Install Barcode Scanner?";  
  44.     var installMessage = "This requires the free Barcode Scanner app. Would you like to install it now?";  
  45.     var yesString = "Yes";  
  46.     var noString = "No";  
  47.     if (typeof options != 'undefined') {  
  48.         if(typeof options.installTitle != 'undefined') {  
  49.             installTitle = options.installTitle;  
  50.         }  
  51.   
  52.         if(typeof options.installMessage != 'undefined') {  
  53.             installMessage = options.installMessage;  
  54.         }  
  55.   
  56.         if(typeof options.yesString != 'undefined') {  
  57.             yesString = options.yesString;  
  58.         }  
  59.   
  60.         if(typeof options.noString != 'undefined') {  
  61.             noString = options.noString;  
  62.         }  
  63.     }  
  64.       
  65.       
  66.     return PhoneGap.exec(function(args) {  
  67.         success(args);  
  68.     }, function(args) {  
  69.         fail(args);  
  70.     }, 'BarcodeScanner''scan', [types, installTitle, installMessage, yesString, noString]);  
  71. };  
  72. BarcodeScanner.prototype.encode = function(type, data, success, fail, options) {  
  73.   
  74.     /* These are the strings used in the dialog that appears if ZXing isn't installed */  
  75.     var installTitle = "Install Barcode Scanner?";  
  76.     var installMessage = "This requires the free Barcode Scanner app. Would you like to install it now?";  
  77.     var yesString = "Yes";  
  78.     var noString = "No";  
  79.     if (typeof options != 'undefined') {  
  80.         if(typeof options.installTitle != 'undefined') {  
  81.             installTitle = options.installTitle;  
  82.         }  
  83.   
  84.         if(typeof options.installMessage != 'undefined') {  
  85.             installMessage = options.installMessage;  
  86.         }  
  87.   
  88.         if(typeof options.yesString != 'undefined') {  
  89.             yesString = options.yesString;  
  90.         }  
  91.   
  92.         if(typeof options.noString != 'undefined') {  
  93.             noString = options.noString;  
  94.         }  
  95.     }  
  96.   
  97.     return PhoneGap.exec(function(args) {  
  98.         success(args);  
  99.     }, function(args) {  
  100.         fail(args);  
  101.     }, 'BarcodeScanner''encode', [type, data, installTitle, installMessage, yesString, noString]);  
  102. };  
  103.   
  104. PhoneGap.addConstructor(function() {  
  105.   //如果不支持window.plugins,则创建并设置     
  106.     if(!window.plugins){     
  107.         window.plugins={};     
  108.     }     
  109.     window.plugins.barcodeScanner=new BarcodeScanner();   
  110.     //PhoneGap.addPlugin('barcodeScanner', new BarcodeScanner());  
  111.     PluginManager.addService("BarcodeScanner","com.easyway.barcode.BarcodeScanner");  
  112. });  
 

二维码调用的页面如下:

 

Html代码  收藏代码
  1. >  
  2. <html>  
  3.   <head>  
  4.     <meta name="viewport" content="width=320; user-scalable=no" />  
  5.     <meta http-equiv="Content-type" content="text/html; charset=utf-8">  
  6.     <title>二维码扫描title>  
  7.   
  8.          
  9.     <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js">script>    
  10.         
  11.     <script type="text/javascript" charset="utf-8" src="jquery.mobile/jquery-1.6.4.min">script>    
  12.         
  13.     <script type="text/javascript" charset="utf-8" src="jquery.mobile/jquery.mobile-1.0.1.js">script>    
  14.         
  15.     <script type="text/javascript" charset="utf-8" src="barcodescanner.js">script>  
  16.     <script type="text/javascript" charset="utf-8">    
  17.            
  18.      $(function(){     
  19.         $("#btnbarcode").click(function(){     
  20.                 window.plugins.barcodeScanner.scan(   
  21.                         BarcodeScanner.Type.QR_CODE,   
  22.                         function(result) {  
  23.                             $("#barcodediv").html(""+result);  
  24.                         },   
  25.                         function(error) {  
  26.                             $("#barcodediv").html("扫描失败:"+result);  
  27.                         },   
  28.                         {  
  29.                             installTitle: "安装提示",  
  30.                             installMessage:"是否安装开源免费的ZXing二维码扫描器",  
  31.                             yesString:"确定",  
  32.                             noString:"取消"  
  33.                         }  
  34.             );  
  35.   
  36.         });     
  37.      });     
  38. script>       
  39.     
  40.    
  41.   head>  
  42.   <body >  
  43.     <h2>二维码扫描h2>  
  44.   
  45.     <p>二维码信息:p>  
  46.     <div id="barcodediv">div>  
  47.     <input type="button" id="btnbarcode" value="扫描" />  
  48.   body>  
  49. html>  

 

 

备注附件中phonegap的apk,下载之后需要重新命名为PhonegapBarcode.apk即可使用。

  • (861.2 KB)
  • 下载次数: 66
  • (271.8 KB)
  • 下载次数: 39

阅读(2283) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~