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

Commit

Permalink
Flashlight option #381
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Dec 3, 2016
1 parent 77622ca commit a7ed689
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phonegap-plugin-barcodescanner",
"version": "6.0.3",
"version": "6.0.4",
"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.",
"cordova": {
"id": "phonegap-plugin-barcodescanner",
Expand Down
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:rim="http://www.blackberry.com/ns/widgets" id="phonegap-plugin-barcodescanner" version="6.0.3">
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:rim="http://www.blackberry.com/ns/widgets" id="phonegap-plugin-barcodescanner" version="6.0.4">
<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>
Expand Down Expand Up @@ -52,7 +52,7 @@
<uses-feature android:name="android.hardware.camera" android:required="true"/>
</config-file>
<framework src="src/android/barcodescanner.gradle" custom="true" type="gradleReference"/>
<resource-file src="src/android/barcodescanner-release-2.0.1.aar" target="libs/barcodescanner.aar"/>
<resource-file src="src/android/barcodescanner-release-2.1.0.aar" target="libs/barcodescanner.aar"/>
<dependency id="cordova-plugin-compat" version="^1.0.0"/>
</platform>
<platform name="windows">
Expand Down
Binary file removed src/android/barcodescanner-release-2.0.1.aar
Binary file not shown.
Binary file added src/android/barcodescanner-release-2.1.0.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class BarcodeScanner extends CordovaPlugin {
private static final String PREFER_FRONTCAMERA = "preferFrontCamera";
private static final String ORIENTATION = "orientation";
private static final String SHOW_FLIP_CAMERA_BUTTON = "showFlipCameraButton";
private static final String SHOW_TORCH_BUTTON = "showTorchButton";
private static final String FORMATS = "formats";
private static final String PROMPT = "prompt";
private static final String TEXT_TYPE = "TEXT_TYPE";
Expand Down Expand Up @@ -172,6 +173,7 @@ public void run() {

intentScan.putExtra(Intents.Scan.CAMERA_ID, obj.optBoolean(PREFER_FRONTCAMERA, false) ? 1 : 0);
intentScan.putExtra(Intents.Scan.SHOW_FLIP_CAMERA_BUTTON, obj.optBoolean(SHOW_FLIP_CAMERA_BUTTON, false));
intentScan.putExtra(Intents.Scan.SHOW_TORCH_BUTTON, obj.optBoolean(SHOW_TORCH_BUTTON, false));
if (obj.has(FORMATS)) {
intentScan.putExtra(Intents.Scan.FORMATS, obj.optString(FORMATS));
}
Expand Down
Binary file added src/ios/CDVBarcodeScanner.bundle/torch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/ios/CDVBarcodeScanner.bundle/torch@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/ios/CDVBarcodeScanner.bundle/torch@3x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 50 additions & 4 deletions src/ios/CDVBarcodeScanner.mm
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ @interface CDVbcsProcessor : NSObject <AVCaptureMetadataOutputObjectsDelegate> {
@property (nonatomic) BOOL capturing;
@property (nonatomic) BOOL isFrontCamera;
@property (nonatomic) BOOL isShowFlipCameraButton;
@property (nonatomic) BOOL isShowTorchButton;
@property (nonatomic) BOOL isFlipped;


Expand Down Expand Up @@ -162,6 +163,8 @@ - (void)scan:(CDVInvokedUrlCommand*)command {
}
BOOL preferFrontCamera = [options[@"preferFrontCamera"] boolValue];
BOOL showFlipCameraButton = [options[@"showFlipCameraButton"] boolValue];
BOOL showTorchButton = [options[@"showTorchButton"] boolValue];

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

Expand Down Expand Up @@ -191,6 +194,10 @@ - (void)scan:(CDVInvokedUrlCommand*)command {
processor.isShowFlipCameraButton = true;
}

if (showTorchButton) {
processor.isShowTorchButton = true;
}

processor.formats = options[@"formats"];

[processor performSelector:@selector(scanBarcode) withObject:nil afterDelay:0];
Expand Down Expand Up @@ -428,6 +435,19 @@ - (void)flipCamera {
}];
}

- (void)toggleTorch {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
if (device.flashActive) {
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
} else {
[device setTorchModeOnWithLevel:AVCaptureMaxAvailableTorchLevel error:nil];
[device setFlashMode:AVCaptureFlashModeOn];
}
[device unlockForConfiguration];
}

//--------------------------------------------------------------------------
- (NSString*)setUpCaptureSession {
NSError* error = nil;
Expand Down Expand Up @@ -893,6 +913,11 @@ - (void)flipCameraButtonPressed:(id)sender
[self.processor performSelector:@selector(flipCamera) withObject:nil afterDelay:0];
}

- (void)torchButtonPressed:(id)sender
{
[self.processor performSelector:@selector(toggleTorch) withObject:nil afterDelay:0];
}

//--------------------------------------------------------------------------
- (UIView *)buildOverlayViewFromXib
{
Expand Down Expand Up @@ -944,6 +969,8 @@ - (UIView*)buildOverlayView {
action:@selector(flipCameraButtonPressed:)
] autorelease];

NSMutableArray *items;

#if USE_SHUTTER
id shutterButton = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
Expand All @@ -952,17 +979,36 @@ - (UIView*)buildOverlayView {
] autorelease];

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

if (_processor.isShowTorchButton && !_processor.isFrontCamera) {
NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:@"CDVBarcodeScanner" withExtension:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
NSString *imagePath = [bundle pathForResource:@"torch" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

id torchButton = [[[UIBarButtonItem alloc]
initWithImage:image
style:UIBarButtonItemStylePlain
target:(id)self
action:@selector(torchButtonPressed:)
] autorelease];

[items insertObject:torchButton atIndex:0];
}

toolbar.items = items;

bounds = overlayView.bounds;

[toolbar sizeToFit];
Expand Down

0 comments on commit a7ed689

Please sign in to comment.