X-Git-Url: http://git.rot13.org/?p=zxing.git;a=blobdiff_plain;f=iphone%2FZXingWidget%2FClasses%2FDecoder.mm;h=75105881f26b971692ee3f2334ead80456c6a61c;hp=13d9439141f1e176cf234d2d4fe3c0827fb533aa;hb=620a215ce9340602fbd603af69462180039901c7;hpb=b5e91706c1f69adcaf5ece3a1b199899462238e7 diff --git a/iphone/ZXingWidget/Classes/Decoder.mm b/iphone/ZXingWidget/Classes/Decoder.mm index 13d94391..75105881 100644 --- a/iphone/ZXingWidget/Classes/Decoder.mm +++ b/iphone/ZXingWidget/Classes/Decoder.mm @@ -27,11 +27,24 @@ #include #include #include -#include +#include #include using namespace zxing; +class ZXingWidgetControllerCallback : public zxing::ResultPointCallback { +private: + Decoder* decoder; +public: + ZXingWidgetControllerCallback(Decoder* _decoder) : decoder(_decoder) {} + void foundPossibleResultPoint(ResultPoint const& result) { + CGPoint point; + point.x = result.getX(); + point.y = result.getY(); + [decoder resultPointCallback:point]; + } +}; + @implementation Decoder @synthesize image; @@ -42,32 +55,16 @@ using namespace zxing; @synthesize subsetHeight; @synthesize subsetBytesPerRow; @synthesize delegate; -@synthesize operationQueue; @synthesize readers; -- (id)init { - if (self = [super init]) { - NSOperationQueue *opQueue = [[NSOperationQueue alloc] init]; - self.operationQueue = opQueue; - [opQueue release]; - } - return self; -} - (void)willDecodeImage { if ([self.delegate respondsToSelector:@selector(decoder:willDecodeImage:usingSubset:)]) { [self.delegate decoder:self willDecodeImage:self.image usingSubset:self.subsetImage]; } } -- (void)progressDecodingImage:(NSString *)progress { - if ([self.delegate respondsToSelector:@selector(decoder:decodingImage:usingSubset:progress:)]) { - [self.delegate decoder:self decodingImage:self.image usingSubset:self.subsetImage progress:progress]; - } -} - - (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]; } @@ -76,13 +73,18 @@ using namespace zxing; } - (void)failedToDecodeImage:(NSString *)reason { - if ([self.delegate respondsToSelector:@selector(decoder:failedToDecodeImage:usingSubset:reason:)]) { [self.delegate decoder:self failedToDecodeImage:self.image usingSubset:self.subsetImage reason:reason]; } } -#define SUBSET_SIZE 320.0 +- (void)resultPointCallback:(CGPoint)point { + if ([self.delegate respondsToSelector:@selector(decoder:foundPossibleResultPoint:)]) { + [self.delegate decoder:self foundPossibleResultPoint:point]; + } +} + +#define SUBSET_SIZE 360 - (void) prepareSubset { CGSize size = [image size]; #ifdef DEBUG @@ -155,22 +157,26 @@ using namespace zxing; #endif } -- (void)decode:(id)arg { +- (BOOL)decode { NSAutoreleasePool* mainpool = [[NSAutoreleasePool alloc] init]; + TwoDDecoderResult *decoderResult = nil; + { //NSSet *formatReaders = [FormatReader formatReaders]; NSSet *formatReaders = self.readers; Ref source - (new GreyscaleLuminanceSource(subsetData, subsetBytesPerRow, subsetHeight, 0, 0, subsetWidth, subsetHeight)); - Ref binarizer (new GlobalHistogramBinarizer(source)); + (new GreyscaleLuminanceSource(subsetData, subsetBytesPerRow, subsetHeight, 0, 0, subsetWidth, subsetHeight)); + + Ref binarizer (new HybridBinarizer(source)); + source = 0; Ref grayImage (new BinaryBitmap(binarizer)); + binarizer = 0; #ifdef DEBUG - NSLog(@"created GreyscaleLuminanceSource", subsetWidth, subsetHeight); + NSLog(@"created GreyscaleLuminanceSource(%p,%d,%d,%d,%d,%d,%d)", + subsetData, subsetBytesPerRow, subsetHeight, 0, 0, subsetWidth, subsetHeight); NSLog(@"grayImage count = %d", grayImage->count()); #endif - TwoDDecoderResult *decoderResult = nil; - #ifdef TRY_ROTATIONS for (int i = 0; !decoderResult && i < 4; i++) { #endif @@ -180,7 +186,9 @@ using namespace zxing; #ifdef DEBUG NSLog(@"decoding gray image"); #endif - Ref result([reader decode:grayImage]); + ResultPointCallback* callback_pointer(new ZXingWidgetControllerCallback(self)); + Ref callback(callback_pointer); + Ref result([reader decode:grayImage andCallback:callback]); #ifdef DEBUG NSLog(@"gray image decoded"); #endif @@ -198,10 +206,9 @@ using namespace zxing; } NSString *resultString = [NSString stringWithCString:cString - encoding:NSUTF8StringEncoding]; - - decoderResult = [[TwoDDecoderResult resultWithText:resultString - points:points] retain]; + encoding:NSUTF8StringEncoding]; + + decoderResult = [[TwoDDecoderResult resultWithText:resultString points:points] retain]; [points release]; } catch (ReaderException &rex) { NSLog(@"failed to decode, caught ReaderException '%s'", @@ -231,6 +238,11 @@ using namespace zxing; free(subsetData); self.subsetData = NULL; + // DONT COMMIT + // [decoderResult release]; + // decoderResult = nil; + + if (decoderResult) { [self performSelectorOnMainThread:@selector(didDecodeImage:) withObject:decoderResult @@ -247,41 +259,26 @@ using namespace zxing; NSLog(@"finished decoding."); #endif [mainpool release]; -} -- (void) decodeImage:(UIImage *)i { - [self decodeImage:i cropRect:CGRectMake(0.0f, 0.0f, i.size.width, i.size.height)]; + return decoderResult == nil ? NO : YES; } - -- (void) asyncDecodeImage { - [self prepareSubset]; - [self willDecodeImage]; - [self performSelectorOnMainThread:@selector(willDecodeImage) - withObject:nil - waitUntilDone:NO]; - [self performSelectorOnMainThread:@selector(progressDecodingImage:) - withObject:NSLocalizedString(@"Decoder MessageWhileDecoding", @"Decoding ...") - waitUntilDone:NO]; -/* NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(decode:) object:nil]; - [operationQueue addOperation:op]; - [op release];*/ - [self decode:nil]; +- (BOOL) decodeImage:(UIImage *)i { + return [self decodeImage:i cropRect:CGRectMake(0.0f, 0.0f, i.size.width, i.size.height)]; } -- (void) decodeImage:(UIImage *)i cropRect:(CGRect)cr { + +- (BOOL) decodeImage:(UIImage *)i cropRect:(CGRect)cr { self.image = i; self.cropRect = cr; - NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(asyncDecodeImage) object:nil]; - [operationQueue addOperation:op]; - [op release]; + [self prepareSubset]; + [self willDecodeImage]; + return [self decode]; } - (void) dealloc { - [image release]; [subsetImage release]; - if (subsetData) free(subsetData); - [operationQueue release]; + free(subsetData); [readers release]; [super dealloc]; }