IOS scan QR Code, barcode implementation

Source: Internet
Author: User

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface Viewcontroller () <AVCaptureMetadataOutputObjectsDelegate>
@property (Weak, nonatomic) Iboutlet UILabel *capturelabel;
@property (strong,nonatomic) avcapturesession *session; Capture session
@property (strong,nonatomic) Avcapturevideopreviewlayer *previewlayer; The layer generated by the QR code
@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.
}

-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
#pragma mark-read QR code
-(void) Readqrcode
{
1. Camera devices
Avcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];

2. Set input
Because the simulator doesn't have a camera, it's best to make a judgment here.
Nserror *error = nil;
Avcapturedeviceinput *input = [Avcapturedeviceinput deviceinputwithdevice:device error:&error];
if (Error) {
NSLog (@ "No webcam-%@", error.localizeddescription);
Return
}

3. Set output (metadata meta data)
Avcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc] init];
3.1 Setting the proxy for the output
Description: Using the main thread queue, the corresponding synchronization, the use of other queues, corresponding to different steps, it is easy for users to produce a bad experience setmetadataobjectsdelegate
[Output Setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()];

4. Shooting session
Avcapturesession *session = [[Avcapturesession alloc] init];
Add input and output to session
[Session Addinput:input];
[Session Addoutput:output];

4.1 Setting the format of the output
Tip: Be sure to specify the output metadata type after you set the output of the session to output!
[Output SETMETADATAOBJECTTYPES:@[AVMETADATAOBJECTTYPEQRCODE]];

5. Set up a preview layer (to allow users to see the scan condition)
Avcapturevideopreviewlayer *preview = [Avcapturevideopreviewlayer layerwithsession:session];

5.1 Setting the properties of the preview layer
[Preview Setvideogravity:avlayervideogravityresizeaspectfill];

5.2 Setting the size of the preview layer
[Preview SetFrame:self.view.bounds];

5.3 Adding layers to a view's layer
[Self.view.layer Insertsublayer:preview atindex:0];
Self.previewlayer = preview;

6. Start session
[Session startrunning];

Self.session = session;
}

#pragma mark-Output proxy method
This method is recognized in the QRCode, and the conversion is completed
If the content of the QRCode is larger, the longer the conversion takes
-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection
{
Frequent scans, invoking proxy methods
1. If the scan is complete, stop the session
[Self.session stoprunning];
2. Delete a preview layer
[Self.previewlayer Removefromsuperlayer];

NSLog (@ "%@", metadataobjects);

3. Set interface to display scan results
if (Metadataobjects.count > 0) {
Avmetadatamachinereadablecodeobject *obj = metadataobjects[0];
Tip: If you need to scan for information such as URLs or business cards, you can expand here!
_capturelabel.text = Obj.stringvalue;
}} @end

IOS scan QR Code, barcode implementation

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.