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 choose to show a 'flip camera' button (default…
Browse files Browse the repository at this point in the history
…: false)
  • Loading branch information
EddyVerbruggen committed Aug 14, 2014
1 parent 80a8285 commit 9f19420
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -749,17 +749,24 @@ private void resetStatusView() {
viewfinderView.setVisibility(View.VISIBLE);
lastResult = null;

// in case the device has multiple camera's, offer a flip button
if (Camera.getNumberOfCameras() > 1) { //
flipButton.setVisibility(View.VISIBLE);
flipButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
getIntent().putExtra(Intents.Scan.PREFER_FRONTCAMERA,
!getIntent().hasExtra(Intents.Scan.PREFER_FRONTCAMERA) ||
!getIntent().getBooleanExtra(Intents.Scan.PREFER_FRONTCAMERA, false));
recreate();
}});
// in case the device has multiple camera's and we want to show the flip button: show the flip button :)
if (getIntent().getBooleanExtra(Intents.Scan.SHOW_FLIP_CAMERA_BUTTON, false)) {
if (Camera.getNumberOfCameras() > 1) {
flipButton.setVisibility(View.VISIBLE);
flipButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {

getIntent().putExtra(Intents.Scan.PREFER_FRONTCAMERA,
!getIntent().hasExtra(Intents.Scan.PREFER_FRONTCAMERA) ||
!getIntent().getBooleanExtra(Intents.Scan.PREFER_FRONTCAMERA, false));

getIntent().putExtra(Intents.Scan.SHOW_FLIP_CAMERA_BUTTON, true);

recreate();
}
});
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ public static final class Scan {
*/
public static final String PREFER_FRONTCAMERA = "PREFER_FRONTCAMERA";

/**
* Set to true if we show the button to flip the camera (if available)
*/
public static final String SHOW_FLIP_CAMERA_BUTTON = "SHOW_FLIP_CAMERA_BUTTON";

private Scan() {
}
}
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 @@ -37,6 +37,7 @@ public class BarcodeScanner extends CordovaPlugin {
private static final String DATA = "data";
private static final String TYPE = "type";
private static final String PREFER_FRONTCAMERA = "preferFrontCamera";
private static final String SHOW_FLIP_CAMERA_BUTTON = "showFlipCameraButton";
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 @@ -100,10 +101,12 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
} else if (action.equals(SCAN)) {
JSONObject obj = args.optJSONObject(0);
boolean preferFrontCamera = false;
boolean showFlipCameraButton = false;
if (obj != null) {
preferFrontCamera = obj.optBoolean(PREFER_FRONTCAMERA, false);
showFlipCameraButton = obj.optBoolean(SHOW_FLIP_CAMERA_BUTTON, false);
}
scan(preferFrontCamera);
scan(preferFrontCamera, showFlipCameraButton);
} else {
return false;
}
Expand All @@ -113,15 +116,16 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
/**
* Starts an intent to scan and decode a barcode.
*/
public void scan(boolean preferFrontCamera) {
public void scan(boolean preferFrontCamera, boolean showFlipCameraButton) {
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);
intentScan.putExtra(Intents.Scan.SHOW_FLIP_CAMERA_BUTTON, showFlipCameraButton);

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

/**
Expand Down
22 changes: 18 additions & 4 deletions src/ios/CDVBarcodeScanner.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ @interface CDVbcsProcessor : NSObject <AVCaptureVideoDataOutputSampleBufferDeleg
@property (nonatomic) BOOL is2D;
@property (nonatomic) BOOL capturing;
@property (nonatomic) BOOL isFrontCamera;
@property (nonatomic) BOOL isShowFlipCameraButton;
@property (nonatomic) BOOL isFlipped;


Expand Down Expand Up @@ -135,7 +136,8 @@ - (void)scan:(CDVInvokedUrlCommand*)command {
options = [NSDictionary dictionary];
}
BOOL preferFrontCamera = [[options objectForKey:@"preferFrontCamera"] boolValue];
// We allow the user to define an alternate xib file for loading the overlay.
BOOL showFlipCameraButton = [[options objectForKey:@"showFlipCameraButton"] boolValue];
// We allow the user to define an alternate xib file for loading the overlay.
NSString *overlayXib = [options objectForKey:@"overlayXib"];

capabilityError = [self isScanNotPossible];
Expand All @@ -158,6 +160,10 @@ - (void)scan:(CDVInvokedUrlCommand*)command {
if (preferFrontCamera) {
processor.isFrontCamera = true;
}

if (showFlipCameraButton) {
processor.isShowFlipCameraButton = true;
}

[processor performSelector:@selector(scanBarcode) withObject:nil afterDelay:0];
}
Expand Down Expand Up @@ -782,10 +788,18 @@ - (UIView*)buildOverlayView {
target:(id)self
action:@selector(shutterButtonPressed)
];

toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace, flipCamera ,shutterButton,nil];

if (_processor.isShowFlipCameraButton) {
toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace, flipCamera ,shutterButton,nil];
} else {
toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace ,shutterButton,nil];
}
#else
toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace, flipCamera,nil];
if (_processor.isShowFlipCameraButton) {
toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace, flipCamera,nil];
} else {
toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace,nil];
}
#endif
bounds = overlayView.bounds;

Expand Down

0 comments on commit 9f19420

Please sign in to comment.