IOS system QR Code scan (can limit scan area)

Source: Internet
Author: User

Use the Avfoundation system library to scan the QR code and limit the scope of the QR code. (Because the default is full screen scanning)

-(void) Begincode

{

1. Camera Device

Avcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];

/*

Media types

Avmediatypevideo

Avmediatypeaudio

Avmediatypetext

Avmediatypeclosedcaption

Avmediatypesubtitle

Avmediatypetimecode

Avmediatypemetadata

Avmediatypemuxed

*/

2. Setting the input

/**

* Set input This method needs to be judged because the simulator does not have a webcam

*

* @param the camera as the input device

*

* @return return Avcaptureinput

*/

Nserror *error = nil;

Avcaptureinput *input = [Avcapturedeviceinput deviceinputwithdevice:device error:&error];

if (Error) {

NSLog (@ "No webcam%@", error);

Return

}

3. Set output (Metdata metadata)

Avcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc] init];

3.1 Output of the proxy captures the image of the two-dimensional code

Dispatch_get_main_queue () uses the main thread queue, responds to comparison synchronization, and responds differently with other queues.

[Output Setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()];

3.2 Setting scan area By default is full screen scan

This cgrectmake (y,x,h,w) 1 means that the maximum origin is the start point in the lower right corner of the navigation

[Output setrectofinterest:cgrectmake (0, 0.5, 0.5, 0.5)];//upper left corner 1/4 screen

[Output Setrectofinterest:cgrectmake (0.5, 0.5, 0.5, 0.5)];//lower left corner 1/4 screen

[Output Setrectofinterest:cgrectmake (0.5, 0, 0.5, 0.5)]; Lower right corner 1/4 screen

[Output setrectofinterest:cgrectmake (0, 0, 0.5, 0.5)]; Top Right corner 1/4 screen

[Output Setrectofinterest:cgrectmake (124)/screenhigh, ((ScreenWidth220)/2)/screenwidth,220/screenhigh,220 /screenwidth)]; Set the location of a custom pixel point

[Output Setrectofinterest:cgrectmake (0.25,0.25, 0.5, 0.5)]; Looks like the middle of the feeling!!!

4. Shooting Sessions

Avcapturesession *session = [[Avcapturesession alloc] init];

_session = session;

Add input and output to session

[Session Addinput:input];

[Session Addoutput:output];

4.1 Setting the format of the output

[Output SETMETADATAOBJECTTYPES:@[AVMETADATAOBJECTTYPEQRCODE]];

5. Set the preview layer (let the user see the scan results)

Avcapturevideopreviewlayer *preview = [Avcapturevideopreviewlayer layerwithsession:session];

_previewlayer = preview;

5.1 Setting the properties of the preview

preview.videogravity = 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];

6. Start the session

[Session startrunning];

}

Proxy method for #pragma mark output

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

{

Will scan frequently.

Stop if the scan is complete

[_session stoprunning];

Delete a previewed layer

[_previewlayer Removefromsuperlayer];

Set interface to display scan results

NSString *reslutstr = [[NSString alloc] init];

if (Metadataobjects.count > 0) {

Avmetadatamachinereadablecodeobject *obj = metadataobjects[0];

Reslutstr = [obj stringvalue]; //This is the result of the scan

If you need to scan for information such as a URL card to expand again

}

NSLog (@ "%@", metadataobjects);

}

IOS system 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.