"Go" iOS native QR code scan (can limit scan area)

Source: Internet
Author: User

After using Avfoundation to complete the scan code, there are 2 problems:

1, how to limit the scan range?

2. How is the barcode scanned?

A friend's article helped me, specially transferred, can help the needy friend.

Original: http://www.2cto.com/kf/201411/356046.html

The main reason for writing this article is not to show how to use avfoundation for QR code scanning, but also to limit the scope of scanning QR code. (Because the default is full screen scanning)

The project encountered the function of scanning QR code, here I gave up the use of the three-party library, and the introduction of Apple native scan.

The advantage of native is that the scan is particularly fast and efficient, but the problem is that you don't know how to limit the scan range.

Let's just say a little bit about how to use it to scan the QR code.


First, there are several classes to use.

@property (strong,nonatomic) avcapturedevice * device;

@property (strong,nonatomic) avcapturedeviceinput * input;

@property (strong,nonatomic) avcapturemetadataoutput * OUTPUT;

@property (strong,nonatomic) avcapturesession * session;

@property (strong,nonatomic) avcapturevideopreviewlayer * preview;

The relationship between them can be seen in the following article

Portal


The following respectively create their

Device

_device = [Avcapturedevicedefaultdevicewithmediatype:avmediatypevideo];

Input

_input = [AVCaptureDeviceInputdeviceInputWithDevice:self.deviceerror:nil];

Output

_output = [[Avcapturemetadataoutputalloc]init];

[_outputsetmetadataobjectsdelegate:selfqueue:dispatch_get_main_queue ()];


Session

_session = [[Avcapturesessionalloc]init];

[_sessionsetsessionpreset:avcapturesessionpresethigh];

if ([_sessioncanaddinput:self.input])

{

[_sessionaddinput:self.input];

}

if ([_sessioncanaddoutput:self.output])

{

[_sessionaddoutput:self.output];

}

Barcode Type Avmetadataobjecttypeqrcode

_output.metadataobjecttypes [email Protected][avmetadataobjecttypeqrcode];


Preview

_preview =[avcapturevideopreviewlayerlayerwithsession:_session];

_preview.videogravity =avlayervideogravityresizeaspectfill;

_preview.frame =self.view.layer.bounds;

[self.view.layerinsertsublayer:_previewatindex:0];


Start

[_sessionstartrunning];


Then implement Avcapturemetadataoutputobjectsdelegate

#pragma Mark Avcapturemetadataoutputobjectsdelegate

-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection

{

NSString *stringvalue;

if ([Metadataobjectscount] >0)

{

Stop scanning

[_sessionstoprunning];

Avmetadatamachinereadablecodeobject * MetadataObject = [metadataobjectsobjectatindex:0];

StringValue = Metadataobject.stringvalue;

}


}


At this point you can successfully scan the QR code, but there is an embarrassing problem, then the scan is full-screen scanning. That

vc/czuejrldry/nt0m/rtb21xle9t6i2vmruwcvsu7hpo6y1q8rhtryyu9dqo6i2vmrhwogjqaos1+ 66872r0qq3xcb6tctksbryt6lp1shl0ru49rhivc+/ydljtcs146gjpc9wpgo8cd48yni+cjwvcd4kpha+ pc9wpgo8cd5achjvcgvydhkobm9uyxrvbwljkunhumvjdcbyzwn0t2zjbnrlcmvzdapou19bvkfjtefctevfsu9tkddfmck7pc9wpgrv4srhtcqgqvzdyxb0d xjltwv0ywrhdgfpdxrwdxqg0ru49sr00nsjrmv8tcs94srnysckcjxwpgpazglzy3vzc2lvbjwvcd4kpha+ Clrozsb2ywx1zsbvzib0aglzihbyb3blcnr5iglzigegq0dszwn0ihroyxqgzgv0zxjtaw5lcyb0agugcmvjzwl2zxi= "s Rectangle of Interest for each frame of video.

The rectangle's origin is top left and are relative to the coordinate space of the device providing the metadata. Specifying

A rectofinterest may improve detection performance for certain types of metadata. The default value of the

Value CGRectMake (0, 0, 1, 1). Metadata objects whose bounds do no intersect with the rectofinterest won't be returned.


Probably means to set the area of interest for each frame (literally), that is not to set the scan range, exultation then hurriedly set the rectofinterest into the frame of the middle frame,

[_outputsetrectofinterest:cgrectmake (ScreenWidth-220)/2,60+64,220,220)];

The width and height of the middle area are all screenwidth for the device screen.

But I found out how the scan was not successful. So I looked at the above paragraph again.
The second sentence: The origin of the area in the upper left (behind only know the Pit bitter me!) ), and then the area is relative to the size of the device, the default value is CGRectMake (0, 0, 1, 1), this time I know that there is a proportional relationship, the maximum is 1, that is, just divided by the corresponding device width and height of the size of the line? And then it's changed into

[_outputsetrectofinterest:cgrectmake ((ScreenWidth-220)/2)/screenwidth, (60+64)/screenhigh,220/screenwidth,220/ Screenhigh)];


Supposedly this should be perfect, but only to know that I am still happy too early, a scan only to find that it is not so much, a lot worse.

So I was 1.1 points, but finally did not adjust the success, the last cruel have set a very definite value.

[_output Setrectofinterest:cgrectmake (0.5,0.5,0.5, 0.5)];


This time should be very sure is in the bottom right of the One-fourth area bar, hey.

But the fact that hit me again, the scan found that the lower left One-fourth area, that is, the origin of Rectofinterest is the upper right corner!!!

Looking back again, even if the upper right corner is the origin that should not affect AH, but why not, is not the origin of the X and Y interchange it? Forget it or whatever, give it a try.

[_outputsetrectofinterest:cgrectmake (60+64)/screenhigh, ((ScreenWidth-220)/2)/screenwidth,220/screenwidth,220/ Screenhigh)];


I scanned it again and found it! The original point is correct, I just want to say tmd!

But what about the width and height? No, it's not interchangeable! Give it a try.

[_outputsetrectofinterest:cgrectmake (124)/screenhigh, ((ScreenWidth-220)/2)/screenwidth,220/screenhigh,220/ ScreenWidth)];

With a disturbed mood and try again, the perfect scan! OMG I've got a heart to die for.

So using the system native scan QR code is perfect!


"Go" iOS native QR code scan (can limit scan area)

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.