为什么我的ZXingObjC捕获委托被多次调用?

时间:2017-11-03 14:35:24

标签: objective-c delegates zxing zxingobjc

我正在使用ZXingObjC pod来扫描我的应用程序中的条形码,它的工作区别很好,因为我的捕获委托被多次调用,即使我停止了读者而且我不明白为什么

以下是我对阅读器视图的设置代码

- (IBAction)transactionListViewCameraBtn_Pressed:(id)sender {

    if([NWTillHelper isDebug] == 1) {
        NSLog(@"%s entered", __PRETTY_FUNCTION__);
    }

    self.capture = [[ZXCapture alloc] init];
    self.capture.camera = self.capture.back;
    self.capture.focusMode = AVCaptureFocusModeContinuousAutoFocus;

    [self.view.layer addSublayer:self.capture.layer];

    self.capture.delegate = self;

    [self applyOrientation];
}

我的代表看起来像这样

- (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result {
    if([NWTillHelper isDebug] == 1) {
        NSLog(@"%s entered", __PRETTY_FUNCTION__);
    }

    if (!result) return;

    // Vibrate
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    UIImage *image = [UIImage imageWithCGImage:capture.lastScannedImage];

    ZXCGImageLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:image.CGImage];
    ZXBinaryBitmap *bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];

    ZXDecodeHints *hints = [ZXDecodeHints hints];
    hints.tryHarder = YES;

    ZXMultiFormatReader *reader = [ZXMultiFormatReader reader];
    ZXGenericMultipleBarcodeReader *multiReader = [[ZXGenericMultipleBarcodeReader alloc] initWithDelegate:reader];

    NSError *error = nil;
    NSArray *results = [multiReader decodeMultiple:bitmap hints:hints error:&error];

    for (ZXResult *result in results) {
        NSLog(@"Decoded barcode %@", result.text);

        int createTransactionResult = -1;
        int barcodeIsTransactionId = -1;

        if ([result.text hasPrefix:@"--"]) {
            actualBarCodeStr = [result.text substringFromIndex:2];
            barcodeIsTransactionId = 1;
            break;
        } else {
            createTransactionResult = [NWBarCodeHelper createTransactionRowFromBarCode:result.text];

            if(createTransactionResult == 0) {
                [self.capture.layer removeFromSuperlayer];
            } else {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                    [self.capture start];
                });
            }

            if(barcodeIsTransactionId == 1) {
                [self.capture.layer removeFromSuperlayer];
                [self performSegueWithIdentifier:@"trListViewToTrSearchView" sender:self];
            }
        }
    }
    [self.capture stop];
}

我不明白为什么委托被多次调用,它就像我以某种方式缓冲的相机并不断向代表发送图像。我该怎么做才能阻止我的代表多次被调用?

0 个答案:

没有答案