5 // Created by Christian Brunschen on 31/03/2008.
8 * Copyright 2008 ZXing authors
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #import "TwoDDecoderResult.h"
26 #include "QRCodeReader.h"
27 #include "ReaderException.h"
28 #include "IllegalArgumentException.h"
29 #include "GrayBytesMonochromeBitmapSource.h"
31 using namespace qrcode;
33 @implementation Decoder
37 @synthesize subsetImage;
38 @synthesize subsetData;
39 @synthesize subsetWidth;
40 @synthesize subsetHeight;
41 @synthesize subsetBytesPerRow;
44 - (void)willDecodeImage {
45 [self.delegate decoder:self willDecodeImage:self.image usingSubset:self.subsetImage];
48 - (void)progressDecodingImage:(NSString *)progress {
49 [self.delegate decoder:self
50 decodingImage:self.image
51 usingSubset:self.subsetImage
55 - (void)didDecodeImage:(TwoDDecoderResult *)result {
56 [self.delegate decoder:self didDecodeImage:self.image usingSubset:self.subsetImage withResult:result];
59 - (void)failedToDecodeImage:(NSString *)reason {
60 [self.delegate decoder:self failedToDecodeImage:self.image usingSubset:self.subsetImage reason:reason];
63 #define SUBSET_SIZE 320.0
64 - (void) prepareSubset {
65 CGSize size = [image size];
67 NSLog(@"decoding: image is (%.1f x %.1f), cropRect is (%.1f,%.1f)x(%.1f,%.1f)", size.width, size.height,
68 cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height);
70 float scale = fminf(1.0f, fmaxf(SUBSET_SIZE / cropRect.size.width, SUBSET_SIZE / cropRect.size.height));
71 CGPoint offset = CGPointMake(-cropRect.origin.x, -cropRect.origin.y);
73 NSLog(@" offset = (%.1f, %.1f), scale = %.3f", offset.x, offset.y, scale);
76 subsetWidth = cropRect.size.width * scale;
77 subsetHeight = cropRect.size.height * scale;
79 subsetBytesPerRow = ((subsetWidth + 0xf) >> 4) << 4;
81 NSLog(@"decoding: image to decode is (%d x %d) (%d bytes/row)", subsetWidth, subsetHeight, subsetBytesPerRow);
84 subsetData = (unsigned char *)malloc(subsetBytesPerRow * subsetHeight);
86 NSLog(@"allocated %d bytes of memory", subsetBytesPerRow * subsetHeight);
89 CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
92 CGBitmapContextCreate(subsetData, subsetWidth, subsetHeight,
93 8, subsetBytesPerRow, grayColorSpace,
95 CGColorSpaceRelease(grayColorSpace);
96 CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
97 CGContextSetAllowsAntialiasing(ctx, false);
98 // adjust the coordinate system
99 CGContextTranslateCTM(ctx, 0.0, subsetHeight);
100 CGContextScaleCTM(ctx, 1.0, -1.0);
103 NSLog(@"created %dx%d bitmap context", subsetWidth, subsetHeight);
106 UIGraphicsPushContext(ctx);
107 CGRect rect = CGRectMake(offset.x * scale, offset.y * scale, scale * size.width, scale * size.height);
109 NSLog(@"rect for image = (%.1f,%.1f)x(%.1f,%.1f)", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
111 [image drawInRect:rect];
112 UIGraphicsPopContext();
115 NSLog(@"drew image into %d(%d)x%d bitmap context", subsetWidth, subsetBytesPerRow, subsetHeight);
119 NSLog(@"flushed context");
122 CGImageRef subsetImageRef = CGBitmapContextCreateImage(ctx);
124 NSLog(@"created CGImage from context");
127 self.subsetImage = [UIImage imageWithCGImage:subsetImageRef];
128 CGImageRelease(subsetImageRef);
130 CGContextRelease(ctx);
132 NSLog(@"released context");
136 - (void)decode:(id)arg {
137 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
141 NSLog(@"created QRCoreReader");
144 Ref<MonochromeBitmapSource> grayImage
145 (new GrayBytesMonochromeBitmapSource(subsetData, subsetWidth, subsetHeight, subsetBytesPerRow));
147 NSLog(@"created GrayBytesMonochromeBitmapSource", subsetWidth, subsetHeight);
148 NSLog(@"grayImage count = %d", grayImage->count());
151 TwoDDecoderResult *decoderResult = nil;
154 for (int i = 0; !decoderResult && i < 4; i++) {
159 NSLog(@"decoding gray image");
161 Ref<Result> result(reader.decode(grayImage));
163 NSLog(@"gray image decoded");
166 Ref<String> resultText(result->getText());
167 const char *cString = resultText->getText().c_str();
168 ArrayRef<Ref<ResultPoint> > resultPoints = result->getResultPoints();
169 NSMutableArray *points =
170 [NSMutableArray arrayWithCapacity:resultPoints->size()];
172 for (size_t i = 0; i < resultPoints->size(); i++) {
173 Ref<ResultPoint> rp(resultPoints[i]);
174 CGPoint p = CGPointMake(rp->getX(), rp->getY());
175 [points addObject:[NSValue valueWithCGPoint:p]];
178 NSString *resultString = [NSString stringWithCString:cString
179 encoding:NSUTF8StringEncoding];
181 decoderResult = [TwoDDecoderResult resultWithText:resultString
183 } catch (ReaderException *rex) {
184 NSLog(@"failed to decode, caught ReaderException '%s'",
187 } catch (IllegalArgumentException *iex) {
188 NSLog(@"failed to decode, caught IllegalArgumentException '%s'",
192 NSLog(@"Caught unknown exception!");
196 if (!decoderResult) {
198 NSLog(@"rotating gray image");
200 grayImage = grayImage->rotateCounterClockwise();
202 NSLog(@"gray image rotated");
209 [self performSelectorOnMainThread:@selector(didDecodeImage:)
210 withObject:decoderResult
213 [self performSelectorOnMainThread:@selector(failedToDecodeImage:)
214 withObject:NSLocalizedString(@"Decoder BarcodeDetectionFailure", @"No barcode detected.")
219 self.subsetData = NULL;
223 NSLog(@"finished decoding.");
226 // if this is not the main thread, then we end it
227 if (![NSThread isMainThread]) {
232 - (void) decodeImage:(UIImage *)i {
233 [self decodeImage:i cropRect:CGRectMake(0.0f, 0.0f, image.size.width, image.size.height)];
236 - (void) decodeImage:(UIImage *)i cropRect:(CGRect)cr {
240 [self prepareSubset];
241 [self.delegate decoder:self willDecodeImage:i usingSubset:self.subsetImage];
244 [self performSelectorOnMainThread:@selector(progressDecodingImage:)
245 withObject:NSLocalizedString(@"Decoder MessageWhileDecoding", @"Decoding ...")
248 [NSThread detachNewThreadSelector:@selector(decode:)
255 [subsetImage release];
256 if (subsetData) free(subsetData);