Use Zbar to scan QR codes and barcodes in iOS

Source: Internet
Author: User

Recently did a outsourcing project, which used the QR code scanning and payment! Is familiar with is zxing, but in Xcode7.1 inside found unexpectedly inexplicable not support, wood has method, from on-line check also has a support QR code scan of thing, right is next I want to say of East, QR Code scan of sharp weapon zbarsdk. Gossip less ramble, to come!

1, download ZBARSDK official website Https://github.com/bmorton/ZBarSDK

2. Import the following frame

650) this.width=650; "src=" http://img.blog.csdn.net/20140911152135125 "style=" margin:0px;padding:0px;border:0px; " />

3. #import "ZBarSDK.h" in Appdelegetem file

And in

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions

Add the following code inside

Two-dimensional code reading

[Zbarreaderviewclass];

4, in the QR Code scanning interface began #import "ZBarSDK.h", add

<ZBarReaderDelegate,ZBarReaderViewDelegate> Commission

Here's how to start the scanning process:
1. Get pictures from the camera
2. Identify images with Zbar's internal processing method
3. Process the information and display

Code implementation:

1. Create a View

-(void) creatview{    //to respond to scan events, click Start Scan     button = [uibutton  buttonwithtype:uibuttontyperoundedrect];    [button setframe:cgrectmake (110, &NBSP;200,&NBSP;100,&NBSP;40)];    [button settitle:@ "Scan"  forState: Uicontrolstatenormal];    [button addtarget:self action: @selector ( scanbuttonpressed:)  forcontrolevents:uicontroleventtouchupinside];    [self.view The  addsubview:button];    //is used to display the scanned image     imageview = [[ Uiimageview alloc]initwithframe:cgrectmake (20, 50, 280, 280)];    [ The self.view addsubview:imageview];    //is used to display the scanned information     label =  [[uilabel alloc]initwithframe:cgrectmake (20, 356, 280, 68)];     [Self.view addsubview:label];}

2. Scan QR code

Method One: Directly invoke the Zbarreaderviewcontroller provided by Zbar to open a scanning interface

Step1: Scan QR code operation

-(void) scanbuttonpressed: (ID) sender{Zbarreaderviewcontroller *reader = [[Zbarreaderviewcontroller alloc]init];    Reader.readerdelegate = self;    Reader.supportedorientationsmask = Zbarorientationmaskall;    Zbarimagescanner *scanner = Reader.scanner;    [Scanner setsymbology:zbar_i25 config:zbar_cfg_enable to:0]; [Self Presentviewcontroller:reader animated:yes completion:nil];}

Step2: The corresponding method of zbarreaderdelegate is executed when the QR code callback is found.

-   (void)  imagePickerController:  (uiimagepickercontroller*)  reader  didfinishpickingmediawithinfo:  (nsdictionary*)  info{    //get results with info      id<nsfastenumeration> results =    [info objectforkey:  ZBarReaderControllerResults];    ZBarSymbol *symbol = nil;     for (symbol in results)         break;   <span style= "White-space:pre" ></span>    imageview.image =     [info objectForKey: UIImagePickerControllerOriginalImage];     //Scan interface exit     [reader dismissviewcontrolleranimated:yes completion:nil];} 

Method Two: Use Zbar to provide a zbarreaderview that can be embedded in other views

Step1: Scan QR code operation

-(void) scanbuttonpressed: (ID) sender{    zbarreaderview *readerview = [[ Zbarreaderview alloc]init];    readerview.frame = cgrectmake (10, 44, &NBSP;300,&NBSP;300);    readerview.readerdelegate = self;     //Scan Area     cgrect scanmaskrect = cgrectmake (60, CGRectGetMidY ( Readerview.frame)  - 126, 200, 200)     //processing simulator      if  (Target_iphone_simulator)  {        zbarcamerasimulator  *camerasimulator        = [[zbarcamerasimulator alloc] initwithviewcontroller:self];        camerasimulator.readerview =  readerView;    }    [self.view addSubview:readerView];     //Scan Area MeterCount     readerView.scanCrop = [self getScanCrop:scanMaskRect  readerviewbounds:readerview.bounds];    //Call Zbarreaderview Start method to begin scanning      [readerview start];}

Step2: Scan area calculation

-(CGRect) Getscancrop: (cgrect) rect readerviewbounds: (cgrect) readerviewbounds{cgfloat x,y,width,height;    x = Rect.origin.x/readerviewbounds.size.width;    y = rect.origin.y/readerviewbounds.size.height;    width = rect.size.width/readerviewbounds.size.width;        Height = rect.size.height/readerviewbounds.size.height; return CGRectMake (x, y, width, height);}

Step3: The corresponding method of zbarreaderviewdelegate is executed when scanning to the QR code callback

-   (void) Readerview: (zbarreaderview *) Readerview didreadsymbols: (zbarsymbolset *) s  FromImage: (uiimage *) image{     for  (zbarsymbol *symbol in symbols )  {        nslog (@ "%@",  symbol.data);         //Display information on lable           Label.te xt =  symbol.data ;          break;      }    //Call the Stop method of Zbarreaderview to stop     [readerview  stop];} Another point to note, QR Code scan page and other pages between the jump as far as possible with the 

SAOMIAOVC *saomiao = [[[SAOMIAOVC alloc]init]autorelease];

Saomiao.hidesbottombarwhenpushed=yes;

saomiao.delegate=self;

Uinavigationcontroller *nav= [[[[Uinavigationcontroller alloc] Initwithrootviewcontroller:saomiao] autorelease];

[Self Presentviewcontroller:nav animated:yes completion:nil]; This way, because it is easy to use push to quit or destroy a variety of problems!


This article is from the "iOS Baidu map" blog, please be sure to keep this source http://2618195.blog.51cto.com/2608195/1616928

Use Zbar to scan QR codes and barcodes in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.