Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Added option to select frontcamera via JS api
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Aug 14, 2014
1 parent c93d756 commit 80a8285
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
8 changes: 4 additions & 4 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?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="1.2.2">
version="1.2.3">

<name>BarcodeScanner</name>
<description>You can use the BarcodeScanner plugin to scan different types of barcodes (using the device's camera) and get the metadata encoded in them for processing within your application.</description>
<license>MIT</license>

<engines>
<engine name="cordova" version=">=3.0.0" />
</engines>
</engines>

<js-module src="www/barcodescanner.js" name="BarcodeScanner">
<clobbers target="cordova.plugins.barcodeScanner" />
Expand Down Expand Up @@ -64,7 +64,7 @@
<!--
<source-file src="R.java" target-dir="src/com/google/zxing/client/android" />
-->

<!--
<config-file target="res/xml/plugins.xml" parent="/plugins">
<plugin name="BarcodeScanner" value="com.phonegap.plugins.barcodescanner.BarcodeScanner"/>
Expand Down Expand Up @@ -119,7 +119,7 @@
search: (src/android/LibraryProject/(.+?)/[^/]+)$
replace: <source-file src="$1" target-dir="$2"/>
-->

<source-file src="src/android/LibraryProject/res/drawable/launcher_icon.png" target-dir="res/drawable"/>
<source-file src="src/android/LibraryProject/res/drawable/share_via_barcode.png" target-dir="res/drawable"/>
<source-file src="src/android/LibraryProject/res/drawable/shopper_icon.png" target-dir="res/drawable"/>
Expand Down
Binary file modified src/android/com.google.zxing.client.android.captureactivity.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package com.phonegap.plugins.barcodescanner;

import com.google.zxing.client.android.Intents;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -35,6 +36,7 @@ public class BarcodeScanner extends CordovaPlugin {
private static final String TEXT = "text";
private static final String DATA = "data";
private static final String TYPE = "type";
private static final String PREFER_FRONTCAMERA = "preferFrontCamera";
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";
Expand Down Expand Up @@ -96,7 +98,12 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
return true;
}
} else if (action.equals(SCAN)) {
scan();
JSONObject obj = args.optJSONObject(0);
boolean preferFrontCamera = false;
if (obj != null) {
preferFrontCamera = obj.optBoolean(PREFER_FRONTCAMERA, false);
}
scan(preferFrontCamera);
} else {
return false;
}
Expand All @@ -106,12 +113,14 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
/**
* Starts an intent to scan and decode a barcode.
*/
public void scan() {
public void scan(boolean preferFrontCamera) {
Intent intentScan = new Intent(SCAN_INTENT);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
// avoid calling other phonegap apps
intentScan.setPackage(this.cordova.getActivity().getApplicationContext().getPackageName());

intentScan.putExtra(Intents.Scan.PREFER_FRONTCAMERA, preferFrontCamera);

this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, REQUEST_CODE);
}

Expand Down
20 changes: 13 additions & 7 deletions src/ios/CDVBarcodeScanner.mm
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,15 @@ - (void)scan:(CDVInvokedUrlCommand*)command {
NSString* capabilityError;

callback = command.callbackId;

// We allow the user to define an alternate xib file for loading the overlay.
NSString *overlayXib = nil;
if ( [command.arguments count] >= 1 )
{
overlayXib = [command.arguments objectAtIndex:0];

NSDictionary* options = [command.arguments objectAtIndex:0];
if ([options isKindOfClass:[NSNull class]]) {
options = [NSDictionary dictionary];
}

BOOL preferFrontCamera = [[options objectForKey:@"preferFrontCamera"] boolValue];
// We allow the user to define an alternate xib file for loading the overlay.
NSString *overlayXib = [options objectForKey:@"overlayXib"];

capabilityError = [self isScanNotPossible];
if (capabilityError) {
[self returnError:capabilityError callback:callback];
Expand All @@ -153,6 +154,11 @@ - (void)scan:(CDVInvokedUrlCommand*)command {
[processor retain];
[processor retain];
// queue [processor scanBarcode] to run on the event loop

if (preferFrontCamera) {
processor.isFrontCamera = true;
}

[processor performSelector:@selector(scanBarcode) withObject:nil afterDelay:0];
}

Expand Down
17 changes: 9 additions & 8 deletions www/barcodescanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@
};
};

/**
* Read code from scanner.
*
* @param {Function} successCallback This function will recieve a result object: {
/**
* Read code from scanner.
*
* @param {Function} successCallback This function will recieve a result object: {
* text : '12345-mock', // The code that was scanned.
* format : 'FORMAT_NAME', // Code format.
* cancelled : true/false, // Was canceled.
* }
* @param {Function} errorCallback
*/
BarcodeScanner.prototype.scan = function (successCallback, errorCallback) {
* @param {Function} errorCallback
* @param options
*/
BarcodeScanner.prototype.scan = function (successCallback, errorCallback, options) {
if (errorCallback == null) {
errorCallback = function () {
};
Expand All @@ -57,7 +58,7 @@
return;
}

exec(successCallback, errorCallback, 'BarcodeScanner', 'scan', []);
exec(successCallback, errorCallback, 'BarcodeScanner', 'scan', [options]);
};

//-------------------------------------------------------------------
Expand Down

0 comments on commit 80a8285

Please sign in to comment.