Use Zbar scan QR code to customize scanning interface in iOS

Source: Internet
Author: User

previously used in Android zxing identification QR code, zxing also has the corresponding iOS version, after understanding, Zbar is also a common QR code recognition software, and provides the iOS and Android SDK is available to use, Finally, I chose Zbar for two-dimensional code recognition, and its annotations are clear and easy to use.

Zbar provides us with two ways of using a direct call to the Zbarreaderviewcontroller provided by Zbar to open a scanning interface, and the other is to use Zbar provides zbarreaderview that can be embedded in other views, In actual projects we are more likely to use the second approach, which allows us to customize the interface more.

Zbar is also very simple to use, import ZBARSDK into your project, and import ZBarSDK.h header files in files that need to use Zbar.

#pragma mark initialization Scan-(void) initscan{Readview = [Zbarreaderview new];    Readview.backgroundcolor = [Uicolor Clearcolor];    Readview.frame = CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height);    Readview.readerdelegate = self;    Readview.allowspinchzoom = yes;//use gesture zoom Readview.trackingcolor = [Uicolor Redcolor];  Readview.showsfps = no;//Display frame rate YES shows no not display//readview.scancrop = CGRectMake (0, 0, 1, 1);//area of the image to be scanned UIImage    *hbimage=[uiimage imagenamed:@ "Pick_bg.png"];    Scanzomeback=[[uiimageview alloc] initwithimage:hbimage]; Add a background picture CGRect Mimagerect=cgrectmake ((readview.frame.size.width-200)/2.0, (readview.frame.size.height-200)/2.0    , 200, 200);    [Scanzomeback Setframe:mimagerect]; Readview.scancrop = [Self getscancrop:mimagerect readerviewbounds:readview.bounds];//the area of the image to be scanned [Readview AddSubvie    W:scanzomeback];    [Readview Addsubview:readlineview];    [Self.view Addsubview:readview];    [Readview start]; }

#pragma mark gets the scan area-(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);}

#pragma mark scan Animation-(void) loopdrawline{cgrect rect = CGRectMake (scanzomeback.frame.origin.x, ScanZomeBack.frame.origin    . Y, ScanZomeBack.frame.size.width, 2);    if (Readlineview) {[Readlineview Removefromsuperview];    } Readlineview = [[Uiimageview alloc] initwithframe:rect];    [Readlineview setimage:[uiimage imagenamed:@ "Line.png"]; [UIView animatewithduration:3.0 delay:0.0 Options:uiviewanimationoptionc Urveeasein animations:^{//Modify Fream code to write here Readlineview . Frame =cgrectmake (scanzomeback.frame.origin.x, Scanzomeback.frame.origin.y+scanzomeback.frame.size.height,                         ScanZomeBack.frame.size.width, 2);                                              [Readlineview setanimationrepeatcount:0];                                                     } completion:^ (BOOL finished) {if (!is_anmotion) {     [Self loopdrawline];        }                                              }];    [Readview Addsubview:readlineview]; #pragma mark gets the scan result-(void) Readerview: (Zbarreaderview *) Readerview didreadsymbols: (Zbarsymbolset *) symbols FromImage    :(UIImage *) image{//Get scanned Barcode Content const zbar_symbol_t *symbol = Zbar_symbol_set_first_symbol (Symbols.zbarsymbolset);    NSString *symbolstr = [nsstring stringwithutf8string:zbar_symbol_get_data (symbol)];        if (zbar_symbol_get_type (symbol) = = Zbar_qrcode) {//is QR code} for (Zbarsymbol *symbol in symbols) {        [Stxtfield SetText:symbol.data];    Break    } [Readerview stop];        [Readerview Removefromsuperview]; }


Use Zbar scan QR code to customize scanning interface 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.