-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathCsZBar.m
181 lines (142 loc) · 7 KB
/
CsZBar.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#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
- (void)pluginInitialize {
self.scanInProgress = NO;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
return;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#pragma mark - Plugin API
- (void)scan: (CDVInvokedUrlCommand*)command;
{
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);
// Get user parameters
NSDictionary *params = (NSDictionary*) [command argumentAtIndex:0];
NSString *camera = [params objectForKey:@"camera"];
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"];
if ([flash isEqualToString:@"on"]) {
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
} else if ([flash isEqualToString:@"off"]) {
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
}else if ([flash isEqualToString:@"auto"]) {
self.scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
}
// 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];
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;
UIToolbar *toolbarViewFlash = [[UIToolbar alloc] init];
//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)];
NSArray *buttons = [NSArray arrayWithObjects: buttonFlash, nil];
[toolbarViewFlash setItems:buttons animated:NO];
[self.scanReader.view addSubview:toolbarViewFlash];
if (drawSight) {
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)];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,dim / 2, dim, 1)];
lineView.backgroundColor = [UIColor redColor];
[polygonView addSubview:lineView];
self.scanReader.cameraOverlayView = polygonView;
}
[self.viewController presentViewController:self.scanReader animated:YES completion:nil];
}
}
- (void)toggleflash {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
if (device.torchAvailable == 1) {
if (device.torchMode == 0) {
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
} else {
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
}
}
[device unlockForConfiguration];
}
#pragma mark - Helpers
- (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;
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
if ([self.scanReader isBeingDismissed]) {
return;
}
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
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]];
}];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController*)picker {
[self.scanReader dismissViewControllerAnimated: YES completion: ^(void) {
self.scanInProgress = NO;
[self sendScanResult: [CDVPluginResult
resultWithStatus: CDVCommandStatus_ERROR
messageAsString: @"cancelled"]];
}];
}
- (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