How to use ZBAR Barcode / QR Scanner In iPhone app?

  • First import ZBAR sdk in app.
  • Declare below method  in app delegate.m at the end of “didFinishLaunchingWithOptions” method.

[ZBarReaderView class];

  • Import below class in viewcontroller.h in which you want to implement scanner.

#import “ZBarReaderView.h”

#import “ZBarCameraSimulator.h”

  • Declare Below variables in viewcontroller.h

ZBarReaderView *readerView;

ZBarCameraSimulator *cameraSim;

  • Now, declare the scanner Method in viewcontroller.m file.

 

-(IBAction)Scanner:(UIButton*)sender

{

readerView = [ZBarReaderView new];

ZBarImageScanner * scanner = [ZBarImageScanner new];

[scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0];

readerView = [readerView initWithImageScanner:scanner];

readerView.readerDelegate = self;

readerView.tracksSymbols = YES;

readerView.trackingColor = [UIColor redColor];

readerView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

readerView.torchMode = 0;

dispatch_async(dispatch_get_main_queue(), ^{[readerView start];});

UIButton *btncancle = [[UIButton alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-50, 100, 40)];

[btncancle setImage:[UIImage imageNamed:@”cancel.png”] forState:UIControlStateNormal];

btncancle.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

[btncancle setTitle:@”Cancle” forState:UIControlStateNormal];

[btncancle addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

[readerView addSubview:btncancle];

[self.view addSubview:readerView];

}

 

– (void) readerView: (ZBarReaderView*) view  didReadSymbols: (ZBarSymbolSet*) syms  fromImage: (UIImage*) img

{

// do something useful with results

for(ZBarSymbol *sym in syms)

{

[readerView stop];

NSLog(@”sym.date:%@”,sym.data);

// set values in textfield or label

[self performSelector:@selector(removeQr) withObject:nil afterDelay:1.0];

break;

}

}

 

-(IBAction)buttonClicked:(UIButton*)sender

{

[readerView stop];

[readerView removeFromSuperview];

}

 

-(void)removeQr

{

[readerView removeFromSuperview];

}