Issue 479
[zxing.git] / iphone / ZXingWidget / Classes / Decoder.mm
index 13d9439..7510588 100644 (file)
 #include <zxing/BinaryBitmap.h>
 #include <zxing/ReaderException.h>
 #include <zxing/common/IllegalArgumentException.h>
-#include <zxing/common/GlobalHistogramBinarizer.h>
+#include <zxing/common/HybridBinarizer.h>
 #include <zxing/common/GreyscaleLuminanceSource.h>
 
 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<LuminanceSource> source 
-           (new GreyscaleLuminanceSource(subsetData, subsetBytesPerRow, subsetHeight, 0, 0, subsetWidth, subsetHeight));
-    Ref<Binarizer> binarizer (new GlobalHistogramBinarizer(source));
+        (new GreyscaleLuminanceSource(subsetData, subsetBytesPerRow, subsetHeight, 0, 0, subsetWidth, subsetHeight));
+
+    Ref<Binarizer> binarizer (new HybridBinarizer(source));
+    source = 0;
     Ref<BinaryBitmap> 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> result([reader decode:grayImage]);
+          ResultPointCallback* callback_pointer(new ZXingWidgetControllerCallback(self));
+          Ref<ResultPointCallback> callback(callback_pointer);
+          Ref<Result> 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];
 }