a scan widget you can include in your iPhone app. Test app coming shortly.
[zxing.git] / iphone / ZXingWidget / ZXingWidgetController.m
1 /**
2  * Copyright 2009 Jeff Verkoeyen
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #import "ZXingWidgetController.h"
18 #import "Decoder.h"
19 #import "NSString+HTML.h"
20 #import "ResultParser.h"
21 #import "ParsedResult.h"
22 #import "ResultAction.h"
23 #include <sys/types.h>
24 #include <sys/sysctl.h>
25
26 #define CAMERA_SCALAR 1.12412 // scalar = (480 / (2048 / 480))
27 #define FIRST_TAKE_DELAY 1.0
28
29 CGImageRef UIGetScreenImage();
30
31 @implementation ZXingWidgetController
32 @synthesize result, actions, showCancel, delegate;
33
34 - (id)initWithDelegate:(id<ZXingDelegate>)scanDelegate {
35         if (self = [super init]) {
36                 [self setDelegate:scanDelegate];
37                 showCancel = true;
38                 self.wantsFullScreenLayout = YES;
39                 self.sourceType = UIImagePickerControllerSourceTypeCamera;
40                 float zoomFactor = CAMERA_SCALAR;
41                 if ([self fixedFocus]) {
42                         zoomFactor *= 1.5;
43                 }
44                 self.cameraViewTransform = CGAffineTransformScale(
45                                         self.cameraViewTransform, zoomFactor, zoomFactor);
46                 overlayView = [[OverlayView alloc] initWithCancelEnabled:showCancel frame:[UIScreen mainScreen].bounds];
47                 [overlayView setDelegate:self];
48                 self.sourceType = UIImagePickerControllerSourceTypeCamera;
49                 self.showsCameraControls = NO;
50                 self.cameraOverlayView = overlayView;
51                 self.allowsEditing = NO; // [[NSUserDefaults standardUserDefaults] boolForKey:@"allowEditing"];
52         }
53         
54         return self;
55 }
56
57 - (void)cancelled {
58         NSLog(@"cancelled called in ZXingWidgetController");
59         wasCancelled = true;
60         if (delegate != nil) {
61                 [delegate cancelled];
62         }
63 }
64
65 - (NSString *)getPlatform {
66         size_t size;
67     sysctlbyname("hw.machine", NULL, &size, NULL, 0);
68     char *machine = malloc(size);
69     sysctlbyname("hw.machine", machine, &size, NULL, 0);
70     NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
71     free(machine);
72         return platform;
73 }
74
75 - (BOOL)fixedFocus {
76         NSString *platform = [self getPlatform];
77         if ([platform isEqualToString:@"iPhone1,1"] ||
78                 [platform isEqualToString:@"iPhone1,2"]) return true;
79         return false;
80 }
81
82 - (void)viewDidAppear:(BOOL)animated {
83     [super viewDidAppear:animated];
84         wasCancelled = false;
85         [NSTimer scheduledTimerWithTimeInterval: FIRST_TAKE_DELAY
86                                                                          target: self
87                                                                    selector: @selector(takePicture:)
88                                                                    userInfo: nil
89                                                                         repeats: NO];
90 }
91
92 - (void)takePicture:(NSTimer*)theTimer {
93         CGImageRef capture = UIGetScreenImage();
94         UIImage *scrn = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(capture, [overlayView cropRect])];
95         Decoder *d = [[Decoder alloc] init];
96         d.delegate = self;
97         CGRect cropRect = overlayView.cropRect;
98         cropRect.origin.x = 0.0;
99         cropRect.origin.y = 0.0;
100         NSLog(@"crop rect %f, %f, %f, %f", cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height);
101         [d decodeImage:scrn cropRect:cropRect];
102 }
103
104 // DecoderDelegate methods
105
106 - (void)decoder:(Decoder *)decoder willDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset{
107         NSLog(@"DecoderViewController MessageWhileDecodingWithDimensions: Decoding image (%.0fx%.0f) ...", image.size.width, image.size.height);
108 }
109
110 - (void)decoder:(Decoder *)decoder
111   decodingImage:(UIImage *)image
112     usingSubset:(UIImage *)subset
113        progress:(NSString *)message {
114         NSLog(@"decoding image %@", message);
115 }
116
117 - (void)presentResultForString:(NSString *)resultString {
118         NSLog(@"in presentResultForString()");
119         self.result = [ResultParser parsedResultForString:resultString];
120         AudioServicesPlaySystemSound(beepSound);
121         //      self.actions = self.result.actions;
122 #ifdef DEBUG
123         NSLog(@"result string = %@", resultString);
124         NSLog(@"result has %d actions", actions ? 0 : actions.count);
125 #endif
126         //      [self updateToolbar];
127 }
128
129 - (void)presentResultPoints:(NSArray *)resultPoints
130                    forImage:(UIImage *)image
131                 usingSubset:(UIImage *)subset {
132         // simply add the points to the image view
133         NSLog(@"got points for display");
134         [overlayView setPoints:resultPoints];
135 }
136
137 - (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)twoDResult {
138         //      [self presentResultForString:twoDResult.text];
139         NSLog(@"decoded image!!");
140         [self presentResultPoints:[twoDResult points] forImage:image usingSubset:subset];
141         if (delegate != nil) {
142                 [delegate scanResult:[twoDResult text]];
143         }
144         decoder.delegate = nil;
145         [decoder release];
146         
147         // save the scan to the shared database
148         //      [[Database sharedDatabase] addScanWithText:twoDResult.text];
149         // need to call delegate....`
150         //      [self performResultAction:self];
151 }
152
153 - (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason {
154         decoder.delegate = nil;
155         [decoder release];
156         [overlayView setPoints:nil];
157         if (!wasCancelled) {
158                 [self takePicture:nil];
159         }
160         //[self updateToolbar];
161 }
162
163 @end