2012-02-17 18:14:45 -08:00
|
|
|
// -*- Mode: ObjC; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2010-05-12 11:13:27 -07:00
|
|
|
/*
|
2012-02-17 18:14:45 -08:00
|
|
|
* Copyright 2008-2012 ZXing authors
|
2010-05-12 11:13:27 -07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "Decoder.h"
|
|
|
|
#import "TwoDDecoderResult.h"
|
|
|
|
#import "FormatReader.h"
|
|
|
|
|
|
|
|
#include <zxing/BinaryBitmap.h>
|
|
|
|
#include <zxing/ReaderException.h>
|
|
|
|
#include <zxing/common/IllegalArgumentException.h>
|
2010-08-17 14:18:56 -07:00
|
|
|
#include <zxing/common/HybridBinarizer.h>
|
2010-08-08 21:46:06 -07:00
|
|
|
#include <zxing/common/GreyscaleLuminanceSource.h>
|
2010-05-12 11:13:27 -07:00
|
|
|
|
|
|
|
using namespace zxing;
|
|
|
|
|
2010-08-17 14:18:56 -07:00
|
|
|
class ZXingWidgetControllerCallback : public zxing::ResultPointCallback {
|
|
|
|
private:
|
|
|
|
Decoder* decoder;
|
|
|
|
public:
|
2012-02-17 18:14:45 -08:00
|
|
|
ZXingWidgetControllerCallback(Decoder* _decoder) : decoder(_decoder) {}
|
2010-08-17 14:18:56 -07:00
|
|
|
void foundPossibleResultPoint(ResultPoint const& result) {
|
|
|
|
CGPoint point;
|
|
|
|
point.x = result.getX();
|
|
|
|
point.y = result.getY();
|
|
|
|
[decoder resultPointCallback:point];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-31 23:00:53 -07:00
|
|
|
@interface Decoder ()
|
|
|
|
@property(nonatomic, retain) UIImage *subsetImage;
|
|
|
|
@end
|
|
|
|
|
2010-05-12 11:13:27 -07:00
|
|
|
@implementation Decoder
|
|
|
|
|
|
|
|
@synthesize image;
|
|
|
|
@synthesize subsetImage;
|
|
|
|
@synthesize delegate;
|
2010-06-15 15:01:04 -07:00
|
|
|
@synthesize readers;
|
2010-05-12 11:13:27 -07:00
|
|
|
|
|
|
|
- (void)willDecodeImage {
|
|
|
|
if ([self.delegate respondsToSelector:@selector(decoder:willDecodeImage:usingSubset:)]) {
|
|
|
|
[self.delegate decoder:self willDecodeImage:self.image usingSubset:self.subsetImage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didDecodeImage:(TwoDDecoderResult *)result {
|
|
|
|
if ([self.delegate respondsToSelector:@selector(decoder:didDecodeImage:usingSubset:withResult:)]) {
|
|
|
|
[self.delegate decoder:self didDecodeImage:self.image usingSubset:self.subsetImage withResult:result];
|
|
|
|
}
|
2010-05-20 06:17:39 -07:00
|
|
|
[result release];
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)failedToDecodeImage:(NSString *)reason {
|
2011-06-01 11:17:37 -07:00
|
|
|
if (!self) return;
|
|
|
|
if ([delegate respondsToSelector:@selector(decoder:failedToDecodeImage:usingSubset:reason:)]) {
|
|
|
|
[delegate decoder:self failedToDecodeImage:self.image usingSubset:self.subsetImage reason:reason];
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-17 14:18:56 -07:00
|
|
|
- (void)resultPointCallback:(CGPoint)point {
|
|
|
|
if ([self.delegate respondsToSelector:@selector(decoder:foundPossibleResultPoint:)]) {
|
|
|
|
[self.delegate decoder:self foundPossibleResultPoint:point];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SUBSET_SIZE 360
|
2013-04-07 12:24:11 -07:00
|
|
|
|
2013-03-31 23:00:53 -07:00
|
|
|
- (ArrayRef<char>) prepareSubset {
|
2010-05-12 11:13:27 -07:00
|
|
|
CGSize size = [image size];
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@"decoding: image is (%.1f x %.1f), cropRect is (%.1f,%.1f)x(%.1f,%.1f)", size.width, size.height,
|
2012-02-17 18:14:45 -08:00
|
|
|
cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height);
|
2010-05-12 11:13:27 -07:00
|
|
|
#endif
|
|
|
|
float scale = fminf(1.0f, fmaxf(SUBSET_SIZE / cropRect.size.width, SUBSET_SIZE / cropRect.size.height));
|
|
|
|
CGPoint offset = CGPointMake(-cropRect.origin.x, -cropRect.origin.y);
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@" offset = (%.1f, %.1f), scale = %.3f", offset.x, offset.y, scale);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
subsetWidth = cropRect.size.width * scale;
|
|
|
|
subsetHeight = cropRect.size.height * scale;
|
|
|
|
|
|
|
|
subsetBytesPerRow = ((subsetWidth + 0xf) >> 4) << 4;
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2013-04-07 12:24:11 -07:00
|
|
|
NSLog(@"decoding: image to decode is (%lu x %lu) (%lu bytes/row)", subsetWidth, subsetHeight, subsetBytesPerRow);
|
2010-05-12 11:13:27 -07:00
|
|
|
#endif
|
|
|
|
|
2013-03-31 23:00:53 -07:00
|
|
|
ArrayRef<char> subsetData (subsetBytesPerRow * subsetHeight);
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2013-04-07 12:24:11 -07:00
|
|
|
NSLog(@"allocated %lu bytes of memory", subsetBytesPerRow * subsetHeight);
|
2010-05-12 11:13:27 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
|
|
|
|
|
|
|
|
CGContextRef ctx =
|
2013-03-31 23:00:53 -07:00
|
|
|
CGBitmapContextCreate(&subsetData->values()[0], subsetWidth, subsetHeight,
|
2012-02-17 18:14:45 -08:00
|
|
|
8, subsetBytesPerRow, grayColorSpace,
|
|
|
|
kCGImageAlphaNone);
|
2010-05-12 11:13:27 -07:00
|
|
|
CGColorSpaceRelease(grayColorSpace);
|
|
|
|
CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
|
|
|
|
CGContextSetAllowsAntialiasing(ctx, false);
|
|
|
|
// adjust the coordinate system
|
|
|
|
CGContextTranslateCTM(ctx, 0.0, subsetHeight);
|
|
|
|
CGContextScaleCTM(ctx, 1.0, -1.0);
|
|
|
|
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2013-04-07 12:24:11 -07:00
|
|
|
NSLog(@"created %lux%lu bitmap context", subsetWidth, subsetHeight);
|
2010-05-12 11:13:27 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
UIGraphicsPushContext(ctx);
|
|
|
|
CGRect rect = CGRectMake(offset.x * scale, offset.y * scale, scale * size.width, scale * size.height);
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@"rect for image = (%.1f,%.1f)x(%.1f,%.1f)", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
|
|
|
|
#endif
|
|
|
|
[image drawInRect:rect];
|
|
|
|
UIGraphicsPopContext();
|
|
|
|
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2013-04-07 12:24:11 -07:00
|
|
|
NSLog(@"drew image into %lu(%lu)x%lu bitmap context", subsetWidth, subsetBytesPerRow, subsetHeight);
|
2010-05-12 11:13:27 -07:00
|
|
|
#endif
|
|
|
|
CGContextFlush(ctx);
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@"flushed context");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CGImageRef subsetImageRef = CGBitmapContextCreateImage(ctx);
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@"created CGImage from context");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
self.subsetImage = [UIImage imageWithCGImage:subsetImageRef];
|
|
|
|
CGImageRelease(subsetImageRef);
|
|
|
|
|
|
|
|
CGContextRelease(ctx);
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@"released context");
|
|
|
|
#endif
|
2013-03-31 23:00:53 -07:00
|
|
|
return subsetData;
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
2013-03-31 23:00:53 -07:00
|
|
|
- (BOOL)decode:(ArrayRef<char>)subsetData {
|
2010-05-29 04:31:41 -07:00
|
|
|
NSAutoreleasePool* mainpool = [[NSAutoreleasePool alloc] init];
|
2010-08-17 14:18:56 -07:00
|
|
|
TwoDDecoderResult *decoderResult = nil;
|
2011-01-27 08:45:25 -08:00
|
|
|
BOOL returnCode = NO;
|
2010-05-12 11:13:27 -07:00
|
|
|
{
|
2010-06-15 15:01:04 -07:00
|
|
|
//NSSet *formatReaders = [FormatReader formatReaders];
|
|
|
|
NSSet *formatReaders = self.readers;
|
2010-08-08 21:46:06 -07:00
|
|
|
Ref<LuminanceSource> source
|
2013-03-31 23:00:53 -07:00
|
|
|
(new GreyscaleLuminanceSource(ArrayRef<char>(subsetData), subsetBytesPerRow, subsetHeight, 0, 0, subsetWidth, subsetHeight));
|
|
|
|
subsetData = 0;
|
2010-08-17 14:18:56 -07:00
|
|
|
|
|
|
|
Ref<Binarizer> binarizer (new HybridBinarizer(source));
|
|
|
|
source = 0;
|
2010-05-12 11:13:27 -07:00
|
|
|
Ref<BinaryBitmap> grayImage (new BinaryBitmap(binarizer));
|
2010-08-17 14:18:56 -07:00
|
|
|
binarizer = 0;
|
2010-05-12 11:13:27 -07:00
|
|
|
|
|
|
|
#ifdef TRY_ROTATIONS
|
2010-05-18 18:52:38 -07:00
|
|
|
for (int i = 0; !decoderResult && i < 4; i++) {
|
2010-05-12 11:13:27 -07:00
|
|
|
#endif
|
|
|
|
for (FormatReader *reader in formatReaders) {
|
2010-05-29 04:31:41 -07:00
|
|
|
NSAutoreleasePool *secondarypool = [[NSAutoreleasePool alloc] init];
|
2011-04-13 12:47:41 -07:00
|
|
|
NSMutableArray *points = nil;
|
|
|
|
NSString *resultString = nil;
|
2010-05-12 11:13:27 -07:00
|
|
|
try {
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@"decoding gray image");
|
2012-02-17 18:14:45 -08:00
|
|
|
#endif
|
2010-08-17 14:18:56 -07:00
|
|
|
ResultPointCallback* callback_pointer(new ZXingWidgetControllerCallback(self));
|
|
|
|
Ref<ResultPointCallback> callback(callback_pointer);
|
|
|
|
Ref<Result> result([reader decode:grayImage andCallback:callback]);
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@"gray image decoded");
|
2012-02-17 18:14:45 -08:00
|
|
|
#endif
|
2010-05-12 11:13:27 -07:00
|
|
|
|
|
|
|
Ref<String> resultText(result->getText());
|
|
|
|
const char *cString = resultText->getText().c_str();
|
2013-03-31 23:00:53 -07:00
|
|
|
const ArrayRef<Ref<ResultPoint> > &resultPoints = result->getResultPoints();
|
2013-04-17 08:16:54 -07:00
|
|
|
points = [[NSMutableArray alloc ] initWithCapacity:resultPoints->size()];
|
2010-05-12 11:13:27 -07:00
|
|
|
|
2013-04-17 08:16:54 -07:00
|
|
|
for (int i = 0; i < resultPoints->size(); i++) {
|
2010-05-12 11:13:27 -07:00
|
|
|
const Ref<ResultPoint> &rp = resultPoints[i];
|
|
|
|
CGPoint p = CGPointMake(rp->getX(), rp->getY());
|
|
|
|
[points addObject:[NSValue valueWithCGPoint:p]];
|
|
|
|
}
|
|
|
|
|
2011-04-13 12:47:41 -07:00
|
|
|
resultString = [[NSString alloc] initWithCString:cString encoding:NSUTF8StringEncoding];
|
2011-01-27 08:45:25 -08:00
|
|
|
if (decoderResult) [decoderResult release];
|
|
|
|
decoderResult = [[TwoDDecoderResult alloc] initWithText:resultString points:points];
|
2010-05-12 11:13:27 -07:00
|
|
|
} catch (ReaderException &rex) {
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@"failed to decode, caught ReaderException '%s'",
|
2012-02-17 18:14:45 -08:00
|
|
|
rex.what());
|
2011-07-08 15:50:07 -07:00
|
|
|
#endif
|
2010-05-12 11:13:27 -07:00
|
|
|
} catch (IllegalArgumentException &iex) {
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@"failed to decode, caught IllegalArgumentException '%s'",
|
2012-02-17 18:14:45 -08:00
|
|
|
iex.what());
|
2011-07-08 15:50:07 -07:00
|
|
|
#endif
|
2010-05-12 11:13:27 -07:00
|
|
|
} catch (...) {
|
|
|
|
NSLog(@"Caught unknown exception!");
|
|
|
|
}
|
2011-04-13 12:47:41 -07:00
|
|
|
[resultString release];
|
|
|
|
[points release];
|
2010-05-29 04:31:41 -07:00
|
|
|
[secondarypool release];
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TRY_ROTATIONS
|
|
|
|
if (!decoderResult) {
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@"rotating gray image");
|
|
|
|
#endif
|
|
|
|
grayImage = grayImage->rotateCounterClockwise();
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@"gray image rotated");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-20 06:17:39 -07:00
|
|
|
|
2010-05-12 11:13:27 -07:00
|
|
|
if (decoderResult) {
|
|
|
|
[self performSelectorOnMainThread:@selector(didDecodeImage:)
|
2012-02-17 18:14:45 -08:00
|
|
|
withObject:[decoderResult copy]
|
|
|
|
waitUntilDone:NO];
|
2011-01-27 08:45:25 -08:00
|
|
|
[decoderResult release];
|
|
|
|
returnCode = YES;
|
2010-05-12 11:13:27 -07:00
|
|
|
} else {
|
|
|
|
[self performSelectorOnMainThread:@selector(failedToDecodeImage:)
|
2012-02-17 18:14:45 -08:00
|
|
|
withObject:NSLocalizedString(@"Decoder BarcodeDetectionFailure", @"No barcode detected.")
|
|
|
|
waitUntilDone:NO];
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
}
|
2010-05-29 04:31:41 -07:00
|
|
|
|
|
|
|
|
2013-04-07 14:59:18 -07:00
|
|
|
#if ZXING_DEBUG
|
2010-05-12 11:13:27 -07:00
|
|
|
NSLog(@"finished decoding.");
|
|
|
|
#endif
|
2010-05-29 04:31:41 -07:00
|
|
|
[mainpool release];
|
2010-05-12 11:13:27 -07:00
|
|
|
|
2011-01-27 08:45:25 -08:00
|
|
|
return returnCode;
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
2010-08-17 14:18:56 -07:00
|
|
|
- (BOOL) decodeImage:(UIImage *)i {
|
|
|
|
return [self decodeImage:i cropRect:CGRectMake(0.0f, 0.0f, i.size.width, i.size.height)];
|
2010-05-30 04:40:09 -07:00
|
|
|
}
|
2010-08-17 14:18:56 -07:00
|
|
|
|
|
|
|
- (BOOL) decodeImage:(UIImage *)i cropRect:(CGRect)cr {
|
2010-05-30 04:40:09 -07:00
|
|
|
self.image = i;
|
2013-03-31 23:00:53 -07:00
|
|
|
cropRect = cr;
|
|
|
|
ArrayRef<char> subsetData = [self prepareSubset];
|
2010-08-17 14:18:56 -07:00
|
|
|
[self willDecodeImage];
|
2013-03-31 23:00:53 -07:00
|
|
|
return [self decode:subsetData];
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc {
|
2011-06-01 11:17:37 -07:00
|
|
|
delegate = nil;
|
2010-05-12 11:13:27 -07:00
|
|
|
[image release];
|
|
|
|
[subsetImage release];
|
2010-06-15 15:01:04 -07:00
|
|
|
[readers release];
|
2010-05-12 11:13:27 -07:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|