cordova-plugin-cszbar-jsg/ios/CsZBar.m

182 lines
7.0 KiB
Mathematica
Raw Normal View History

#import "CsZBar.h"
#import <AVFoundation/AVFoundation.h>
#import "AlmaZBarReaderViewController.h"
#pragma mark - State
@interface CsZBar ()
@property bool scanInProgress;
@property NSString *scanCallbackId;
@property AlmaZBarReaderViewController *scanReader;
@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 {
self.scanInProgress = NO;
}
2016-07-29 09:12:02 +08:00
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
return;
}
2016-07-29 09:12:02 +08:00
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#pragma mark - Plugin API
2016-07-29 09:12:02 +08:00
- (void)scan: (CDVInvokedUrlCommand*)command;
{
2016-07-29 09:12:02 +08:00
if (self.scanInProgress) {
[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];
self.scanReader = [AlmaZBarReaderViewController new];
self.scanReader.readerDelegate = self;
self.scanReader.supportedOrientationsMask = ZBarOrientationMask(UIInterfaceOrientationPortrait);
2014-12-06 16:38:00 +08:00
// Get user parameters
NSDictionary *params = (NSDictionary*) [command argumentAtIndex:0];
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;
}
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
NSString *flash = [params objectForKey:@"flash"];
2016-07-29 09:12:02 +08:00
if ([flash isEqualToString:@"on"]) {
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
2016-07-29 09:12:02 +08:00
} else if ([flash isEqualToString:@"off"]) {
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:38:00 +08:00
// Hack to hide the bottom bar's Info button... originally based on http://stackoverflow.com/a/16353530
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];
[infoButton setHidden:YES];
//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;
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;
UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( (screenWidth/2) - (dim/2), (screenHeight/2) - (dim/2), dim, dim)];
2016-07-29 09:12:02 +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];
2014-12-22 22:51:57 +08:00
self.scanReader.cameraOverlayView = polygonView;
}
2016-04-05 02:08:01 +08:00
[self.viewController presentViewController:self.scanReader animated:YES completion:nil];
}
}
2016-07-29 09:12:02 +08:00
- (void)toggleflash {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
2016-07-29 09:12:02 +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 {
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
}
}
2016-07-29 09:12:02 +08:00
[device unlockForConfiguration];
}
#pragma mark - Helpers
2016-07-29 09:12:02 +08:00
- (void)sendScanResult: (CDVPluginResult*)result {
[self.commandDelegate sendPluginResult: result callbackId: self.scanCallbackId];
}
#pragma mark - ZBarReaderDelegate
- (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;
}
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
2016-07-29 09:12:02 +08:00
ZBarSymbol *symbol = nil;
2016-07-29 09:12:02 +08:00
for (symbol in results) break; // get the first result
[self.scanReader dismissViewControllerAnimated: YES completion: ^(void) {
self.scanInProgress = NO;
[self sendScanResult: [CDVPluginResult
resultWithStatus: CDVCommandStatus_OK
messageAsString: symbol.data]];
}];
}
2016-07-29 09:12:02 +08:00
- (void) imagePickerControllerDidCancel:(UIImagePickerController*)picker {
[self.scanReader dismissViewControllerAnimated: YES completion: ^(void) {
self.scanInProgress = NO;
[self sendScanResult: [CDVPluginResult
resultWithStatus: CDVCommandStatus_ERROR
messageAsString: @"cancelled"]];
}];
}
2016-07-29 09:12:02 +08:00
- (void) readerControllerDidFailToRead:(ZBarReaderController*)reader withRetry:(BOOL)retry {
[self.scanReader dismissViewControllerAnimated: YES completion: ^(void) {
self.scanInProgress = NO;
[self sendScanResult: [CDVPluginResult
resultWithStatus: CDVCommandStatus_ERROR
messageAsString: @"Failed"]];
}];
}
@end