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

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{    //For responding to scan events, click Start Scan    button = [UIButton buttonwithtype:uibuttontyperoundedrect];    [Button Setframe:cgrectmake (max, Max, Max)];    [Button settitle:@ "Scan" forstate:uicontrolstatenormal];    [Button addtarget:self action: @selector (scanbuttonpressed:) forcontrolevents:uicontroleventtouchupinside];    [Self.view Addsubview:button];    Used to display scanned images    ImageView = [[Uiimageview alloc]initwithframe:cgrectmake (280, 280)];    [Self.view Addsubview:imageview];    Used to display the scanned information    label = [[UILabel alloc]initwithframe:cgrectmake (356, 280, and)];    [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 via 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 (ten, +, +);    Readerview.readerdelegate = self;    Scanning area    CGRect scanmaskrect = CGRectMake (Cgrectgetmidy (readerview.frame)-126, (+);    Processing simulator    if (target_iphone_simulator) {        zbarcamerasimulator *camerasimulator        = [[Zbarcamerasimulator Alloc]initwithviewcontroller:self];        Camerasimulator.readerview = Readerview;    }    [Self.view Addsubview:readerview];    Scan area calculation    Readerview.scancrop = [self getscancrop:scanmaskrect readerViewBounds:readerView.bounds];    Call the Start method of Zbarreaderview to start 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 *) symbols FromImage: (UIImage *) image
{
For (Zbarsymbol *symbol in symbols)
{ NSLog (@ "%@", symbol.data);
To display information on the lable
Label.text = Symbol.data;
Break
}

[Readerview stop];
}

There is another point to note, QR Code scan page and other pages of the jump as far as possible to use

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!

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.