ZXingObjc Integration IOS ,but didn't work at all

ぃ、小莉子 提交于 2019-12-13 08:09:54

问题


I have integrated the ZXingObjc framework,and ViewController as the RootViewController. the codes in ViewController.m follows:

#import <AudioToolbox/AudioToolbox.h>
#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) ZXCapture *capture;
@property (nonatomic, weak) IBOutlet UIView *scanRectView;
@property (nonatomic, weak) IBOutlet UILabel *decodedLabel;

@end

@implementation ViewController

#pragma mark - View Controller Methods

- (void)dealloc {
    [self.capture.layer removeFromSuperlayer];
}

- (void)viewDidLoad {
    [super viewDidLoad];

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

    self.capture.layer.frame = self.view.bounds;
    [self.view.layer addSublayer:self.capture.layer];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.capture.delegate = self;
    self.capture.layer.frame = self.view.bounds;
    self.capture.scanRect = self.scanRectView.frame;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}

#pragma mark - Private Methods

- (NSString *)barcodeFormatToString:(ZXBarcodeFormat)format {
    switch (format) {
        case kBarcodeFormatAztec:
            return @"Aztec";

        case kBarcodeFormatCodabar:
            return @"CODABAR";

        case kBarcodeFormatCode39:
            return @"Code 39";

        case kBarcodeFormatCode93:
            return @"Code 93";

        case kBarcodeFormatCode128:
            return @"Code 128";

        case kBarcodeFormatDataMatrix:
            return @"Data Matrix";

        case kBarcodeFormatEan8:
            return @"EAN-8";

        case kBarcodeFormatEan13:
            return @"EAN-13";

        case kBarcodeFormatITF:
            return @"ITF";

        case kBarcodeFormatPDF417:
            return @"PDF417";

        case kBarcodeFormatQRCode:
            return @"QR Code";

        case kBarcodeFormatRSS14:
            return @"RSS 14";

        case kBarcodeFormatRSSExpanded:
            return @"RSS Expanded";

        case kBarcodeFormatUPCA:
            return @"UPCA";

        case kBarcodeFormatUPCE:
            return @"UPCE";

        case kBarcodeFormatUPCEANExtension:
            return @"UPC/EAN extension";

        default:
            return @"Unknown";
    }
}

#pragma mark - ZXCaptureDelegate Methods


- (void)captureCameraIsReady:(ZXCapture *)capture
{
    NSLog(@"%s,%d",__FUNCTION__,__LINE__);

}

- (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result {

    if (!result) return;
    NSLog(@"%s,%d",__FUNCTION__,__LINE__);
    // We got a result. Display information about the result onscreen.
    NSString *formatString = [self barcodeFormatToString:result.barcodeFormat];
    NSString *display = [NSString stringWithFormat:@"Scanned!\n\nFormat: %@\n\nContents:\n%@", formatString, result.text];
    [self.decodedLabel performSelectorOnMainThread:@selector(setText:) withObject:display waitUntilDone:YES];

    // Vibrate
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    [self.capture stop];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self.capture start];
    });
}

@end

I found that the mothod - (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result doesn't work at all.

Next,the same codes i put it into the example of ZXingObjc,it works well. I really don't know what's wrong with me. Wish your help!


回答1:


I had the same problem. In my situation, the view controller would launch, but would never detect a barcode. Turns out I needed to add '-ObjC' to 'Other Linker Flags'.

Reference: https://developer.apple.com/library/ios/technotes/iOSStaticLibraries/Articles/configuration.html




回答2:


Did your Viewcontroller.h file adopt the ZXCapture protocol? Something like this:

    //ViewController.h    
    @interface ViewController: NSObject <ZXCaptureDelegate> //<---- this guy

    - (void)yourFunction;
    - (void)anotherFunction;

    @end

(I'm not sure what is its real name)



来源:https://stackoverflow.com/questions/30366192/zxingobjc-integration-ios-but-didnt-work-at-all

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!