Use ZBar in iOS to scan the QR code and customize the scan interface

Source: Internet
Author: User

Use ZBar in iOS to scan the QR code and customize the scan interface
ZXing has been used in Android to identify QR codes. ZXing also has the corresponding iOS version. After understanding it, ZBar is also a common QR code recognition software, the sdks for iOS and Android are provided for use. Finally, I chose ZBar for QR code identification. The annotations are clear and easy to use.

ZBar provides two usage methods. One is to directly call ZBarReaderViewController provided by ZBar to open a scanning interface, another method is to use ZBarReaderView provided by ZBar that can be embedded in other views. In actual projects, we are more likely to use the second method, this allows us to customize more interfaces.

ZBar is also very simple to use. Import ZBarSDK to the project and import the ZBarSDK. h header file to the file that requires 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 show NO not show // readview. scanCrop = CGRectMake (0, 0, 1, 1); // UIImage * hbImage = [UIImage imageNamed: @ "pick_bg.png"]; scanZomeBack = [[UIImageView alloc] initWithImage: hbImage]; // Add a background image 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 region of the image to be scanned [readview addSubview: scanZomeBack]; [readview addSubview: readLineView]; [self. view addSubview: readview]; [readview start];}

# Pragma mark obtain 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: UIViewAnimationOptio NCurveEaseIn animations: ^ {// write the fream code here in 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 obtains scan results-(void) readerView :( ZBarReaderView *) readerView didReadSymbols :( ZBarSymbolSet *) symbols fromImage :( UIImage *) image {// get the scanned barcode content const zbar_symbol_t * symbol = zbar_symbol_set_first_symbolset. zbarSymbolSet); NSString * symbolStr = [NSString stringwithuf8string: zbar_symbol_get_data (symbol)]; if (zbar_symbol_get_type (symbol) = ZBAR_QRCODE) {// QR code} for (ZBarSymbol * symbol in symbols) {[sTxtField setText: symbol. data]; break;} [readerView stop]; [readerView removeFromSuperview];}


Related Article

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.