在UIView中实现ZBar QR码阅读器

时间:2012-12-16 05:00:18

标签: objective-c ios qr-code zbar-sdk zbar

我真的需要帮助。我是iOS / Objective-C的新手,很抱歉,如果解决问题很明显,或者我的代码很糟糕。对我来说很容易!! : - )

我正在努力将ZBarSDK整合到我正在构建的iPad应用程序中读取QR码。如果我使用ZBarReaderController(其中有很多关于实现的教程和指南),它可以正常工作。但是我想让相机出现在UIView中而不是全屏相机。

现在我已经按照预期使得摄像机视图(readerView)出现在UIView(ZBarReaderView)中,但是在扫描代码时出现错误。在扫描代码之前不会出现错误,这使我相信这是代理相关或其他内容。

以下是我的代码的重要部分:(在PCH文件中导入ZBarSDK.h)

SignInViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@class AVCaptureSession, AVCaptureDevice;

@interface SignInViewController : UIViewController
< ZBarReaderDelegate >
{
    ZBarReaderView *readerView;
    UITextView *resultText;
}

@property (nonatomic, retain) UIImagePickerController *imgPicker;
@property (strong, nonatomic) IBOutlet UITextView *resultText;
@property (strong, nonatomic) IBOutlet ZBarReaderView *readerView;

-(IBAction)StartScan:(id) sender;

SignInViewController.m

#import "SignInViewController.h"

@interface SignInViewController ()

@end

@implementation SignInViewController


@synthesize resultText, readerView;

-(IBAction)StartScan:(id) sender
{
readerView = [ZBarReaderView new];
readerView.readerDelegate = self;

readerView.tracksSymbols = NO;

readerView.frame = CGRectMake(30,70,230,230);
readerView.torchMode = 0;
readerView.device = [self frontFacingCameraIfAvailable];

ZBarImageScanner *scanner = readerView.scanner;
[scanner setSymbology: ZBAR_I25
               config: ZBAR_CFG_ENABLE
                   to: 0];


[self relocateReaderPopover:[self interfaceOrientation]];

[readerView start];

[self.view addSubview: readerView];
resultText.hidden=NO;

}


- (void) readerControllerDidFailToRead: (ZBarReaderController*) reader
                         withRetry: (BOOL) retry{
NSLog(@"the image picker failing to read");

}

- (void) imagePickerController: (UIImagePickerController*) reader     didFinishPickingMediaWithInfo: (NSDictionary*) info
{


NSLog(@"the image picker is calling successfully %@",info);
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
NSString *hiddenData;
for(symbol in results)
    hiddenData=[NSString stringWithString:symbol.data];


NSLog(@"the symbols  is the following %@",symbol.data);

resultText.text=symbol.data;


NSLog(@"BARCODE= %@",symbol.data);

NSLog(@"SYMBOL : %@",hiddenData);
resultText.text=hiddenData;

}

扫描代码时出现的错误:

2012-12-16 14:28:32.797 QRTestApp[7970:907] -[SignInViewController     readerView:didReadSymbols:fromImage:]: unrecognized selector sent to instance 0x1e88b1c0
2012-12-16 14:28:32.799 QRTestApp[7970:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SignInViewController readerView:didReadSymbols:fromImage:]: unrecognized selector sent to instance 0x1e88b1c0'

我不太担心结果会发生什么,只是想克服这个错误。由于严重缺乏关于ZBarReaderView的教程或文档(无论如何对于初学者),我花了很多时间来让相机出现在UIView中。谢谢大家。

1 个答案:

答案 0 :(得分:2)

我修好了。有一个不正确的代表名称。应该是&#34; ZbarReaderViewDelegate&#34;。我错过了&#34;视图&#34;部分。 DOH !!

@interface SignInViewController : UIViewController
< ZBarReaderViewDelegate >
{
ZBarReaderView *readerView;
UITextView *resultText;
}