IOS native QR code scan (can limit scan area)

Source: Internet
Author: User

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

Transmission Door

The following respectively create their

Device

_device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];

Input

_input = [Avcapturedeviceinput deviceinputwithdevice:self. device Error:nil];

Output

_output = [[Avcapturemetadataoutput alloc] init];

[_output setmetadataobjectsdelegate:self Queue:dispatch_get_main_queue ()];

Session

_session = [[Avcapturesession alloc] init];

[_session Setsessionpreset:avcapturesessionpresethigh];

if ([_session canaddinput:self. Input])

{

[_session addinput:self. Input];

}

if ([_session canaddoutput:self. Output])

{

[_session addoutput:self. Output];

}

Barcode Type Avmetadataobjecttypeqrcode

_output. Metadataobjecttypes = @[Avmetadataobjecttypeqrcode];

Preview

_preview =[avcapturevideopreviewlayer layerwithsession: _session];

_preview. videogravity = Avlayervideogravityresizeaspectfill;

_preview. frame = self. View. Layer. bounds;

[Self. View. Layer Insertsublayer: _preview atindex:0];

Start

[_session startrunning];

Then implement Avcapturemetadataoutputobjectsdelegate

#pragma Mark Avcapturemetadataoutputobjectsdelegate

-(void) Captureoutput: ( Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects fromconnection: ( Avcaptureconnection *) connection

{

NSString *stringvalue;

if ([metadataobjects count] > 0)

{

Stop scanning

[_session stoprunning];

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

In general, the scan page in the project is like this, but when you scan you will find that the QR code has not entered the center of the small box, it has been successfully scanned, which is not good for the experience. But since then there is no time to optimize the project. At last, I took out the time today.

I have been working from morning to afternoon, I have tried all the methods I thought of, but none (all tears), and finally will give up the time to find a more suspicious point.

@property (nonatomic) cgrect rectofinterest Ns_available_ios (7 _0);

This is a property of Avcapturemetadataoutput, and its interpretation is

@discussion

The value of this property was a cgrect that determines the receiver'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 deviceproviding 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), wouldn't that be to set the scan range.

So hurriedly set the rectofinterest into the frame of the middle frame,

[_output Setrectofinterest: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

[_output Setrectofinterest:cgrectmake (((ScreenWidth-220)/2)/ScreenWidth, (60+)/Screenhigh, 220/SCREENW Idth, 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.

[_output Setrectofinterest:cgrectmake ((+)/Screenhigh, ((ScreenWidth-220)/2)/ScreenWidth, 220/SCREENW Idth, 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.

[_output Setrectofinterest: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!

Today to share with you this point, reproduced please indicate the source, thank you--LC

IOS native QR code scan (can limit scan area)

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.