2 * Copyright 2008 Google Inc.
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.
18 // DecoderViewController.m
21 // Created by Christian Brunschen on 31/03/2008.
24 #import "DecoderViewController.h"
27 @implementation DecoderViewController
29 @synthesize imageView;
30 @synthesize messageView;
35 if (self = [super init]) {
36 // Initialize your view controller.
37 self.title = @"DecoderViewController";
39 Decoder *d = [[Decoder alloc] init];
50 [[NSBundle mainBundle] loadNibNamed:@"DecoderView" owner:self options:nil];
52 /* programmatically create the message view for now */
53 // TODO(christian.brunschen): find a suitable & working IB configuration
54 CGRect rect = [UIScreen mainScreen].applicationFrame;
55 rect = CGRectInset(rect, CGRectGetWidth(rect)/4, CGRectGetHeight(rect)/3);
57 UITextView *mView = [[UITextView alloc] initWithFrame:rect];
58 mView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
59 mView.text = @"Please take or select a picture containing a barcode";
60 mView.backgroundColor = [UIColor yellowColor];
62 [self.view addSubview:mView];
63 self.messageView = mView;
67 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
69 // Return YES for supported orientations.
73 - (void)didReceiveMemoryWarning
75 [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview.
76 // Release anything that's not essential, such as cached data.
83 [messageView release];
90 - (void)pickAndDecode {
91 // Create the Image Picker
92 UIImagePickerControllerSourceType sourceType =
93 [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]
94 ? UIImagePickerControllerSourceTypeCamera
95 : UIImagePickerControllerSourceTypePhotoLibrary;
97 UIImagePickerController* picker = [[UIImagePickerController alloc] init];
98 picker.sourceType = sourceType;
99 picker.delegate = self;
100 picker.allowsImageEditing = YES;
102 // Picker is displayed asynchronously.
103 [self presentModalViewController:picker animated:YES];
107 // DecoderDelegate methods
109 - (void)decoder:(Decoder *)decoder willDecodeImage:(UIImage *)image {
110 [self.imageView setImage:image];
111 self.messageView.text = [NSString stringWithFormat:@"Decoding image (%.0fx%.0f) ...", image.size.width, image.size.height];
114 - (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image withResult:(NSString *)result {
115 self.messageView.text = result;
119 // UIImagePickerControllerDelegate methods
121 - (void)imagePickerController:(UIImagePickerController *)picker
122 didFinishPickingImage:(UIImage *)image
123 editingInfo:(NSDictionary *)editingInfo
125 [[picker parentViewController] dismissModalViewControllerAnimated:YES];
128 [self.decoder decodeImage:image];
132 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
134 [picker dismissModalViewControllerAnimated:YES];