2014-09-12 01:45:17 +08:00
|
|
|
#import "CsZBar.h"
|
2015-09-15 06:50:42 +08:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
2015-01-23 17:36:49 +08:00
|
|
|
#import "AlmaZBarReaderViewController.h"
|
2014-09-12 01:45:17 +08:00
|
|
|
|
|
|
|
#pragma mark - State
|
|
|
|
|
|
|
|
@interface CsZBar ()
|
|
|
|
@property bool scanInProgress;
|
|
|
|
@property NSString *scanCallbackId;
|
2015-01-23 17:36:49 +08:00
|
|
|
@property AlmaZBarReaderViewController *scanReader;
|
2015-09-15 06:50:42 +08:00
|
|
|
|
2014-09-12 01:45:17 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark - Synthesize
|
|
|
|
|
|
|
|
@implementation CsZBar
|
|
|
|
|
|
|
|
@synthesize scanInProgress;
|
|
|
|
@synthesize scanCallbackId;
|
|
|
|
@synthesize scanReader;
|
|
|
|
|
|
|
|
#pragma mark - Cordova Plugin
|
|
|
|
|
2016-07-29 09:12:02 +08:00
|
|
|
- (void)pluginInitialize {
|
2014-09-12 01:45:17 +08:00
|
|
|
self.scanInProgress = NO;
|
|
|
|
}
|
2016-07-29 09:12:02 +08:00
|
|
|
|
2015-09-15 06:50:42 +08:00
|
|
|
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-12 01:45:17 +08:00
|
|
|
|
2016-07-29 09:12:02 +08:00
|
|
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
|
|
|
return YES;
|
2015-09-15 06:50:42 +08:00
|
|
|
}
|
2014-09-12 01:45:17 +08:00
|
|
|
|
|
|
|
#pragma mark - Plugin API
|
|
|
|
|
2016-07-29 09:12:02 +08:00
|
|
|
- (void)scan: (CDVInvokedUrlCommand*)command;
|
2014-09-12 01:45:17 +08:00
|
|
|
{
|
2016-07-29 09:12:02 +08:00
|
|
|
if (self.scanInProgress) {
|
2014-09-12 01:45:17 +08:00
|
|
|
[self.commandDelegate
|
|
|
|
sendPluginResult: [CDVPluginResult
|
|
|
|
resultWithStatus: CDVCommandStatus_ERROR
|
|
|
|
messageAsString:@"A scan is already in progress."]
|
|
|
|
callbackId: [command callbackId]];
|
|
|
|
} else {
|
|
|
|
self.scanInProgress = YES;
|
|
|
|
self.scanCallbackId = [command callbackId];
|
2015-01-23 17:36:49 +08:00
|
|
|
self.scanReader = [AlmaZBarReaderViewController new];
|
2014-09-12 01:45:17 +08:00
|
|
|
|
|
|
|
self.scanReader.readerDelegate = self;
|
2015-01-23 17:36:49 +08:00
|
|
|
self.scanReader.supportedOrientationsMask = ZBarOrientationMask(UIInterfaceOrientationPortrait);
|
2014-09-12 01:45:17 +08:00
|
|
|
|
2014-12-06 16:38:00 +08:00
|
|
|
// Get user parameters
|
|
|
|
NSDictionary *params = (NSDictionary*) [command argumentAtIndex:0];
|
2014-12-06 16:58:46 +08:00
|
|
|
NSString *camera = [params objectForKey:@"camera"];
|
2014-12-06 16:38:00 +08:00
|
|
|
if([camera isEqualToString:@"front"]) {
|
|
|
|
// We do not set any specific device for the default "back" setting,
|
|
|
|
// as not all devices will have a rear-facing camera.
|
|
|
|
self.scanReader.cameraDevice = UIImagePickerControllerCameraDeviceFront;
|
|
|
|
}
|
2015-09-15 06:50:42 +08:00
|
|
|
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
|
|
|
|
|
2014-12-06 16:58:46 +08:00
|
|
|
NSString *flash = [params objectForKey:@"flash"];
|
2016-07-29 09:12:02 +08:00
|
|
|
|
|
|
|
if ([flash isEqualToString:@"on"]) {
|
2014-12-06 16:58:46 +08:00
|
|
|
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
|
2016-07-29 09:12:02 +08:00
|
|
|
} else if ([flash isEqualToString:@"off"]) {
|
2014-12-06 16:58:46 +08:00
|
|
|
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
|
2016-07-29 09:12:02 +08:00
|
|
|
}else if ([flash isEqualToString:@"auto"]) {
|
|
|
|
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
|
2014-12-06 16:58:46 +08:00
|
|
|
}
|
2014-12-06 16:38:00 +08:00
|
|
|
|
2015-02-27 18:41:44 +08:00
|
|
|
// Hack to hide the bottom bar's Info button... originally based on http://stackoverflow.com/a/16353530
|
2016-07-31 22:59:49 +08:00
|
|
|
NSInteger infoButtonIndex;
|
|
|
|
if ([[[UIDevice currentDevice] systemVersion] compare:@"10.0" options:NSNumericSearch] != NSOrderedAscending) {
|
|
|
|
infoButtonIndex = 1;
|
|
|
|
} else {
|
|
|
|
infoButtonIndex = 3;
|
|
|
|
}
|
|
|
|
UIView *infoButton = [[[[[self.scanReader.view.subviews objectAtIndex:2] subviews] objectAtIndex:0] subviews] objectAtIndex:infoButtonIndex];
|
2015-02-27 18:41:44 +08:00
|
|
|
[infoButton setHidden:YES];
|
2015-09-15 06:50:42 +08:00
|
|
|
|
|
|
|
//UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:@"Press Me" forState:UIControlStateNormal]; [button sizeToFit]; [self.view addSubview:button];
|
2015-11-03 21:51:48 +08:00
|
|
|
CGRect screenRect = [[UIScreen mainScreen] bounds];
|
|
|
|
CGFloat screenWidth = screenRect.size.width;
|
|
|
|
CGFloat screenHeight = screenRect.size.height;
|
|
|
|
|
2015-01-23 21:32:14 +08:00
|
|
|
BOOL drawSight = [params objectForKey:@"drawSight"] ? [[params objectForKey:@"drawSight"] boolValue] : true;
|
2015-11-03 21:51:48 +08:00
|
|
|
UIToolbar *toolbarViewFlash = [[UIToolbar alloc] init];
|
2016-07-29 09:12:02 +08:00
|
|
|
|
2015-11-03 21:51:48 +08:00
|
|
|
//The bar length it depends on the orientation
|
|
|
|
toolbarViewFlash.frame = CGRectMake(0.0, 0, (screenWidth > screenHeight ?screenWidth:screenHeight), 44.0);
|
|
|
|
toolbarViewFlash.barStyle = UIBarStyleBlackOpaque;
|
|
|
|
UIBarButtonItem *buttonFlash = [[UIBarButtonItem alloc] initWithTitle:@"Flash" style:UIBarButtonItemStyleDone target:self action:@selector(toggleflash)];
|
2016-07-29 09:12:02 +08:00
|
|
|
|
2015-11-03 21:51:48 +08:00
|
|
|
NSArray *buttons = [NSArray arrayWithObjects: buttonFlash, nil];
|
|
|
|
[toolbarViewFlash setItems:buttons animated:NO];
|
|
|
|
[self.scanReader.view addSubview:toolbarViewFlash];
|
|
|
|
|
2016-07-29 09:12:02 +08:00
|
|
|
if (drawSight) {
|
2014-12-22 22:51:57 +08:00
|
|
|
CGFloat dim = screenWidth < screenHeight ? screenWidth / 1.1 : screenHeight / 1.1;
|
2015-09-15 06:50:42 +08:00
|
|
|
UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( (screenWidth/2) - (dim/2), (screenHeight/2) - (dim/2), dim, dim)];
|
2016-07-29 09:12:02 +08:00
|
|
|
|
2015-09-15 06:50:42 +08:00
|
|
|
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,dim / 2, dim, 1)];
|
2014-12-22 22:51:57 +08:00
|
|
|
lineView.backgroundColor = [UIColor redColor];
|
|
|
|
[polygonView addSubview:lineView];
|
2015-02-27 18:41:44 +08:00
|
|
|
|
2014-12-22 22:51:57 +08:00
|
|
|
self.scanReader.cameraOverlayView = polygonView;
|
|
|
|
}
|
2014-09-12 01:45:17 +08:00
|
|
|
|
2016-04-05 02:08:01 +08:00
|
|
|
[self.viewController presentViewController:self.scanReader animated:YES completion:nil];
|
2014-09-12 01:45:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-29 09:12:02 +08:00
|
|
|
- (void)toggleflash {
|
2015-09-15 06:50:42 +08:00
|
|
|
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
|
2016-07-29 09:12:02 +08:00
|
|
|
|
2015-09-15 06:50:42 +08:00
|
|
|
[device lockForConfiguration:nil];
|
|
|
|
if (device.torchAvailable == 1) {
|
|
|
|
if (device.torchMode == 0) {
|
|
|
|
[device setTorchMode:AVCaptureTorchModeOn];
|
|
|
|
[device setFlashMode:AVCaptureFlashModeOn];
|
2016-07-29 09:12:02 +08:00
|
|
|
} else {
|
2015-09-15 06:50:42 +08:00
|
|
|
[device setTorchMode:AVCaptureTorchModeOff];
|
|
|
|
[device setFlashMode:AVCaptureFlashModeOff];
|
|
|
|
}
|
|
|
|
}
|
2016-07-29 09:12:02 +08:00
|
|
|
|
|
|
|
[device unlockForConfiguration];
|
2015-09-15 06:50:42 +08:00
|
|
|
}
|
2014-09-12 01:45:17 +08:00
|
|
|
|
|
|
|
#pragma mark - Helpers
|
|
|
|
|
2016-07-29 09:12:02 +08:00
|
|
|
- (void)sendScanResult: (CDVPluginResult*)result {
|
2014-09-12 01:45:17 +08:00
|
|
|
[self.commandDelegate sendPluginResult: result callbackId: self.scanCallbackId];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - ZBarReaderDelegate
|
|
|
|
|
2015-09-15 06:50:42 +08:00
|
|
|
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-29 09:12:02 +08:00
|
|
|
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
|
|
|
|
if ([self.scanReader isBeingDismissed]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-12 01:45:17 +08:00
|
|
|
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
|
2016-07-29 09:12:02 +08:00
|
|
|
|
2014-09-12 01:45:17 +08:00
|
|
|
ZBarSymbol *symbol = nil;
|
2016-07-29 09:12:02 +08:00
|
|
|
for (symbol in results) break; // get the first result
|
2014-09-12 01:45:17 +08:00
|
|
|
|
2016-02-17 08:00:47 +08:00
|
|
|
[self.scanReader dismissViewControllerAnimated: YES completion: ^(void) {
|
|
|
|
self.scanInProgress = NO;
|
|
|
|
[self sendScanResult: [CDVPluginResult
|
|
|
|
resultWithStatus: CDVCommandStatus_OK
|
|
|
|
messageAsString: symbol.data]];
|
|
|
|
}];
|
2014-09-12 01:45:17 +08:00
|
|
|
}
|
|
|
|
|
2016-07-29 09:12:02 +08:00
|
|
|
- (void) imagePickerControllerDidCancel:(UIImagePickerController*)picker {
|
2016-02-17 08:00:47 +08:00
|
|
|
[self.scanReader dismissViewControllerAnimated: YES completion: ^(void) {
|
|
|
|
self.scanInProgress = NO;
|
|
|
|
[self sendScanResult: [CDVPluginResult
|
|
|
|
resultWithStatus: CDVCommandStatus_ERROR
|
|
|
|
messageAsString: @"cancelled"]];
|
|
|
|
}];
|
2014-09-12 01:45:17 +08:00
|
|
|
}
|
|
|
|
|
2016-07-29 09:12:02 +08:00
|
|
|
- (void) readerControllerDidFailToRead:(ZBarReaderController*)reader withRetry:(BOOL)retry {
|
2016-02-17 08:00:47 +08:00
|
|
|
[self.scanReader dismissViewControllerAnimated: YES completion: ^(void) {
|
|
|
|
self.scanInProgress = NO;
|
|
|
|
[self sendScanResult: [CDVPluginResult
|
|
|
|
resultWithStatus: CDVCommandStatus_ERROR
|
|
|
|
messageAsString: @"Failed"]];
|
|
|
|
}];
|
2014-09-12 01:45:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|