IOS AV Foundation QR code scan 01 using Webcam

Source: Internet
Author: User

From this section, we use AV Foundation to make a barcode (not just a QR code) scanner, in addition to scanning the QR code function, but also with speech synthesis and camera zoom function.

Create a single view application named COLLOQR. Open storyboard, disable sized class. Select the view controller and put it into the navigation controller via the editor menu. The last modified title is COLLOQR:


using the camera

Open viewcontroller.m, add import:

@import avfoundation;
The following instance variables are defined in the implementation:

@implementation Viewcontroller {    avcapturesession *_capturesession;    Avcapturedevice *_videodevice;    Avcapturedeviceinput * _VIDEOINPUT;    Avcapturevideopreviewlayer *_previewlayer;    BOOL _running;}
The core class in avcapturesession:avfoundation for acquiring, processing, and outputting video through hardware. A capture session consists of multiple inputs and multiple outputs, and controls the format and resolution of the output frame.

Avcapturedevice: Encapsulates the physical camera on the device. There are two cameras in front of the iphone.

Avcapturedeviceinput: To add a avcapturedevice to the session, you need to wrap it with avcapturedeviceinput.

Avcapturevideopreviewlayer: Used to display the video captured by the camera to the UI.

_running: Used to store the session state, indicating whether the session is running or is in a stopped state.

Add the following methods:

-(void) Setupcapturesession {    if (_capturesession) return;        _videodevice = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];    if (!_videodevice) {        NSLog (@ "No video camera on this device!");        return;    }        _capturesession = [[Avcapturesession alloc] init];        _videoinput = [[Avcapturedeviceinput alloc] Initwithdevice:_videodevice Error:nil];        if ([_capturesession canaddinput:_videoinput])    {        [_capturesession addinput:_videoinput];    }        _previewlayer = [[Avcapturevideopreviewlayer alloc] initwithsession:_capturesession];    _previewlayer.videogravity = Avlayervideogravityresizeaspectfill;}
This method is used to create a capture session.

    • If the session already exists, it is returned directly.
    • Initializes the video device and returns directly if the device does not have a webcam.
    • Initializes the capture session.
    • Create video input from video device.
    • Query session whether to accept an input, if accepted, add input to the session.
    • Finally, create a preview layer and assign it a capture session to preview.

Create a preview view

Open Storyboard, add a uiview to the view controller, and fill it with the entire view. and add a outlet named Previewview to it.

Go back to VIEWCONTROLLER.M, modify the Viewdidload method, create the capture session, set the preview layer, fill it with the view that contains it, and set it as a child layer of its container view.

-(void) viewdidload {    [super viewdidload];        [Self setupcapturesession];    _previewlayer.frame = _previewview.bounds;    [_previewview.layer Addsublayer:_previewlayer];}
Add the following two methods to start and stop snapping:

-(void) startrunning{    if (_running)    {        return;    }    [_capturesession startrunning];    _running = YES;} -(void) stoprunning{    if (!_running)    {        return;    }    [_capturesession stoprunning];    _running = NO;}
Add two additional methods to start capture before the view is displayed, and stop capture when the app enters the background:

-(void) Viewwillappear: (BOOL) animated{    [Super viewwillappear:animated];    [Self startrunning];} -(void) Viewwilldisappear: (BOOL) animated{    [Super viewwilldisappear:animated];    [Self stoprunning];}
To modify the Viewdidload method, register the following two notifications:

    [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (applicationwillenterforeground:) Name: Uiapplicationwillenterforegroundnotification Object:nil];        [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (applicationdidenterbackground:) Name: Uiapplicationdidenterbackgroundnotification Object:nil];
To implement the notification processing method:

-(void) Applicationwillenterforeground: (nsnotification *) note{    [self startrunning];} -(void) Applicationdidenterbackground: (nsnotification *) note{    [self stoprunning];}
Compile, execute on the real machine (the emulator does not support the camera) the effect is as follows:



In the next section, we will add a sweep code function to the program.

Reprint Please specify source: http://blog.csdn.net/yamingwu/article/details/44498123


IOS AV Foundation QR code scan 01 using Webcam

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.