2 // DecoderViewController.m
5 // Created by Christian Brunschen on 22/05/2008.
7 * Copyright 2008 ZXing authors
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
22 #import "DecoderViewController.h"
24 #import "NSString+HTML.h"
25 #import "ResultParser.h"
26 #import "ParsedResult.h"
27 #import "ResultAction.h"
30 #import "ArchiveController.h"
31 #import "MessageViewController.h"
33 #import "TwoDDecoderResult.h"
35 // Michael Jurewitz, Dec 16, 2009 6:32 PM writes:
36 // https://devforums.apple.com/message/149553
37 // Notice Regarding UIGetScreenImage()
38 // After carefully considering the issue, Apple is now allowing applications to
39 // use the function UIGetScreenImage() to programmatically capture the current
41 // Note that a future release of iPhone OS may provide a public API equivalent
42 // of this functionality. At such time, all applications using
43 // UIGetScreenImage() will be required to adopt the public API.
44 CGImageRef MyCGImageCopyScreenContents(void) {
45 extern CGImageRef UIGetScreenImage(void);
46 return UIGetScreenImage(); /* already retained */
49 @interface DecoderViewController()
50 - (void)takeScreenshot;
53 @implementation DecoderViewController
55 @synthesize cameraBarItem;
56 @synthesize libraryBarItem;
57 @synthesize savedPhotosBarItem;
58 @synthesize archiveBarItem;
59 @synthesize actionBarItem;
61 @synthesize messageView;
62 @synthesize messageTextView;
63 @synthesize messageHelpButton;
64 @synthesize imageView;
72 @synthesize resultPointViews;
74 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
75 if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
76 // Initialization code
78 NSLocalizedString(@"DecoderViewController AppTitle", @"Barcode Scanner");
80 Decoder *d = [[Decoder alloc] init];
84 resultPointViews = [[NSMutableArray alloc] init];
89 - (void) messageReady:(id)sender {
90 MessageViewController *messageController = sender;
91 [[self navigationController] pushViewController:messageController animated:true];
92 [messageController release];
95 - (void) messageFailed:(id)sender {
96 MessageViewController *messageController = sender;
97 NSLog(@"Failed to load message!");
98 [messageController release];
101 - (void) showHints:(id)sender {
102 NSLog(@"Showing Hints!");
104 MessageViewController *hintsController =
105 [[MessageViewController alloc] initWithMessageFilename:@"Hints"
107 onSuccess:@selector(messageReady:)
108 onFailure:@selector(messageFailed:)];
109 hintsController.title =
110 NSLocalizedString(@"DecoderViewController Hints MessageViewController title", @"Hints");
111 [hintsController view];
114 - (void) showAbout:(id)sender {
115 NSLog(@"Showing About!");
117 MessageViewController *aboutController =
118 [[MessageViewController alloc] initWithMessageFilename:@"About"
120 onSuccess:@selector(messageReady:)
121 onFailure:@selector(messageFailed:)];
122 aboutController.title =
123 NSLocalizedString(@"DecoderViewController About MessageViewController title", @"About");
124 [aboutController view];
128 #define HELP_BUTTON_WIDTH (44.0)
129 #define HELP_BUTTON_HEIGHT (55.0)
132 #define FONT_NAME @"TimesNewRomanPSMT"
133 #define FONT_SIZE 16.0
137 [self clearImageView];
138 [self updateToolbar];
139 [self showMessage:NSLocalizedString(@"DecoderViewController take or choose picture",
140 @"Please take or choose a picture containing a barcode") helpButton:YES];
143 // Implement loadView if you want to create a view hierarchy programmatically
147 CGRect messageViewFrame = imageView.frame;
148 UIView *mView = [[UIView alloc] initWithFrame:messageViewFrame];
149 mView.backgroundColor = [UIColor darkGrayColor];
151 mView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
152 UIViewAutoresizingFlexibleWidth |
153 UIViewAutoresizingFlexibleTopMargin;
155 UITextView *mTextView = [[UITextView alloc] initWithFrame:messageViewFrame];
156 mTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
157 UIViewAutoresizingFlexibleWidth;
158 mTextView.editable = false;
159 mTextView.scrollEnabled = true;
160 mTextView.font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE];
161 mTextView.textColor = [UIColor whiteColor];
162 mTextView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.0];
163 mTextView.textAlignment = UITextAlignmentLeft;
164 mTextView.alpha = 1.0;
165 [mView addSubview:mTextView];
167 UIButton *mHelpButton = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
168 mHelpButton.frame = CGRectMake(messageViewFrame.size.width - HELP_BUTTON_WIDTH, 0.0, HELP_BUTTON_WIDTH, HELP_BUTTON_HEIGHT);
170 mHelpButton.backgroundColor = [UIColor clearColor];
171 [mHelpButton setUserInteractionEnabled:YES];
172 [mHelpButton addTarget:self action:@selector(showHints:) forControlEvents:UIControlEventTouchUpInside];
174 self.messageHelpButton = mHelpButton;
175 [mHelpButton release];
177 self.messageTextView = mTextView;
180 self.messageView = mView;
183 [self.view addSubview:self.messageView];
185 // add the 'About' button at the top-right of the navigation bar
186 UIBarButtonItem *aboutButton =
187 [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DecoderViewController about button title", @"About")
188 style:UIBarButtonItemStyleBordered
190 action:@selector(showAbout:)];
191 self.navigationItem.rightBarButtonItem = aboutButton;
192 [aboutButton release];
197 - (void) updateToolbar {
198 self.cameraBarItem.enabled = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
199 self.savedPhotosBarItem.enabled = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
200 self.libraryBarItem.enabled = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
201 self.archiveBarItem.enabled = true;
202 self.actionBarItem.enabled = (self.result != nil) && ([self.result actions] != nil) && ([self.result actions].count > 0);
206 // If you need to do additional setup after loading the view, override viewDidLoad.
207 - (void)viewDidLoad {
212 - (void)clearImageView {
213 imageView.image = nil;
214 for (UIView *view in resultPointViews) {
215 [view removeFromSuperview];
217 [resultPointViews removeAllObjects];
220 - (void)pickAndDecodeFromSource:(UIImagePickerControllerSourceType) sourceType {
223 // Create the Image Picker
224 if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
225 UIImagePickerController* aPicker =
226 [[[UIImagePickerController alloc] init] autorelease];
227 aPicker.sourceType = sourceType;
228 aPicker.delegate = self;
229 self.picker = aPicker;
231 // [[NSUserDefaults standardUserDefaults] boolForKey:@"allowEditing"];
232 BOOL isCamera = (sourceType == UIImagePickerControllerSourceTypeCamera);
233 picker.allowsEditing = !isCamera;
235 picker.showsCameraControls = NO;
236 UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
237 NSString *cancelString =
238 NSLocalizedString(@"DecoderViewController cancel button title", @"");
239 CGFloat height = [UIFont systemFontSize];
240 CGSize size = [cancelString sizeWithFont:[UIFont systemFontOfSize:height]];
241 [cancelButton setTitle:cancelString forState:UIControlStateNormal];
242 CGRect appFrame = [[UIScreen mainScreen] bounds];
243 static const int kMargin = 10;
244 static const int kInternalXMargin = 10;
245 static const int kInternalYMargin = 10;
246 CGRect frame = CGRectMake(kMargin,
247 appFrame.size.height - (height + 2*kInternalYMargin + kMargin),
248 2*kInternalXMargin + size.width,
249 height + 2*kInternalYMargin);
250 [cancelButton setFrame:frame];
251 [cancelButton addTarget:self
252 action:@selector(cancel:)
253 forControlEvents:UIControlEventTouchUpInside];
254 picker.cameraOverlayView = cancelButton;
255 // The camera takes quite a while to start up. Hence the 2 second delay.
256 [self performSelector:@selector(takeScreenshot)
261 // Picker is displayed asynchronously.
262 [self presentModalViewController:picker animated:YES];
264 NSLog(@"Attempted to pick an image with illegal source type '%d'", sourceType);
268 - (IBAction)pickAndDecode:(id) sender {
269 UIImagePickerControllerSourceType sourceType;
270 int i = [sender tag];
273 case 0: sourceType = UIImagePickerControllerSourceTypeCamera; break;
274 case 1: sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; break;
275 case 2: sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break;
276 default: sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
278 [self pickAndDecodeFromSource:sourceType];
282 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
283 // Return YES for supported orientations
284 return (interfaceOrientation == UIInterfaceOrientationPortrait);
288 - (void)didReceiveMemoryWarning {
289 [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
290 // Release anything that's not essential, such as cached data
295 [self clearImageView];
297 [actionBarItem release];
298 [cameraBarItem release];
299 [libraryBarItem release];
300 [savedPhotosBarItem release];
301 [archiveBarItem release];
305 [resultPointViews release];
310 - (void)showMessage:(NSString *)message helpButton:(BOOL)showHelpButton {
312 NSLog(@"Showing message '%@' %@ help Button", message, showHelpButton ? @"with" : @"without");
315 CGSize imageMaxSize = imageView.bounds.size;
316 if (showHelpButton) {
317 imageMaxSize.width -= messageHelpButton.frame.size.width;
319 CGSize size = [message sizeWithFont:messageTextView.font constrainedToSize:imageMaxSize lineBreakMode:UILineBreakModeWordWrap];
320 float height = 20.0 + fmin(100.0, size.height);
321 if (showHelpButton) {
322 height = fmax(HELP_BUTTON_HEIGHT, height);
325 CGRect messageFrame = imageView.bounds;
326 messageFrame.origin.y = CGRectGetMaxY(messageFrame) - height;
327 messageFrame.size.height = height;
328 [self.messageView setFrame:messageFrame];
329 messageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
330 CGRect messageViewBounds = [messageView bounds];
332 self.messageTextView.text = message;
333 messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
334 if (showHelpButton) {
335 CGRect textViewFrame;
336 CGRect helpButtonFrame;
338 CGRectDivide(messageViewBounds, &helpButtonFrame, &textViewFrame, HELP_BUTTON_WIDTH, CGRectMaxXEdge);
339 [self.messageTextView setFrame:textViewFrame];
341 [messageHelpButton setFrame:helpButtonFrame];
342 messageHelpButton.alpha = 1.0;
343 messageHelpButton.enabled = YES;
344 messageHelpButton.autoresizingMask =
345 UIViewAutoresizingFlexibleLeftMargin |
346 UIViewAutoresizingFlexibleTopMargin;
347 [messageView addSubview:messageHelpButton];
349 [messageHelpButton removeFromSuperview];
350 messageHelpButton.alpha = 0.0;
351 messageHelpButton.enabled = NO;
353 [self.messageTextView setFrame:messageViewBounds];
357 // DecoderDelegate methods
359 - (void)decoder:(Decoder *)decoder willDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset{
360 [self clearImageView];
361 [self.imageView setImage:subset];
362 [self showMessage:[NSString stringWithFormat:NSLocalizedString(@"DecoderViewController MessageWhileDecodingWithDimensions", @"Decoding image (%.0fx%.0f) ..."), image.size.width, image.size.height]
366 - (void)decoder:(Decoder *)decoder
367 decodingImage:(UIImage *)image
368 usingSubset:(UIImage *)subset
369 progress:(NSString *)message {
370 [self clearImageView];
371 [self.imageView setImage:subset];
372 [self showMessage:message helpButton:NO];
375 - (void)presentResultForString:(NSString *)resultString {
376 self.result = [ResultParser parsedResultForString:resultString];
377 [self showMessage:[self.result stringForDisplay] helpButton:NO];
378 self.actions = self.result.actions;
380 NSLog(@"result has %d actions", actions ? 0 : actions.count);
382 [self updateToolbar];
385 - (void)presentResultPoints:(NSArray *)resultPoints
386 forImage:(UIImage *)image
387 usingSubset:(UIImage *)subset {
388 // simply add the points to the image view
389 imageView.image = subset;
390 for (NSValue *pointValue in resultPoints) {
391 [imageView addResultPoint:[pointValue CGPointValue]];
395 - (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)twoDResult {
397 [self presentResultForString:twoDResult.text];
399 [self presentResultPoints:twoDResult.points forImage:image usingSubset:subset];
401 // save the scan to the shared database
402 [[Database sharedDatabase] addScanWithText:twoDResult.text];
404 [self performResultAction:self];
407 - (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason {
408 if (self.picker && UIImagePickerControllerSourceTypeCamera == self.picker.sourceType) {
409 // If we are using the camera, and the user hasn't manually cancelled,
410 // take another snapshot and try to decode it.
411 [self takeScreenshot];
413 [self showMessage:reason helpButton:YES];
414 [self updateToolbar];
419 - (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
420 [super willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
422 if (imageView.image) {
424 CGRect viewBounds = imageView.bounds;
425 CGSize imageSize = imageView.image.size;
426 float scale = fmin(viewBounds.size.width / imageSize.width,
427 viewBounds.size.height / imageSize.height);
428 float xOffset = (viewBounds.size.width - scale * imageSize.width) / 2.0;
429 float yOffset = (viewBounds.size.height - scale * imageSize.height) / 2.0;
432 for (UIView *view in resultPointViews) {
438 - (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
439 [super willAnimateSecondHalfOfRotationFromInterfaceOrientation:fromInterfaceOrientation duration:duration];
441 if (imageView.image) {
443 CGRect viewBounds = imageView.bounds;
444 CGSize imageSize = imageView.image.size;
445 float scale = fmin(viewBounds.size.width / imageSize.width,
446 viewBounds.size.height / imageSize.height);
447 float xOffset = (viewBounds.size.width - scale * imageSize.width) / 2.0;
448 float yOffset = (viewBounds.size.height - scale * imageSize.height) / 2.0;
451 for (UIView *view in resultPointViews) {
457 - (void)cancel:(id)sender {
461 - (void)takeScreenshot {
463 CGImageRef cgScreen = MyCGImageCopyScreenContents();
465 CGRect croppedFrame = CGRectMake(0, 0, CGImageGetWidth(cgScreen),
466 CGImageGetHeight(cgScreen) - (10+toolbar.bounds.size.height));
467 CGImageRef cgCropped = CGImageCreateWithImageInRect(cgScreen, croppedFrame);
469 UIImage *screenshot = [UIImage imageWithCGImage:cgCropped];
470 CGImageRelease(cgCropped);
471 [self.decoder decodeImage:screenshot];
473 CGImageRelease(cgScreen);
478 // UIImagePickerControllerDelegate methods
480 - (void)imagePickerController:(UIImagePickerController *)aPicker
481 didFinishPickingMediaWithInfo:(NSDictionary *)info {
482 UIImage *imageToDecode =
483 [info objectForKey:UIImagePickerControllerEditedImage];
484 if (!imageToDecode) {
485 imageToDecode = [info objectForKey:UIImagePickerControllerOriginalImage];
487 CGSize size = [imageToDecode size];
488 CGRect cropRect = CGRectMake(0.0, 0.0, size.width, size.height);
491 NSLog(@"picked image size = (%f, %f)", size.width, size.height);
493 NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
495 NSValue *cropRectValue = [info objectForKey:UIImagePickerControllerCropRect];
497 UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
500 NSLog(@"original image size = (%f, %f)", originalImage.size.width, originalImage.size.height);
502 cropRect = [cropRectValue CGRectValue];
504 NSLog(@"crop rect = (%f, %f) x (%f, %f)", CGRectGetMinX(cropRect), CGRectGetMinY(cropRect), CGRectGetWidth(cropRect), CGRectGetHeight(cropRect));
506 if (([picker sourceType] == UIImagePickerControllerSourceTypeSavedPhotosAlbum) &&
507 [@"2.1" isEqualToString:systemVersion]) {
508 // adjust crop rect to work around bug in iPhone OS 2.1 when selecting from the photo roll
509 cropRect.origin.x *= 2.5;
510 cropRect.origin.y *= 2.5;
511 cropRect.size.width *= 2.5;
512 cropRect.size.height *= 2.5;
514 NSLog(@"2.1-adjusted crop rect = (%f, %f) x (%f, %f)", CGRectGetMinX(cropRect), CGRectGetMinY(cropRect), CGRectGetWidth(cropRect), CGRectGetHeight(cropRect));
518 imageToDecode = originalImage;
522 [imageToDecode retain];
524 [self.decoder decodeImage:imageToDecode cropRect:cropRect];
525 [imageToDecode release];
529 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)aPicker {
533 - (void)setPicker:(UIImagePickerController *)aPicker {
534 if (picker != aPicker) {
535 [picker dismissModalViewControllerAnimated:YES];
536 picker = [aPicker retain];
537 [self updateToolbar];
541 - (void)navigationController:(UINavigationController *)navigationController
542 didShowViewController:(UIViewController *)viewController
543 animated:(BOOL)animated {
547 - (void)navigationController:(UINavigationController *)navigationController
548 willShowViewController:(UIViewController *)viewController
549 animated:(BOOL)animated {
553 - (void)performAction:(ResultAction *)action {
554 [action performActionWithController:self shouldConfirm:NO];
557 - (void)confirmAndPerformAction:(ResultAction *)action {
558 [action performActionWithController:self shouldConfirm:YES];
562 - (IBAction)performResultAction:(id)sender {
563 if (self.result == nil) {
564 NSLog(@"no result to perform an action on!");
568 if (self.actions == nil || self.actions.count == 0) {
569 NSLog(@"result has no actions to perform!");
573 if (self.actions.count == 1) {
574 ResultAction *action = [self.actions lastObject];
576 NSLog(@"Result has the single action, (%@) '%@', performing it",
577 NSStringFromClass([action class]), [action title]);
579 [self performSelector:@selector(confirmAndPerformAction:)
584 NSLog(@"Result has multiple actions, popping up an action sheet");
586 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithFrame:self.view.bounds];
588 for (ResultAction *action in self.actions) {
589 [actionSheet addButtonWithTitle:[action title]];
592 int cancelIndex = [actionSheet addButtonWithTitle:NSLocalizedString(@"DecoderViewController cancel button title", @"Cancel")];
593 actionSheet.cancelButtonIndex = cancelIndex;
595 actionSheet.delegate = self;
597 [actionSheet showFromToolbar:self.toolbar];
601 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
602 if (buttonIndex < self.actions.count) {
603 int actionIndex = buttonIndex;
604 ResultAction *action = [self.actions objectAtIndex:actionIndex];
605 [self performSelector:@selector(performAction:)
611 - (IBAction)showArchive:(id)sender {
612 ArchiveController *archiveController = [[ArchiveController alloc] initWithDecoderViewController:self];
613 [[self navigationController] pushViewController:archiveController animated:true];
614 [archiveController release];
617 - (void)showScan:(Scan *)scan {
618 [self clearImageView];
619 [self presentResultForString:scan.text];
620 [[self navigationController] popToViewController:self animated:YES];