This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ryan Willoughby
committed
Oct 4, 2012
1 parent
6de3087
commit 9219728
Showing
7 changed files
with
18,259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
id="com.phonegap.plugins.barcodescanner" | ||
version="0.0.1"> | ||
|
||
<name>BarcodeScanner</name> | ||
|
||
<asset src="www/barcodescanner.js" target="barcodescanner.js" /> | ||
|
||
<!-- ios --> | ||
<platform name="ios"> | ||
<plugins-plist key="CDVBarcodeScanner" string="CDVBarcodeScanner" /> | ||
|
||
<resource-file src="scannerOverlay.xib" /> | ||
|
||
<header-file src="zxing-all-in-one.h" /> | ||
|
||
<source-file src="CDVBarcodeScanner.mm" /> | ||
<source-file src="zxing-all-in-one.cpp" /> | ||
|
||
<framework src="libiconv.dylib" /> | ||
<framework src="AVFoundation.framework" /> | ||
<framework src="AssetsLibrary.framework" /> | ||
<framework src="CoreVideo.framework" /> | ||
</platform> | ||
|
||
<!-- android --> | ||
<platform name="android"> | ||
|
||
<config-file target="res/xml/plugins.xml" parent="/plugins"> | ||
<plugin name="BarcodeScanner" | ||
value="com.phonegap.plugins.barcodescanner.BarcodeScanner"/> | ||
</config-file> | ||
|
||
<config-file target="res/xml/config.xml" parent="/cordova/plugins"> | ||
<plugin name="BarcodeScanner" | ||
value="com.phonegap.plugins.barcodescanner.BarcodeScanner"/> | ||
</config-file> | ||
|
||
<source-file src="com/phonegap/plugins/barcodescanner/BarcodeScanner.java" target-dir="src/com/phonegap/plugins/barcodescanner" /> | ||
|
||
<config-file target="AndroidManifest.xml" parent="/manifest/application"> | ||
<activity | ||
android:name="com.google.zxing.client.android.CaptureActivity" | ||
android:screenOrientation="landscape" | ||
android:configChanges="orientation|keyboardHidden" | ||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" | ||
android:windowSoftInputMode="stateAlwaysHidden" | ||
android:exported="false"> | ||
<intent-filter> | ||
<action android:name="com.phonegap.plugins.barcodescanner.SCAN"/> | ||
<category android:name="android.intent.category.DEFAULT"/> | ||
</intent-filter> | ||
</activity> | ||
<activity | ||
android:name="com.google.zxing.client.android.encode.EncodeActivity" | ||
android:label="@string/share_name"> | ||
<intent-filter> | ||
<action android:name="com.phonegap.plugins.barcodescanner.ENCODE"/> | ||
<category android:name="android.intent.category.DEFAULT"/> | ||
</intent-filter> | ||
</activity> | ||
</config> | ||
|
||
</platform> | ||
</plugin> |
152 changes: 152 additions & 0 deletions
152
src/android/com/phonegap/plugins/barcodescanner/BarcodeScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/** | ||
* PhoneGap is available under *either* the terms of the modified BSD license *or* the | ||
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text. | ||
* | ||
* Copyright (c) Matt Kane 2010 | ||
* Copyright (c) 2011, IBM Corporation | ||
*/ | ||
|
||
package com.phonegap.plugins.barcodescanner; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.util.Log; | ||
|
||
import org.apache.cordova.api.Plugin; | ||
import org.apache.cordova.api.PluginResult; | ||
|
||
/** | ||
* This calls out to the ZXing barcode reader and returns the result. | ||
*/ | ||
public class BarcodeScanner extends Plugin { | ||
private static final String SCAN = "scan"; | ||
private static final String ENCODE = "encode"; | ||
private static final String CANCELLED = "cancelled"; | ||
private static final String FORMAT = "format"; | ||
private static final String TEXT = "text"; | ||
private static final String DATA = "data"; | ||
private static final String TYPE = "type"; | ||
private static final String SCAN_INTENT = "com.phonegap.plugins.barcodescanner.SCAN"; | ||
private static final String ENCODE_DATA = "ENCODE_DATA"; | ||
private static final String ENCODE_TYPE = "ENCODE_TYPE"; | ||
private static final String ENCODE_INTENT = "com.phonegap.plugins.barcodescanner.ENCODE"; | ||
private static final String TEXT_TYPE = "TEXT_TYPE"; | ||
private static final String EMAIL_TYPE = "EMAIL_TYPE"; | ||
private static final String PHONE_TYPE = "PHONE_TYPE"; | ||
private static final String SMS_TYPE = "SMS_TYPE"; | ||
|
||
public static final int REQUEST_CODE = 0x0ba7c0de; | ||
|
||
public String callback; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public BarcodeScanner() { | ||
} | ||
|
||
/** | ||
* 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; | ||
|
||
if (action.equals(ENCODE)) { | ||
JSONObject obj = args.optJSONObject(0); | ||
if (obj != null) { | ||
String type = obj.optString(TYPE); | ||
String data = obj.optString(DATA); | ||
|
||
// If the type is null then force the type to text | ||
if (type == null) { | ||
type = TEXT_TYPE; | ||
} | ||
|
||
if (data == null) { | ||
return new PluginResult(PluginResult.Status.ERROR, "User did not specify data to encode"); | ||
} | ||
|
||
encode(type, data); | ||
} else { | ||
return new PluginResult(PluginResult.Status.ERROR, "User did not specify data to encode"); | ||
} | ||
} | ||
else if (action.equals(SCAN)) { | ||
scan(); | ||
} else { | ||
return new PluginResult(PluginResult.Status.INVALID_ACTION); | ||
} | ||
PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); | ||
r.setKeepCallback(true); | ||
return r; | ||
} | ||
|
||
|
||
/** | ||
* Starts an intent to scan and decode a barcode. | ||
*/ | ||
public void scan() { | ||
Intent intentScan = new Intent(SCAN_INTENT); | ||
intentScan.addCategory(Intent.CATEGORY_DEFAULT); | ||
|
||
this.cordova.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE); | ||
} | ||
|
||
/** | ||
* Called when the barcode scanner intent completes | ||
* | ||
* @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) { | ||
JSONObject obj = new JSONObject(); | ||
try { | ||
obj.put(TEXT, intent.getStringExtra("SCAN_RESULT")); | ||
obj.put(FORMAT, intent.getStringExtra("SCAN_RESULT_FORMAT")); | ||
obj.put(CANCELLED, false); | ||
} catch(JSONException e) { | ||
//Log.d(LOG_TAG, "This should never happen"); | ||
} | ||
this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback); | ||
} if (resultCode == Activity.RESULT_CANCELED) { | ||
JSONObject obj = new JSONObject(); | ||
try { | ||
obj.put(TEXT, ""); | ||
obj.put(FORMAT, ""); | ||
obj.put(CANCELLED, true); | ||
} catch(JSONException e) { | ||
//Log.d(LOG_TAG, "This should never happen"); | ||
} | ||
this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback); | ||
} else { | ||
this.error(new PluginResult(PluginResult.Status.ERROR), this.callback); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Initiates a barcode encode. | ||
* @param data The data to encode in the bar code | ||
* @param data2 | ||
*/ | ||
public void encode(String type, String data) { | ||
Intent intentEncode = new Intent(ENCODE_INTENT); | ||
intentEncode.putExtra(ENCODE_TYPE, type); | ||
intentEncode.putExtra(ENCODE_DATA, data); | ||
|
||
this.cordova.getActivity().startActivity(intentEncode); | ||
} | ||
} |
Oops, something went wrong.