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(),
*
没有合适的资源?快使用搜索试试~ 我知道了~
二维玛扫描phonegap插件
共4个文件
java:2个
html:1个
js:1个
3星 · 超过75%的资源 需积分: 10 6 下载量 79 浏览量
2014-01-07
11:42:47
上传
评论
收藏 5KB RAR 举报
温馨提示
二维玛扫描phonegap插件,包括java代码,js代码,html代码
资源推荐
资源详情
资源评论
收起资源包目录
phonegap插件.rar (4个子文件)
二维玛扫描
index.html 2KB
BarcodeScanner.java 13KB
barcodescanner.js 4KB
PhonegapBarcodeActivity.java 686B
共 4 条
- 1
资源评论
- 方丈-麻煩給我剃個頭2014-01-15有点坑呀,不怎么好用。
wangguishan
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功