iOS development--ios7 (and later) SDK comes with QR code (including barcode) sweep code, QR code generation

Source: Internet
Author: User

Now many apps are involved in the QR code sweep function, this function is simple and practical, in many cases the user is happy to use, now this article brings iOS7 QR code to sweep the code of the tutorial, also includes scanning barcode, enough to meet the simple scanning code requirements, and avoid the use of third-party cumbersome.

I need to generate a QR code in the Post project, I found a handy lightweight open source Library on git and recommended it to friends who need it: Https://github.com/moqod/ios-qr-code-encoder

Here's how to use the IOS7 SDK self-code API.

First, to introduce the avfoundation framework:

1 #import <AVFoundation/AVFoundation.h>

Then complete the object of the related class to be used for the sweep code:

1 @property (strong,nonatomic) avcapturedevice *device;2 @property (strong,nonatomic) avcapturedeviceinput *input;3 @ Property (strong,nonatomic) avcapturemetadataoutput *output;4 @property (strong,nonatomic) avcapturesession *session; 5 @property (strong,nonatomic) Avcapturevideopreviewlayer *previewlayer;

Initialization

 1//Device 2 _device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo]; 3//Input 4 nserror *error = nil; 5 _input = [Avcapturedeviceinput deviceInputWithDevice:self.device error:&error];  6 if (Error) {7 NSLog (@ "Initialize input device Failed"); 8} 9//Output10 _output = [[Avcapturemetadataoutput alloc]  init];11 [_output setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()];12//Session13 _session = [[Avcapturesession alloc] init];14 [_session setsessionpreset:avcapturesessionpresethigh];15//Add input/Output [_session CanAddInput:self.input])         [_session addinput:self.input];19}20 if ([_session canAddOutput:self.output]) 21 {22 [_session addoutput:self.output];23}24//barcode type Avmetadataobjecttypeqrcode is a QR code, if you need to scan the barcode, it is necessary to add an enumeration in this array, command point two The dimension code is enumerated, and you will see other required barcode types, such as upc,code39,code128, and use the _output.metadataobjecttypes as appropriate [email protected][ AvmetadataobjecttypeqRCODE];26//Preview _previewlayer =[avcapturevideopreviewlayer layerwithsession:self.session];29 _p reviewlayer.videogravity = avlayervideogravityresizeaspectfill;30 _previewlayer.frame = self.view.bounds;31 [self. View.layer InsertSublayer:self.previewLayer atindex:0];
32//Start sweep code
33[_session startrunning];

Implement proxy Avcapturemetadataoutputobjectsdelegate:

1 #pragma mark-avcapturemetadataoutputobjectsdelegate methods 2//sweep to the code, this proxy method will tell the result of the sweep code 3-(void) Captureoutput: (Avcap Tureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects fromconnection: (AVCaptureConnection *) connection {4      5     nsstring *stringvalue = nil, 6     if ([metadataobjects Count] >0) 7     {8         Avmetadata Machinereadablecodeobject * MetadataObject = [metadataobjects objectatindex:0]; 9         stringvalue = metadataobject.stringvalue;10     }11     //Sweep code successful, stop scanning session layer activity     [_session stoprunning];13     -     NSLog (@ "%@", stringvalue); 15}

The above is iOS7 after the SDK native scan code, the use is relatively simple.

iOS development--ios7 (and later) SDK comes with QR code (including barcode) sweep code, QR code generation

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.