UI improvements
[zxing.git] / iphone / Classes / Decoder.m
1 //
2 //  Decoder.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 31/03/2008.
6 //
7 /*
8  * Copyright 2008 ZXing authors
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22
23 #import "Decoder.h"
24 #import "TwoDDecoderResult.h"
25
26 #include "QRCodeReader.h"
27 #include "ReaderException.h"
28 #include "IllegalArgumentException.h"
29 #include "GrayBytesMonochromeBitmapSource.h"
30
31 using namespace qrcode;
32
33 @implementation Decoder
34
35 @synthesize image;
36 @synthesize subsetImage;
37 @synthesize subsetData;
38 @synthesize subsetWidth;
39 @synthesize subsetHeight;
40 @synthesize subsetBytesPerRow;
41 @synthesize delegate;
42
43 - (void)willDecodeImage {
44   [self.delegate decoder:self willDecodeImage:self.image];
45 }
46
47 - (void)progressDecodingImage:(NSString *)progress {
48   [self.delegate decoder:self 
49           decodingImage:self.image 
50             usingSubset:self.subsetImage
51                progress:progress];
52 }
53
54 - (void)didDecodeImage:(TwoDDecoderResult *)result {
55   [self.delegate decoder:self didDecodeImage:self.image usingSubset:self.subsetImage withResult:result];
56 }
57
58 - (void)failedToDecodeImage:(NSString *)reason {
59   [self.delegate decoder:self failedToDecodeImage:self.image usingSubset:self.subsetImage reason:reason];
60 }
61
62 #define SUBSET_SIZE 320.0
63 - (void) prepareSubset {
64   CGImageRef cgImage = self.image.CGImage;
65   CGSize size = CGSizeMake(CGImageGetWidth(cgImage), CGImageGetHeight(cgImage));
66 #ifdef DEBUG
67   NSLog(@"decoding: image is (%.1f x %.1f)", size.width, size.height);
68 #endif
69   float scale = fminf(1.0f, fmaxf(SUBSET_SIZE / size.width, SUBSET_SIZE / size.height));
70   subsetWidth = size.width * scale;
71   subsetHeight = size.height * scale;
72   
73   subsetBytesPerRow = ((subsetWidth + 0xf) >> 4) << 4;
74 #ifdef DEBUG
75   NSLog(@"decoding: image to decode is (%d x %d) (%d bytes/row)", subsetWidth, subsetHeight, subsetBytesPerRow);
76 #endif
77   
78   subsetData = (unsigned char *)malloc(subsetBytesPerRow * subsetHeight);
79 #ifdef DEBUG
80   NSLog(@"allocated %d bytes of memory", subsetBytesPerRow * subsetHeight);
81 #endif
82   
83   CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
84   
85   CGContextRef ctx = 
86   CGBitmapContextCreate(subsetData, subsetWidth, subsetHeight, 
87                         8, subsetBytesPerRow, grayColorSpace, 
88                         kCGImageAlphaNone);
89   CGColorSpaceRelease(grayColorSpace);
90   CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
91   CGContextSetAllowsAntialiasing(ctx, false);
92   
93 #ifdef DEBUG
94   NSLog(@"created %dx%d bitmap context", subsetWidth, subsetHeight);
95 #endif
96   CGRect rect = CGRectMake(0, 0, subsetWidth, subsetHeight);
97   
98   CGContextDrawImage(ctx, rect, cgImage);
99 #ifdef DEBUG
100   NSLog(@"drew image into %d(%d)x%d  bitmap context", subsetWidth, subsetBytesPerRow, subsetHeight);
101 #endif
102   CGContextFlush(ctx);
103 #ifdef DEBUG
104   NSLog(@"flushed context");
105 #endif
106     
107   CGImageRef subsetImageRef = CGBitmapContextCreateImage(ctx);
108 #ifdef DEBUG
109   NSLog(@"created CGImage from context");
110 #endif
111         
112   self.subsetImage = [UIImage imageWithCGImage:subsetImageRef];
113   CGImageRelease(subsetImageRef);
114   
115   CGContextRelease(ctx);
116 #ifdef DEBUG
117   NSLog(@"released context");  
118 #endif
119 }  
120
121 - (void)decode:(id)arg {
122   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
123   { 
124     QRCodeReader reader;
125 #ifdef DEBUG
126     NSLog(@"created QRCoreReader");
127 #endif
128     
129     Ref<MonochromeBitmapSource> grayImage
130     (new GrayBytesMonochromeBitmapSource(subsetData, subsetWidth, subsetHeight, subsetBytesPerRow));
131 #ifdef DEBUG
132     NSLog(@"created GrayBytesMonochromeBitmapSource", subsetWidth, subsetHeight);
133     NSLog(@"grayImage count = %d", grayImage->count());
134 #endif
135     
136     TwoDDecoderResult *decoderResult = nil;
137     
138 #ifdef TRY_ROTATIONS
139     for (int i = 0; !decoderResult && i < 4; i++) {
140 #endif
141
142     try {
143 #ifdef DEBUG
144       NSLog(@"decoding gray image");
145 #endif
146       Ref<Result> result(reader.decode(grayImage));
147 #ifdef DEBUG
148       NSLog(@"gray image decoded");
149 #endif
150       
151       Ref<String> resultText(result->getText());
152       const char *cString = resultText->getText().c_str();
153       ArrayRef<Ref<ResultPoint> > resultPoints = result->getResultPoints();
154       NSMutableArray *points = 
155         [NSMutableArray arrayWithCapacity:resultPoints->size()];
156       
157       for (size_t i = 0; i < resultPoints->size(); i++) {
158         Ref<ResultPoint> rp(resultPoints[i]);
159         CGPoint p = CGPointMake(rp->getX(), rp->getY());
160         [points addObject:[NSValue valueWithCGPoint:p]];
161       }
162       
163       NSString *resultString = [NSString stringWithCString:cString
164                                         encoding:NSUTF8StringEncoding];
165       
166       decoderResult = [TwoDDecoderResult resultWithText:resultString
167                                              points:points];
168     } catch (ReaderException *rex) {
169       NSLog(@"failed to decode, caught ReaderException '%s'",
170             rex->what());
171       delete rex;
172     } catch (IllegalArgumentException *iex) {
173       NSLog(@"failed to decode, caught IllegalArgumentException '%s'", 
174             iex->what());
175       delete iex;
176     } catch (...) {
177       NSLog(@"Caught unknown exception!");
178     }
179
180 #ifdef TRY_ROTATIONS
181       if (!decoderResult) {
182 #ifdef DEBUG
183         NSLog(@"rotating gray image");
184 #endif
185         grayImage = grayImage->rotateCounterClockwise();
186 #ifdef DEBUG
187         NSLog(@"gray image rotated");
188 #endif
189       }
190     }
191 #endif
192     
193     if (decoderResult) {
194       [self performSelectorOnMainThread:@selector(didDecodeImage:)
195                              withObject:decoderResult
196                           waitUntilDone:NO];
197     } else {
198       [self performSelectorOnMainThread:@selector(failedToDecodeImage:)
199                              withObject:NSLocalizedString(@"No barcode detected.", @"No barcode detected.")
200                           waitUntilDone:NO];
201     }
202
203     free(subsetData);
204     self.subsetData = NULL;
205   }
206   [pool release];
207 #ifdef DEBUG
208   NSLog(@"finished decoding.");
209 #endif
210   
211   // if this is not the main thread, then we end it
212   if (![NSThread isMainThread]) {
213     [NSThread exit];
214   }
215 }
216
217 - (void) decodeImage:(UIImage *)i {
218   CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
219   [self decodeImage:i cropRectangle:rect];
220 }
221
222 - (void) decodeImage:(UIImage *)i cropRectangle:(CGRect)cropRect {
223         self.image = i;
224         [self.delegate decoder:self willDecodeImage:i];
225   
226   [self prepareSubset];
227   
228   [self performSelectorOnMainThread:@selector(progressDecodingImage:)
229                          withObject:@"Decoding ..."
230                       waitUntilDone:NO];  
231   
232         [NSThread detachNewThreadSelector:@selector(decode:) 
233                            toTarget:self 
234                          withObject:nil];
235 }
236
237 - (void) dealloc {
238         [image release];
239   [subsetImage release];
240   if (subsetData) free(subsetData);
241         [super dealloc];
242 }
243
244 @end