iOS7 new API for scanning QR code

Source: Internet
Author: User

Originally used in the Zbar Open Source Library to implement the scanning QR code, but seemingly do not support arm64, nor in the update.

Now do not fit iOS7 below, and iOS new system API has supported scanning code, refer to a foreigner blog to do a demo, you need to refer to the following

Reference Blog: http://www.appcoda.com/qr-code-ios-programming-tutorial/


#import <AVFoundation/AVFoundation.h> @interface qrcodereadcontroller:baseviewcontroller< Avcapturemetadataoutputobjectsdelegate> @property (Weak, nonatomic) Iboutlet UIView *viewpreview; @end
Add a Viewpreview to the xib to dynamically display the contents of the captured camera when scanning the code

@interface Qrcodereadcontroller () {Nsinteger maxy;    Nsinteger Miny;        Nstimer *timer; Uiimageview *line;} @property (nonatomic) BOOL isreading; @property (nonatomic, strong) avcapturesession *capturesession; @property ( Nonatomic, Strong) Avcapturevideopreviewlayer *videopreviewlayer;-(BOOL) startreading;-(void) stopReading;@ End@implementation qrcodereadcontroller-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *)    nibbundleornil{self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;}    -(void) viewdidload{[Super Viewdidload];    Do any additional setup after loading the view from its nib.    _isreading = NO;        if ([self startreading]) {Maxy = 280;        Miny = 2;  Line =[[uiimageview alloc]initwithframe:cgrectmake (0, 0, 280, 10)];        0-200 [Line setimage:[uiimage imagenamed:@ "E0"];                [_viewpreview Addsubview:line]; Timer = [NstimerSCHEDULEDTIMERWITHTIMEINTERVAL:1.0/40 target:self selector: @selector (move) Userinfo:nil Repeats:yes];    }; }/* * * Avcapturemetadataoutput object. This class in combination with the Avcapturemetadataoutputobjectsdelegate protocol would manage to intercept any metadata F Ound in the input device (meaning data in a QR code captured by our camera) and translate it to a human readable format.        */-(BOOL) startreading {nserror *error;    Avcapturedevice *capturedevice = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];        Avcapturedeviceinput *input = [Avcapturedeviceinput deviceinputwithdevice:capturedevice error:&error];        if (!input) {NSLog (@ "%@", [Error localizeddescription]);    return NO;    } _capturesession = [[Avcapturesession alloc] init];        [_capturesession Addinput:input];    Avcapturemetadataoutput *capturemetadataoutput = [[Avcapturemetadataoutput alloc] init];            [_capturesession Addoutput:capturemetadataoutput]; Dispatch_queue_t Dispatchqueue;    Dispatchqueue = Dispatch_queue_create ("Myqueue", NULL);    [Capturemetadataoutput setmetadataobjectsdelegate:self Queue:dispatchqueue];            [Capturemetadataoutput Setmetadataobjecttypes:[nsarray Arraywithobject:avmetadataobjecttypeqrcode]; Show to user what the camera of the device sees using a avcapturevideopreviewlayer _videopreviewlayer = [[Avcapturev    Ideopreviewlayer alloc] initwithsession:_capturesession];    [_videopreviewlayer Setvideogravity:avlayervideogravityresizeaspectfill];    [_videopreviewlayer Setframe:_viewpreview.layer.bounds];        [_viewpreview.layer Addsublayer:_videopreviewlayer];    [_capturesession startrunning]; return YES;} -(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects  Fromconnection: (avcaptureconnection *) connection{if (metadataobjects! = Nil && [metadataobjects count] > 0) {Avmetadatamachinereadablecodeobject *metadataobj =[Metadataobjects objectatindex:0]; if ([[[Metadataobj Type] isequaltostring:avmetadataobjecttypeqrcode]) {[Self performselectoronmainthread: @selec            Tor (stopreading) Withobject:nil Waituntildone:no];            NSLog (@ "metadataobj string =%@", [Metadataobj stringvalue]);        _isreading = NO;    }}}-(void) stopreading{[_capturesession stoprunning];        _capturesession = nil; [_videopreviewlayer Removefromsuperlayer];}    When scanning, move the scan line-(void) move{NSLog (@ + + +);  Static BOOL flag = TRUE;                                    True down and false up if (flag) {if (line.frame.origin.y <maxy) {line.frame = CGRectMake ( Line.frame.origin.x, Line.frame.origin.y +5, Line.fra        Me.size.width, Line.frame.size.height);            }else{flag =!flag;            if (_isreading&&[timer IsValid]) {[Timer invalidate];        }}}else {if (line.frame.origin.y >miny) {line.frame = CGRectMake ( Line.frame.origin.x, Line.frame.origin.y-5, Line.frame.size.width, Line.frame.        Size.Height);        }else {flag =!flag; }    }        }





iOS7 new API for scanning QR code

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.