2 * Copyright 2009 Jeff Verkoeyen
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #import "ZXingWidgetController.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>
26 #define CAMERA_SCALAR 1.12412 // scalar = (480 / (2048 / 480))
27 #define FIRST_TAKE_DELAY 1.0
29 CGImageRef UIGetScreenImage();
31 @implementation ZXingWidgetController
32 @synthesize result, actions, showCancel, delegate;
34 - (id)initWithDelegate:(id<ZXingDelegate>)scanDelegate {
35 if (self = [super init]) {
36 [self setDelegate:scanDelegate];
38 self.wantsFullScreenLayout = YES;
39 self.sourceType = UIImagePickerControllerSourceTypeCamera;
40 float zoomFactor = CAMERA_SCALAR;
41 if ([self fixedFocus]) {
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"];
58 NSLog(@"cancelled called in ZXingWidgetController");
60 if (delegate != nil) {
65 - (NSString *)getPlatform {
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];
76 NSString *platform = [self getPlatform];
77 if ([platform isEqualToString:@"iPhone1,1"] ||
78 [platform isEqualToString:@"iPhone1,2"]) return true;
82 - (void)viewDidAppear:(BOOL)animated {
83 [super viewDidAppear:animated];
84 [overlayView setPoints:nil];
86 [NSTimer scheduledTimerWithTimeInterval: FIRST_TAKE_DELAY
88 selector: @selector(takePicture:)
93 - (void)takePicture:(NSTimer*)theTimer {
94 CGImageRef capture = UIGetScreenImage();
95 UIImage *scrn = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(capture, [overlayView cropRect])];
96 Decoder *d = [[Decoder alloc] init];
98 CGRect cropRect = overlayView.cropRect;
99 cropRect.origin.x = 0.0;
100 cropRect.origin.y = 0.0;
101 NSLog(@"crop rect %f, %f, %f, %f", cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height);
102 [d decodeImage:scrn cropRect:cropRect];
105 // DecoderDelegate methods
107 - (void)decoder:(Decoder *)decoder willDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset{
108 NSLog(@"DecoderViewController MessageWhileDecodingWithDimensions: Decoding image (%.0fx%.0f) ...", image.size.width, image.size.height);
111 - (void)decoder:(Decoder *)decoder
112 decodingImage:(UIImage *)image
113 usingSubset:(UIImage *)subset
114 progress:(NSString *)message {
115 NSLog(@"decoding image %@", message);
118 - (void)presentResultForString:(NSString *)resultString {
119 NSLog(@"in presentResultForString()");
120 self.result = [ResultParser parsedResultForString:resultString];
121 AudioServicesPlaySystemSound(beepSound);
122 // self.actions = self.result.actions;
124 NSLog(@"result string = %@", resultString);
125 NSLog(@"result has %d actions", actions ? 0 : actions.count);
127 // [self updateToolbar];
130 - (void)presentResultPoints:(NSArray *)resultPoints
131 forImage:(UIImage *)image
132 usingSubset:(UIImage *)subset {
133 // simply add the points to the image view
134 NSLog(@"got points for display");
135 [overlayView setPoints:resultPoints];
138 - (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)twoDResult {
139 // [self presentResultForString:twoDResult.text];
140 NSLog(@"decoded image!!");
141 [self presentResultPoints:[twoDResult points] forImage:image usingSubset:subset];
142 // now, in a selector, call the delegate to give this overlay time to show the points
143 [self performSelectorOnMainThread:@selector(alertDelegate:) withObject:[[twoDResult text] copy] waitUntilDone:false];
144 decoder.delegate = nil;
148 - (void)alertDelegate:(id)text {
149 if (delegate != nil) {
150 [delegate scanResult:text];
154 - (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason {
155 decoder.delegate = nil;
157 [overlayView setPoints:nil];
159 [self takePicture:nil];
161 //[self updateToolbar];