First version of iPhone skeleton app using official iPhone SDK.
[zxing.git] / iphone / ZXing / Classes / DecoderViewController.m
1 /*
2  * Copyright 2008 Google Inc.
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 //
18 //  DecoderViewController.m
19 //  ZXing
20 //
21 //  Created by Christian Brunschen on 31/03/2008.
22 //
23
24 #import "DecoderViewController.h"
25 #import "Decoder.h"
26
27 @implementation DecoderViewController
28
29 @synthesize imageView;
30 @synthesize messageView;
31 @synthesize decoder;
32
33 - (id)init
34 {
35     if (self = [super init]) {
36         // Initialize your view controller.
37         self.title = @"DecoderViewController";
38
39         Decoder *d = [[Decoder alloc] init];
40         self.decoder = d;
41         d.delegate = self;
42         [d release];
43     }
44     return self;
45 }
46
47
48 - (void)loadView
49 {
50     [[NSBundle mainBundle] loadNibNamed:@"DecoderView" owner:self options:nil];
51     
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);
56     rect.origin.y -= 50;
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];
61     mView.alpha = 0.7;
62     [self.view addSubview:mView];
63     self.messageView = mView;
64     [mView release];
65 }
66
67 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
68 {
69     // Return YES for supported orientations.
70     return YES;
71 }
72
73 - (void)didReceiveMemoryWarning
74 {
75     [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview.
76     // Release anything that's not essential, such as cached data.
77 }
78
79 - (void)dealloc
80 {
81     [decoder release];
82     [imageView release];
83     [messageView release];
84     [super dealloc];
85 }
86
87
88 // own methods
89
90 - (void)pickAndDecode {
91     // Create the Image Picker
92     UIImagePickerControllerSourceType sourceType = 
93     [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] 
94     ? UIImagePickerControllerSourceTypeCamera 
95     : UIImagePickerControllerSourceTypePhotoLibrary;
96     
97     UIImagePickerController* picker = [[UIImagePickerController alloc] init];
98     picker.sourceType = sourceType;
99     picker.delegate = self;
100     picker.allowsImageEditing = YES;
101         
102     // Picker is displayed asynchronously.
103     [self presentModalViewController:picker animated:YES];
104 }
105
106
107 // DecoderDelegate methods
108
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];
112 }
113
114 - (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image withResult:(NSString *)result {
115     self.messageView.text = result;
116 }
117
118
119 // UIImagePickerControllerDelegate methods
120
121 - (void)imagePickerController:(UIImagePickerController *)picker
122         didFinishPickingImage:(UIImage *)image
123                   editingInfo:(NSDictionary *)editingInfo
124 {
125     [[picker parentViewController] dismissModalViewControllerAnimated:YES];
126     [image retain];
127     [picker release];
128     [self.decoder decodeImage:image];
129     [image release];
130 }
131
132 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
133 {
134     [picker dismissModalViewControllerAnimated:YES];
135     [picker release];
136 }
137
138
139
140 @end