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"
25 #import "FormatReader.h"
27 #include <zxing/BinaryBitmap.h>
28 #include <zxing/ReaderException.h>
29 #include <zxing/common/IllegalArgumentException.h>
30 #include <zxing/common/GlobalHistogramBinarizer.h>
31 #include "GrayBytesMonochromeBitmapSource.h"
33 using namespace zxing;
35 @implementation Decoder
39 @synthesize subsetImage;
40 @synthesize subsetData;
41 @synthesize subsetWidth;
42 @synthesize subsetHeight;
43 @synthesize subsetBytesPerRow;
45 @synthesize operationQueue;
49 if (self = [super init]) {
50 NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
51 self.operationQueue = opQueue;
56 - (void)willDecodeImage {
57 if ([self.delegate respondsToSelector:@selector(decoder:willDecodeImage:usingSubset:)]) {
58 [self.delegate decoder:self willDecodeImage:self.image usingSubset:self.subsetImage];
62 - (void)progressDecodingImage:(NSString *)progress {
63 if ([self.delegate respondsToSelector:@selector(decoder:decodingImage:usingSubset:progress:)]) {
64 [self.delegate decoder:self decodingImage:self.image usingSubset:self.subsetImage progress:progress];
68 - (void)didDecodeImage:(TwoDDecoderResult *)result {
70 if ([self.delegate respondsToSelector:@selector(decoder:didDecodeImage:usingSubset:withResult:)]) {
71 [self.delegate decoder:self didDecodeImage:self.image usingSubset:self.subsetImage withResult:result];
77 - (void)failedToDecodeImage:(NSString *)reason {
79 if ([self.delegate respondsToSelector:@selector(decoder:failedToDecodeImage:usingSubset:reason:)]) {
80 [self.delegate decoder:self failedToDecodeImage:self.image usingSubset:self.subsetImage reason:reason];
84 #define SUBSET_SIZE 320.0
85 - (void) prepareSubset {
86 CGSize size = [image size];
88 NSLog(@"decoding: image is (%.1f x %.1f), cropRect is (%.1f,%.1f)x(%.1f,%.1f)", size.width, size.height,
89 cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height);
91 float scale = fminf(1.0f, fmaxf(SUBSET_SIZE / cropRect.size.width, SUBSET_SIZE / cropRect.size.height));
92 CGPoint offset = CGPointMake(-cropRect.origin.x, -cropRect.origin.y);
94 NSLog(@" offset = (%.1f, %.1f), scale = %.3f", offset.x, offset.y, scale);
97 subsetWidth = cropRect.size.width * scale;
98 subsetHeight = cropRect.size.height * scale;
100 subsetBytesPerRow = ((subsetWidth + 0xf) >> 4) << 4;
102 NSLog(@"decoding: image to decode is (%d x %d) (%d bytes/row)", subsetWidth, subsetHeight, subsetBytesPerRow);
105 subsetData = (unsigned char *)malloc(subsetBytesPerRow * subsetHeight);
107 NSLog(@"allocated %d bytes of memory", subsetBytesPerRow * subsetHeight);
110 CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
113 CGBitmapContextCreate(subsetData, subsetWidth, subsetHeight,
114 8, subsetBytesPerRow, grayColorSpace,
116 CGColorSpaceRelease(grayColorSpace);
117 CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
118 CGContextSetAllowsAntialiasing(ctx, false);
119 // adjust the coordinate system
120 CGContextTranslateCTM(ctx, 0.0, subsetHeight);
121 CGContextScaleCTM(ctx, 1.0, -1.0);
124 NSLog(@"created %dx%d bitmap context", subsetWidth, subsetHeight);
127 UIGraphicsPushContext(ctx);
128 CGRect rect = CGRectMake(offset.x * scale, offset.y * scale, scale * size.width, scale * size.height);
130 NSLog(@"rect for image = (%.1f,%.1f)x(%.1f,%.1f)", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
132 [image drawInRect:rect];
133 UIGraphicsPopContext();
136 NSLog(@"drew image into %d(%d)x%d bitmap context", subsetWidth, subsetBytesPerRow, subsetHeight);
140 NSLog(@"flushed context");
143 CGImageRef subsetImageRef = CGBitmapContextCreateImage(ctx);
145 NSLog(@"created CGImage from context");
148 self.subsetImage = [UIImage imageWithCGImage:subsetImageRef];
149 CGImageRelease(subsetImageRef);
151 CGContextRelease(ctx);
153 NSLog(@"released context");
157 - (void)decode:(id)arg {
159 NSAutoreleasePool* mainpool = [[NSAutoreleasePool alloc] init];
162 NSSet *formatReaders = [FormatReader formatReaders];
164 Ref<LuminanceSource> source (new GrayBytesMonochromeBitmapSource(subsetData, subsetWidth, subsetHeight, subsetBytesPerRow));
165 Ref<Binarizer> binarizer (new GlobalHistogramBinarizer(source));
166 Ref<BinaryBitmap> grayImage (new BinaryBitmap(binarizer));
168 NSLog(@"created GrayBytesMonochromeBitmapSource", subsetWidth, subsetHeight);
169 NSLog(@"grayImage count = %d", grayImage->count());
172 TwoDDecoderResult *decoderResult = nil;
175 for (int i = 0; !decoderResult && i < 4; i++) {
177 for (FormatReader *reader in formatReaders) {
178 NSAutoreleasePool *secondarypool = [[NSAutoreleasePool alloc] init];
181 NSLog(@"decoding gray image");
183 Ref<Result> result([reader decode:grayImage]);
185 NSLog(@"gray image decoded");
188 Ref<String> resultText(result->getText());
189 const char *cString = resultText->getText().c_str();
190 const std::vector<Ref<ResultPoint> > &resultPoints = result->getResultPoints();
191 NSMutableArray *points =
192 [NSMutableArray arrayWithCapacity:resultPoints.size()];
194 for (size_t i = 0; i < resultPoints.size(); i++) {
195 const Ref<ResultPoint> &rp = resultPoints[i];
196 CGPoint p = CGPointMake(rp->getX(), rp->getY());
197 [points addObject:[NSValue valueWithCGPoint:p]];
200 NSString *resultString = [NSString stringWithCString:cString
201 encoding:NSUTF8StringEncoding];
203 decoderResult = [[TwoDDecoderResult resultWithText:resultString
204 points:points] retain];
205 } catch (ReaderException &rex) {
206 NSLog(@"failed to decode, caught ReaderException '%s'",
208 } catch (IllegalArgumentException &iex) {
209 NSLog(@"failed to decode, caught IllegalArgumentException '%s'",
212 NSLog(@"Caught unknown exception!");
214 [secondarypool release];
218 if (!decoderResult) {
220 NSLog(@"rotating gray image");
222 grayImage = grayImage->rotateCounterClockwise();
224 NSLog(@"gray image rotated");
231 self.subsetData = NULL;
234 [self performSelectorOnMainThread:@selector(didDecodeImage:)
235 withObject:decoderResult
238 [self performSelectorOnMainThread:@selector(failedToDecodeImage:)
239 withObject:NSLocalizedString(@"Decoder BarcodeDetectionFailure", @"No barcode detected.")
246 NSLog(@"finished decoding.");
251 - (void) decodeImage:(UIImage *)i {
252 [self decodeImage:i cropRect:CGRectMake(0.0f, 0.0f, i.size.width, i.size.height)];
255 - (void) decodeImage:(UIImage *)i cropRect:(CGRect)cr {
259 [self prepareSubset];
260 [self willDecodeImage];
261 [self performSelectorOnMainThread:@selector(progressDecodingImage:)
262 withObject:NSLocalizedString(@"Decoder MessageWhileDecoding", @"Decoding ...")
264 NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(decode:) object:nil];
265 [operationQueue addOperation:op];
267 //[self performSelectorInBackground:@selector(decode:) withObject:nil];
270 //[self performSelector:@selector(decode:) onThread:decodingThread withObject:nil waitUntilDone:NO];
271 /*[NSThread detachNewThreadSelector:@selector(decode:)
279 [subsetImage release];
280 if (subsetData) free(subsetData);
281 [operationQueue release];