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/HybridBinarizer.h>
31 #include <zxing/common/GreyscaleLuminanceSource.h>
33 using namespace zxing;
35 class ZXingWidgetControllerCallback : public zxing::ResultPointCallback {
39 ZXingWidgetControllerCallback(Decoder* _decoder) : decoder(_decoder) {}
40 void foundPossibleResultPoint(ResultPoint const& result) {
42 point.x = result.getX();
43 point.y = result.getY();
44 [decoder resultPointCallback:point];
48 @implementation Decoder
52 @synthesize subsetImage;
53 @synthesize subsetData;
54 @synthesize subsetWidth;
55 @synthesize subsetHeight;
56 @synthesize subsetBytesPerRow;
61 - (void)willDecodeImage {
62 if ([self.delegate respondsToSelector:@selector(decoder:willDecodeImage:usingSubset:)]) {
63 [self.delegate decoder:self willDecodeImage:self.image usingSubset:self.subsetImage];
67 - (void)didDecodeImage:(TwoDDecoderResult *)result {
68 if ([self.delegate respondsToSelector:@selector(decoder:didDecodeImage:usingSubset:withResult:)]) {
69 [self.delegate decoder:self didDecodeImage:self.image usingSubset:self.subsetImage withResult:result];
75 - (void)failedToDecodeImage:(NSString *)reason {
76 if ([self.delegate respondsToSelector:@selector(decoder:failedToDecodeImage:usingSubset:reason:)]) {
77 [self.delegate decoder:self failedToDecodeImage:self.image usingSubset:self.subsetImage reason:reason];
81 - (void)resultPointCallback:(CGPoint)point {
82 if ([self.delegate respondsToSelector:@selector(decoder:foundPossibleResultPoint:)]) {
83 [self.delegate decoder:self foundPossibleResultPoint:point];
87 #define SUBSET_SIZE 360
88 - (void) prepareSubset {
89 CGSize size = [image size];
91 NSLog(@"decoding: image is (%.1f x %.1f), cropRect is (%.1f,%.1f)x(%.1f,%.1f)", size.width, size.height,
92 cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height);
94 float scale = fminf(1.0f, fmaxf(SUBSET_SIZE / cropRect.size.width, SUBSET_SIZE / cropRect.size.height));
95 CGPoint offset = CGPointMake(-cropRect.origin.x, -cropRect.origin.y);
97 NSLog(@" offset = (%.1f, %.1f), scale = %.3f", offset.x, offset.y, scale);
100 subsetWidth = cropRect.size.width * scale;
101 subsetHeight = cropRect.size.height * scale;
103 subsetBytesPerRow = ((subsetWidth + 0xf) >> 4) << 4;
105 NSLog(@"decoding: image to decode is (%d x %d) (%d bytes/row)", subsetWidth, subsetHeight, subsetBytesPerRow);
108 subsetData = (unsigned char *)malloc(subsetBytesPerRow * subsetHeight);
110 NSLog(@"allocated %d bytes of memory", subsetBytesPerRow * subsetHeight);
113 CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
116 CGBitmapContextCreate(subsetData, subsetWidth, subsetHeight,
117 8, subsetBytesPerRow, grayColorSpace,
119 CGColorSpaceRelease(grayColorSpace);
120 CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
121 CGContextSetAllowsAntialiasing(ctx, false);
122 // adjust the coordinate system
123 CGContextTranslateCTM(ctx, 0.0, subsetHeight);
124 CGContextScaleCTM(ctx, 1.0, -1.0);
127 NSLog(@"created %dx%d bitmap context", subsetWidth, subsetHeight);
130 UIGraphicsPushContext(ctx);
131 CGRect rect = CGRectMake(offset.x * scale, offset.y * scale, scale * size.width, scale * size.height);
133 NSLog(@"rect for image = (%.1f,%.1f)x(%.1f,%.1f)", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
135 [image drawInRect:rect];
136 UIGraphicsPopContext();
139 NSLog(@"drew image into %d(%d)x%d bitmap context", subsetWidth, subsetBytesPerRow, subsetHeight);
143 NSLog(@"flushed context");
146 CGImageRef subsetImageRef = CGBitmapContextCreateImage(ctx);
148 NSLog(@"created CGImage from context");
151 self.subsetImage = [UIImage imageWithCGImage:subsetImageRef];
152 CGImageRelease(subsetImageRef);
154 CGContextRelease(ctx);
156 NSLog(@"released context");
161 NSAutoreleasePool* mainpool = [[NSAutoreleasePool alloc] init];
162 TwoDDecoderResult *decoderResult = nil;
165 //NSSet *formatReaders = [FormatReader formatReaders];
166 NSSet *formatReaders = self.readers;
167 Ref<LuminanceSource> source
168 (new GreyscaleLuminanceSource(subsetData, subsetBytesPerRow, subsetHeight, 0, 0, subsetWidth, subsetHeight));
170 Ref<Binarizer> binarizer (new HybridBinarizer(source));
172 Ref<BinaryBitmap> grayImage (new BinaryBitmap(binarizer));
175 NSLog(@"created GreyscaleLuminanceSource(%p,%d,%d,%d,%d,%d,%d)",
176 subsetData, subsetBytesPerRow, subsetHeight, 0, 0, subsetWidth, subsetHeight);
177 NSLog(@"grayImage count = %d", grayImage->count());
181 for (int i = 0; !decoderResult && i < 4; i++) {
183 for (FormatReader *reader in formatReaders) {
184 NSAutoreleasePool *secondarypool = [[NSAutoreleasePool alloc] init];
187 NSLog(@"decoding gray image");
189 ResultPointCallback* callback_pointer(new ZXingWidgetControllerCallback(self));
190 Ref<ResultPointCallback> callback(callback_pointer);
191 Ref<Result> result([reader decode:grayImage andCallback:callback]);
193 NSLog(@"gray image decoded");
196 Ref<String> resultText(result->getText());
197 const char *cString = resultText->getText().c_str();
198 const std::vector<Ref<ResultPoint> > &resultPoints = result->getResultPoints();
199 NSMutableArray *points =
200 [[NSMutableArray alloc ] initWithCapacity:resultPoints.size()];
202 for (size_t i = 0; i < resultPoints.size(); i++) {
203 const Ref<ResultPoint> &rp = resultPoints[i];
204 CGPoint p = CGPointMake(rp->getX(), rp->getY());
205 [points addObject:[NSValue valueWithCGPoint:p]];
208 NSString *resultString = [NSString stringWithCString:cString
209 encoding:NSUTF8StringEncoding];
211 decoderResult = [[TwoDDecoderResult resultWithText:resultString points:points] retain];
213 } catch (ReaderException &rex) {
214 NSLog(@"failed to decode, caught ReaderException '%s'",
216 } catch (IllegalArgumentException &iex) {
217 NSLog(@"failed to decode, caught IllegalArgumentException '%s'",
220 NSLog(@"Caught unknown exception!");
222 [secondarypool release];
226 if (!decoderResult) {
228 NSLog(@"rotating gray image");
230 grayImage = grayImage->rotateCounterClockwise();
232 NSLog(@"gray image rotated");
239 self.subsetData = NULL;
242 // [decoderResult release];
243 // decoderResult = nil;
247 [self performSelectorOnMainThread:@selector(didDecodeImage:)
248 withObject:decoderResult
251 [self performSelectorOnMainThread:@selector(failedToDecodeImage:)
252 withObject:NSLocalizedString(@"Decoder BarcodeDetectionFailure", @"No barcode detected.")
259 NSLog(@"finished decoding.");
263 return decoderResult == nil ? NO : YES;
266 - (BOOL) decodeImage:(UIImage *)i {
267 return [self decodeImage:i cropRect:CGRectMake(0.0f, 0.0f, i.size.width, i.size.height)];
270 - (BOOL) decodeImage:(UIImage *)i cropRect:(CGRect)cr {
273 [self prepareSubset];
274 [self willDecodeImage];
275 return [self decode];
280 [subsetImage release];