scan archive UI improvements, phase 1
[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 withResult:result];
56 }
57
58 - (void)failedToDecodeImage:(NSString *)reason {
59   [self.delegate decoder:self failedToDecodeImage:self.image reason:reason];
60 }
61
62 - (void) prepareSubset {
63   CGImageRef cgImage = self.image.CGImage;
64   CGSize size = CGSizeMake(CGImageGetWidth(cgImage), CGImageGetHeight(cgImage));
65   NSLog(@"decoding: image is (%.1f x %.1f)", size.width, size.height);
66   float scale = min(1.0f, max(0.25f, (float)max(400.0f / size.width, 400.0f / size.height)));
67   subsetWidth = size.width * scale;
68   subsetHeight = size.height * scale;
69   
70   subsetBytesPerRow = ((subsetWidth + 0xf) >> 4) << 4;
71   NSLog(@"decoding: image to decode is (%d x %d) (%d bytes/row)", subsetWidth, subsetHeight, subsetBytesPerRow);
72   
73   subsetData = (unsigned char *)malloc(subsetBytesPerRow * subsetHeight);
74   NSLog(@"allocated %d bytes of memory", subsetBytesPerRow * subsetHeight);
75   
76   CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
77   
78   CGContextRef ctx = 
79   CGBitmapContextCreate(subsetData, subsetWidth, subsetHeight, 
80                         8, subsetBytesPerRow, grayColorSpace, 
81                         kCGImageAlphaNone);
82   CGColorSpaceRelease(grayColorSpace);
83   CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
84   CGContextSetAllowsAntialiasing(ctx, false);
85   
86   NSLog(@"created %dx%d bitmap context", subsetWidth, subsetHeight);
87   CGRect rect = CGRectMake(0, 0, subsetWidth, subsetHeight);
88   
89   CGContextDrawImage(ctx, rect, cgImage);
90   NSLog(@"drew image into %d(%d)x%d  bitmap context", subsetWidth, subsetBytesPerRow, subsetHeight);
91   CGContextFlush(ctx);
92   NSLog(@"flushed context");
93     
94   CGImageRef subsetImageRef = CGBitmapContextCreateImage(ctx);
95   NSLog(@"created CGImage from context");
96         
97   self.subsetImage = [UIImage imageWithCGImage:subsetImageRef];
98   CGImageRelease(subsetImageRef);
99   
100   CGContextRelease(ctx);
101   
102   NSLog(@"released context");  
103 }  
104
105 - (void)decode:(id)arg {
106   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
107   { 
108     QRCodeReader reader;
109     
110     Ref<MonochromeBitmapSource> grayImage
111     (new GrayBytesMonochromeBitmapSource(subsetData, subsetWidth, subsetHeight, subsetBytesPerRow));
112     NSLog(@"grayImage count = %d", grayImage->count());
113     
114     NSLog(@"created GrayBytesMonochromeBitmapSource", subsetWidth, subsetHeight);
115     
116     NSLog(@"created QRCoreReader");
117     
118     TwoDDecoderResult *decoderResult = nil;
119     
120 #ifdef TRY_ROTATIONS
121     for (int i = 0; !decoderResult && i < 4; i++) {
122 #endif
123
124     try {
125       NSLog(@"decoding gray image");
126       Ref<Result> result(reader.decode(grayImage));
127       NSLog(@"gray image decoed");
128       
129       Ref<String> resultText(result->getText());
130       const char *cString = resultText->getText().c_str();
131       ArrayRef<Ref<ResultPoint> > resultPoints = result->getResultPoints();
132       NSMutableArray *points = 
133         [NSMutableArray arrayWithCapacity:resultPoints->size()];
134       
135       for (size_t i = 0; i < resultPoints->size(); i++) {
136         Ref<ResultPoint> rp(resultPoints[i]);
137         CGPoint p = CGPointMake(rp->getX(), rp->getY());
138         [points addObject:[NSValue valueWithCGPoint:p]];
139       }
140       
141       NSString *resultString = [NSString stringWithCString:cString
142                                         encoding:NSUTF8StringEncoding];
143       
144       decoderResult = [TwoDDecoderResult resultWithText:resultString
145                                              points:points];
146     } catch (ReaderException *rex) {
147       NSLog(@"failed to decode, caught ReaderException '%s'",
148             rex->what());
149       delete rex;
150     } catch (IllegalArgumentException *iex) {
151       NSLog(@"failed to decode, caught IllegalArgumentException '%s'", 
152             iex->what());
153       delete iex;
154     } catch (...) {
155       NSLog(@"Caught unknown exception, trying again");
156     }
157
158 #ifdef TRY_ROTATIONS
159       if (!decoderResult) {
160         NSLog(@"rotating gray image");
161         grayImage = grayImage->rotateCounterClockwise();
162         NSLog(@"gray image rotated");
163       }
164     }
165 #endif
166     
167     if (decoderResult) {
168       [self performSelectorOnMainThread:@selector(didDecodeImage:)
169                              withObject:decoderResult
170                           waitUntilDone:NO];
171     } else {
172       [self performSelectorOnMainThread:@selector(failedToDecodeImage:)
173                              withObject:NSLocalizedString(@"No barcode detected.", @"No barcode detected.")
174                           waitUntilDone:NO];
175     }
176
177     free(subsetData);
178     self.subsetData = NULL;
179   }
180   [pool release];
181   NSLog(@"finished decoding.");
182   
183   // if this is not the main thread, then we end it
184   if (![NSThread isMainThread]) {
185     [NSThread exit];
186   }
187 }
188
189 - (void) decodeImage:(UIImage *)i {
190   CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
191   [self decodeImage:i cropRectangle:rect];
192 }
193
194 - (void) decodeImage:(UIImage *)i cropRectangle:(CGRect)cropRect {
195         self.image = i;
196         [self.delegate decoder:self willDecodeImage:i];
197   
198   [self prepareSubset];
199   
200   [self performSelectorOnMainThread:@selector(progressDecodingImage:)
201                          withObject:@"Decoding ..."
202                       waitUntilDone:NO];  
203   
204         [NSThread detachNewThreadSelector:@selector(decode:) 
205                            toTarget:self 
206                          withObject:nil];
207 }
208
209 - (void) dealloc {
210         [image release];
211   [subsetImage release];
212   if (subsetData) free(subsetData);
213         [super dealloc];
214 }
215
216 @end