`
longgangbai
  • 浏览: 7339758 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

            在phonegap中通过二维码实现也是通过扩展phonegap的插件实现。

项目结构如下:

实现如下:

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

 

package com.easyway.barcode;



import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;

import com.phonegap.api.PhonegapActivity;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

/**
 * 扩展二维码扫描的phonegap类实现
 * 实现原理如下:
 *    1.使用phonegap的js类库实现通过插件调用相关的Plugin java类。
 *    2.plugin调用zxing相关的二维码扫码的方法实现。
 *    3.如果调用zxing没有安装,到google下载相关的zxing apk安装,并调用对应的intent实现。
 * 
 * This calls out to the ZXing barcode reader and returns the result.
 */
public class BarcodeScanner extends Plugin {
	public static final int REQUEST_CODE = 0x0ba7c0de;


	public static final String defaultInstallTitle = "Install Barcode Scanner?";
	public static final String defaultInstallMessage = "This requires the free Barcode Scanner app. Would you like to install it now?";
	public static final String defaultYesString = "Yes";
	public static final String defaultNoString = "No";

	public String callback;

    /**
     * Constructor.
     */
	public BarcodeScanner() {
	}

	/**
	 * 
	 * 用于plugin相关的方法,用于暴露相关的方法使用。
	 * 
	 * Executes the request and returns PluginResult.
	 *
	 * @param action 		The action to execute.
	 * @param args 			JSONArray of arguments for the plugin.
	 * @param callbackId	The callback id used when calling back into JavaScript.
	 * @return 				A PluginResult object with a status and message.
	 */
	public PluginResult execute(String action, JSONArray args, String callbackId) {
		this.callback = callbackId;

		try {
			if (action.equals("encode")) {
				String type = null;
				if(args.length() > 0) {
					type = args.getString(0);
				}

				String data = null;
				if(args.length() > 1) {
					data = args.getString(1);
				}

				String installTitle = defaultInstallTitle;
				if(args.length() > 2) {
					installTitle = args.getString(2);
				}

				String installMessage = defaultInstallMessage;
				if(args.length() > 3) {
					installMessage = args.getString(3);
				}

				String yesString = defaultYesString;
				if(args.length() > 4) {
					yesString = args.getString(4);
				}

				String noString = defaultNoString;
				if(args.length() > 5) {
					noString = args.getString(5);
				}

				// if data.TypeOf() == Bundle, then call
				// encode(type, Bundle)
				// else
				// encode(type, String)
				this.encode(type, data, installTitle, installMessage, yesString, noString);
			}
			else if (action.equals("scan")) {
				String barcodeTypes = null;
				if(args.length() > 0) {
					barcodeTypes = args.getString(0);
				}

				String installTitle = defaultInstallTitle;
				if(args.length() > 1) {
					installTitle = args.getString(1);
				}

				String installMessage = defaultInstallMessage;
				if(args.length() > 2) {
					installMessage = args.getString(2);
				}

				String yesString = defaultYesString;
				if(args.length() > 3) {
					yesString = args.getString(3);
				}

				String noString = defaultNoString;
				if(args.length() > 4) {
					noString = args.getString(4);
				}

				scan(barcodeTypes, installTitle, installMessage, yesString, noString);
			} else {
	            return new PluginResult(PluginResult.Status.INVALID_ACTION);
			}
			PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
			r.setKeepCallback(true);
			return r;
		} catch (JSONException e) {
			e.printStackTrace();
			return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
		}
	}


	/**
	 * 扫描二维码的方法
	 *    备注:在扫描二维码的类型最好不好设置,在前期的zxing可能需要,在后期的版本中不需要,
	 *    zxing会自动检索二维码的类型,并识别相关二维码。
	 *    
	 * Initiates a barcode scan. If the ZXing scanner isn't installed, the user
	 * will be prompted to install it.
	 * @param types	The barcode types to accept
	 * @param installTitle The title for the dialog box that prompts the user to install the scanner
	 * @param installMessage The message prompting the user to install the barcode scanner
	 * @param yesString The string "Yes" or localised equivalent
	 * @param noString The string "No" or localised version
	 */
	public void scan(String barcodeFormats, String installTitle, String installMessage, String yesString, String noString ) {
	    Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");
	   // intentScan.addCategory(Intent.CATEGORY_DEFAULT);

	    //设置扫描特定类型的二维码
	    //if (barcodeFormats != null) {
	    //      Tell the scanner what types we're after
	    //			intentScan.putExtra("SCAN_FORMATS", barcodeFormats);
	    // }
	    try {
			this.ctx.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE);
	    } catch (ActivityNotFoundException e) {
	    	showDownloadDialog(installTitle, installMessage, yesString, noString);
	    }
	}

    /**
     * 用于获取二维码扫描之后获取相关的二维码相关的信息
     * Called when the barcode scanner exits
     *
     * @param requestCode		The request code originally supplied to startActivityForResult(),
     * 							allowing you to identify who this result came from.
     * @param resultCode		The integer result code returned by the child activity through its setResult().
     * @param intent			An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
     */
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == REQUEST_CODE) {
			if (resultCode == Activity.RESULT_OK) {
				String contents = intent.getStringExtra("SCAN_RESULT");
				String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
				this.success(new PluginResult(PluginResult.Status.OK, " 条形码为:"+contents+" 条码类型为: "+format), this.callback);
			} else {
				this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
			}
		}
	}

	/**
	 * 创建相关的对话框,在通过没有安装相关的zxing开源组件时候,调用远程的intent或者下载相关执行类实现相关的
	 * 功能
	 * @param title
	 * @param message
	 * @param yesString
	 * @param noString
	 */
	private void showDownloadDialog(final String title, final String message, final String yesString, final String noString) {
		final PhonegapActivity context = this.ctx;
		Runnable runnable = new Runnable() {
			public void run() {

				AlertDialog.Builder dialog = new AlertDialog.Builder(context);
				dialog.setTitle(title);
				dialog.setMessage(message);
				dialog.setPositiveButton(yesString, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dlg, int i) {
						dlg.dismiss();
						Intent intent = new Intent(Intent.ACTION_VIEW,
												   Uri.parse("market://search?q=pname:com.google.zxing.client.android")
												   );
						try {
							context.startActivity(intent);
						} catch (ActivityNotFoundException e) {
							//	We don't have the market app installed, so download it directly.
							Intent in = new Intent(Intent.ACTION_VIEW);
							in.setData(Uri.parse("http://zxing.googlecode.com/files/BarcodeScanner4.1.apk"));
							context.startActivity(in);

						}

					}
				});
				dialog.setNegativeButton(noString, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dlg, int i) {
						dlg.dismiss();
					}
				});
				dialog.create();
				dialog.show();
			}
		};
		context.runOnUiThread(runnable);
	}

	/**
	 * 
	 * 
	 * Initiates a barcode encode. If the ZXing scanner isn't installed, the user
	 * will be prompted to install it.
	 * @param type  The barcode type to encode
	 * @param data  The data to encode in the bar code
	 * @param installTitle The title for the dialog box that prompts the user to install the scanner
	 * @param installMessage The message prompting the user to install the barcode scanner
	 * @param yesString The string "Yes" or localised equivalent
	 * @param noString The string "No" or localised version
	 */
	public void encode(String type, String data, String installTitle, String installMessage, String yesString, String noString) {
		Intent intentEncode = new Intent("com.google.zxing.client.android.ENCODE");
		intentEncode.putExtra("ENCODE_TYPE", type);
		intentEncode.putExtra("ENCODE_DATA", data);

		try {
			this.ctx.startActivity(intentEncode);
		} catch (ActivityNotFoundException e) {
			showDownloadDialog(installTitle, installMessage, yesString, noString);
		}
	}
}

 

 

package com.easyway.barcode;

import com.phonegap.*;
import android.os.Bundle;
/**
 * phonegap实现相关的二维码的扫码功能
 * 
 * @Title: 
 * @Description: 实现phonegap实现相关的二维码的扫码功能
 * @Copyright:Copyright (c) 2011
 * @Company:易程科技股份有限公司
 * @Date:2012-5-10
 * @author  longgangbai
 * @version 1.0
 */
public class PhonegapBarcodeActivity extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
    }
}
 

二维码扫码实现的js:

 

/**
 * Phonegap Barcode Scanner plugin
 * Copyright (c) Matt Kane 2010
 *
 */
var BarcodeScanner = function() { 

}

BarcodeScanner.Type = {
	QR_CODE: "QR_CODE",
	DATA_MATRIX: "DATA_MATRIX",
	UPC_E: "UPC_E",
	UPC_A: "UPC_A",
	EAN_8: "EAN_8",
	EAN_13: "EAN_13",
	CODE_128: "CODE_128",
	CODE_39: "CODE_39",
	CODE_93: "CODE_93",
	CODABAR: "CODABAR",
	ITF: "ITF",
	RSS14: "RSS14",
	PDF417: "PDF417",
	RSS_EXPANDED: "RSS_EXPANDED",
	PRODUCT_CODE_TYPES: "UPC_A,UPC_E,EAN_8,EAN_13",
	ONE_D_CODE_TYPES: "UPC_A,UPC_E,EAN_8,EAN_13,CODE_39,CODE_93,CODE_128",
	QR_CODE_TYPES: "QR_CODE",
	ALL_CODE_TYPES: null
}

BarcodeScanner.Encode = {
		TEXT_TYPE: "TEXT_TYPE",
		EMAIL_TYPE: "EMAIL_TYPE",
		PHONE_TYPE: "PHONE_TYPE",
		SMS_TYPE: "SMS_TYPE",
		//  CONTACT_TYPE: "CONTACT_TYPE",  // TODO:  not implemented, requires passing a Bundle class from Javascriopt to Java
		//  LOCATION_TYPE: "LOCATION_TYPE" // TODO:  not implemented, requires passing a Bundle class from Javascriopt to Java
	}
//二维码扫描的方法
BarcodeScanner.prototype.scan = function(types, success, fail, options) {
	
	/* These are the strings used in the dialog that appears if ZXing isn't installed */
	var installTitle = "Install Barcode Scanner?";
	var installMessage = "This requires the free Barcode Scanner app. Would you like to install it now?";
	var yesString = "Yes";
	var noString = "No";
	if (typeof options != 'undefined') {
		if(typeof options.installTitle != 'undefined') {
			installTitle = options.installTitle;
		}

		if(typeof options.installMessage != 'undefined') {
			installMessage = options.installMessage;
		}

		if(typeof options.yesString != 'undefined') {
			yesString = options.yesString;
		}

		if(typeof options.noString != 'undefined') {
			noString = options.noString;
		}
	}
	
	
    return PhoneGap.exec(function(args) {
        success(args);
    }, function(args) {
        fail(args);
    }, 'BarcodeScanner', 'scan', [types, installTitle, installMessage, yesString, noString]);
};
BarcodeScanner.prototype.encode = function(type, data, success, fail, options) {

	/* These are the strings used in the dialog that appears if ZXing isn't installed */
	var installTitle = "Install Barcode Scanner?";
	var installMessage = "This requires the free Barcode Scanner app. Would you like to install it now?";
	var yesString = "Yes";
	var noString = "No";
	if (typeof options != 'undefined') {
		if(typeof options.installTitle != 'undefined') {
			installTitle = options.installTitle;
		}

		if(typeof options.installMessage != 'undefined') {
			installMessage = options.installMessage;
		}

		if(typeof options.yesString != 'undefined') {
			yesString = options.yesString;
		}

		if(typeof options.noString != 'undefined') {
			noString = options.noString;
		}
	}

    return PhoneGap.exec(function(args) {
        success(args);
    }, function(args) {
        fail(args);
    }, 'BarcodeScanner', 'encode', [type, data, installTitle, installMessage, yesString, noString]);
};

PhoneGap.addConstructor(function() {
  //如果不支持window.plugins,则创建并设置   
    if(!window.plugins){   
        window.plugins={};   
    }   
    window.plugins.barcodeScanner=new BarcodeScanner(); 
	//PhoneGap.addPlugin('barcodeScanner', new BarcodeScanner());
	PluginManager.addService("BarcodeScanner","com.easyway.barcode.BarcodeScanner");
});
 

二维码调用的页面如下:

 

<!DOCTYPE HTML>
<html>
  <head>
    <meta name="viewport" content="width=320; user-scalable=no" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>二维码扫描</title>

     <!-- 加载phonegap -->  
    <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>  
    <!-- 加载jquery -->  
    <script type="text/javascript" charset="utf-8" src="jquery.mobile/jquery-1.6.4.min"></script>  
    <!-- 加载jquerymobile -->  
    <script type="text/javascript" charset="utf-8" src="jquery.mobile/jquery.mobile-1.0.1.js"></script>  
    <!-- 加载自定义插件 -->  
    <script type="text/javascript" charset="utf-8" src="barcodescanner.js"></script>
    <script type="text/javascript" charset="utf-8">  
         
     $(function(){   
        $("#btnbarcode").click(function(){   
	            window.plugins.barcodeScanner.scan( 
	                    BarcodeScanner.Type.QR_CODE, 
	                    function(result) {
	                        $("#barcodediv").html(""+result);
	                    }, 
	                    function(error) {
	                        $("#barcodediv").html("扫描失败:"+result);
	            		}, 
	            		{
	            		    installTitle: "安装提示",
	            		    installMessage:"是否安装开源免费的ZXing二维码扫描器",
	            		    yesString:"确定",
	            		    noString:"取消"
	            		}
	        );

        });   
     });   
</script>     
  
 
  </head>
  <body >
    <h2>二维码扫描</h2>

	<p>二维码信息:</p>
	<div id="barcodediv"></div>
	<input type="button" id="btnbarcode" value="扫描" />
  </body>
</html>

 

 

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

分享到:
评论
1 楼 hyqaxxs 2014-03-20  
不好使啊,扫描出来都是NUll

相关推荐

    二、phoneGap项目中使用二维码扫描功能

    PhoneGap项目中集成二维码扫描功能是一项常见的需求,特别是在开发跨平台移动应用时。PhoneGap,现称为Cordova,是一个开放源代码的框架,允许开发者使用HTML、CSS和JavaScript来构建原生的移动应用。本篇将详细介绍...

    phonegap-iphone条码二维码扫描插件

    BarcodeScanner插件是PhoneGap生态系统中的一个重要组成部分,它允许开发者通过JavaScript调用设备的摄像头进行条形码和二维码的扫描,无需掌握原生iOS编程语言如Objective-C或Swift。 二、安装 BarcodeScanner ...

    android+Phonegap开发环境搭建

    总之,搭建Android+PhoneGap开发环境涉及安装JDK、Android Studio、Node.js和PhoneGap CLI,配置Android SDK,以及创建和运行第一个PhoneGap项目。掌握这个过程后,开发者就可以利用Web技术开发出能在Android平台上...

    Phonegap二维码扫描

    PhoneGap二维码扫描是一种在移动应用开发中实现二维码识别的技术,主要应用于Android平台。PhoneGap是一个开源框架,允许开发者使用HTML、CSS和JavaScript来构建原生的移动应用。它通过一个桥接机制,使得Web应用...

    使用jQuery Mobile + PhoneGap 开发Android应用程序

    使用jQuery Mobile + PhoneGap 开发Android应用程序

    phonegap之android示例

    通过这个项目,我们可以深入理解PhoneGap如何与Android原生环境相结合,以及如何利用JavaScript进行移动应用开发。 首先,PhoneGap的核心思想是将Web应用打包成原生应用。它通过一个WebView组件(在Android中是...

    sencha tocuh利用phonegap开发android的文件浏览器

    在本项目中,“sencha touch利用phonegap开发android的文件浏览器”,我们看到了这两个技术的结合。首先,PhoneGap的File类接口是关键,它遵循W3C的File API标准,提供了访问和操作本地文件系统的能力。开发者可以...

    phonegap+android开发环境配置

    总的来说,配置PhoneGap+Android开发环境需要安装JDK、Android SDK、可能的Eclipse/Android Studio,以及PhoneGap本身。确保所有组件都更新到最新版本,以获得最佳的开发体验。此外,熟悉PhoneGap API和Android SDK...

    phonegap插件包Android版本

    在这个"phonegap插件包Android版本"中,我们主要关注的是如何在Android平台上利用PhoneGap开发应用,并集成特定的插件。 1. **PhoneGap框架介绍**:PhoneGap基于Apache Cordova,提供了一个统一的API接口,使得...

    自定义plugin插件实现phonegap与Android交互

    这个框架的主要优点是能够跨平台开发,使得开发者可以用一套代码库为iOS、Android等不同操作系统创建应用。然而,虽然JavaScript可以处理大部分的前端逻辑,但当涉及到与设备硬件或操作系统更深度的交互时,例如访问...

    phonegap扫码项目包

    2个项目目录,都引入,把zxing当作库引入到mylchscm里,mylchscm就能运行了,生成的apk不用单装zxing,基本上没做什么功能,就是把架子搭好了,能扫码,能正确得到返回值,要实现什么功能就自己写js去吧。...

    phoneGap-Android开发环境搭建

    本教程将详细介绍如何搭建PhoneGap在Android平台上的开发环境,以便你可以开始利用Web技术进行Android应用的开发。 首先,我们需要安装Java Development Kit (JDK)。JDK是开发Android应用的基础,因为Android ...

    cordova二维码扫描插件

    本文将详细介绍"Cordova二维码扫描插件",它是一个专为Ionic、PhoneGap等混合应用程序(Hybrid APP)设计的高效、美观且兼容iOS和Android平台的二维码扫描解决方案。相较于PhoneGap官方提供的二维码扫描插件,这个...

    phoneGap实现android程序开发代码

    在这个“phoneGap实现android程序开发代码”中,我们将探讨如何使用PhoneGap进行Android应用开发。 首先,我们需要安装PhoneGap的开发环境。这包括安装Java Development Kit (JDK),Android SDK,以及集成开发环境...

    android与phonegap混合开发之相互跳转

    在移动应用开发领域,PhoneGap和Android是两种广泛使用的平台。PhoneGap是一种基于HTML5、CSS3和JavaScript的跨平台框架,它允许开发者使用Web技术创建原生移动应用。而Android则是Google主导的开源操作系统,提供了...

    Eclipse+android+jdk+phonegap开发环境教程.pdf

    Eclipse+Android+JDK+PhoneGap 开发环境教程 本文将介绍如何安装基于 Android SDK 下安装 PhoneGap 框架,并详细讲解 PhoneGap 开发环境的配置过程。 一、前言 PhoneGap 是一个跨平台的移动应用开发框架,允许...

    PhoneGap开发Android 程序例子(只用Javascript+HTML并跨手机平台)

    该Simple已经实现了GPS定位,重力感应,声音调用,振动,摄像头调用...  PhoneGap是一款开源的手机应用开发平台,它仅仅... 目前,PhoneGap已实现对iPhone/ipad、Android、Symbian,Palm、黑莓各版本绝大部分功能的支持

    基于PhoneGap的Android应用开发

    ### 基于PhoneGap的Android应用开发 #### 一、引言 随着移动互联网的飞速发展,跨平台移动应用开发技术越来越受到开发者们的青睐。PhoneGap作为一款开源框架,能够利用HTML5、CSS3与JavaScript等Web技术来开发跨...

Global site tag (gtag.js) - Google Analytics