IOS AV Foundation QR code scan 03 Add visualizations for scanned QR codes

Source: Internet
Author: User

In the previous section, we have implemented QR code scanning, which we use bounds and corners of metadata to achieve a visual scanning effect.

Bounds defines a rectangle that contains a two-D code image, and corners defines the actual coordinates of the two-dimensional code image:

The bounds and corners are the same when the camera and the QR code picture are completely aligned. But in general, it is almost impossible to completely align the camera with the QR code.

Open VIEWCONTROLLER.M, add the following instance variable to hold all detected QR code, the content of the QR code is indexed.

Nsmutabledictionary *_barcodes;
Initialize this dictionary in the Viewdidload method:

_barcodes = [Nsmutabledictionary new];
Defines a barcode class that holds metadata for a recognized QR code.

@interface Barcode:nsobject@property (nonatomic, strong) Avmetadatamachinereadablecodeobject *metadataObject;@ Property (Nonatomic, Strong) Uibezierpath *cornerspath; @property (nonatomic, strong) Uibezierpath *boundingboxpath;@ End@implementation Barcode@end
Add Processmetadataobject:

-(Barcode *) Processmetadataobject: (Avmetadatamachinereadablecodeobject *) code{//1 Barcode *barcode = _barcodes[co        De.stringvalue];        2 if (!barcode) {barcode = [barcode new];    _barcodes[code.stringvalue] = barcode;        }//3 Barcode.metadataobject = code;        Create the path joining code ' s corners//4 Cgmutablepathref Cornerspath = cgpathcreatemutable ();    5 Cgpoint Point;        Cgpointmakewithdictionaryrepresentation ((cfdictionaryref) code.corners[0], &point);        6 Cgpathmovetopoint (Cornerspath, Nil, point.x, POINT.Y); 7 for (int i = 1; i < Code.corners.count; i++) {cgpointmakewithdictionaryrepresentation (cfdictionaryref)        Code.corners[i], &point);    Cgpathaddlinetopoint (Cornerspath, Nil, point.x, POINT.Y);        }//8 Cgpathclosesubpath (Cornerspath);    9 Barcode.cornerspath =[uibezierpath Bezierpathwithcgpath:cornerspath]; Cgpathrelease (Cornerspath); Create the path for the code ' s bounding box//Barcode.boundingboxpath = [Uibezierpath bezierpathwithrect:        Code.bounds]; return barcode;}

    1. Query the Barcode object dictionary to see if there is a barcode of the same content already exists.
    2. If not, create a barcode object and add it to the dictionary.
    3. Stores the metadata for the QR code in the newly created barcode object.
    4. Creates a cornerspath that stores the four corner paths of the plotted QR code.
    5. Use Coregraphics to convert the coordinates of the first corner to a cgpoint instance.
    6. The path is drawn starting at the corner constructed from step fifth.
    7. Loop through the other three corners to create the appropriate path.
    8. After you draw the fourth point to the first point path, close the path.
    9. Creates a Uibezierpath object from Cornerspath and stores it in a barcode object.
    10. Create a border block from the Bezierpathwithrect: method.
    11. Returns the Barcode object.

To modify the CaptureOutput:didOutputMetadataObjects:fromConnection method:

-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects    Fromconnection: (avcaptureconnection *) connection{//1 Nsmutableset *foundbarcodes = [Nsmutableset new]; [Metadataobjects enumerateobjectsusingblock: ^ (avmetadataobject *obj, Nsuinteger idx, BOOL *stop) {NSLog (@ "Metadat        A:%@ ", obj); 2 if ([Obj Iskindofclass:[avmetadatamachinereadablecodeobject class]]) {//3 Avmet Adatamachinereadablecodeobject *code = (avmetadatamachinereadablecodeobject*) [_previewlayer TransformedMetadat            Aobjectformetadataobject:obj];            4 Barcode *barcode = [self processmetadataobject:code];        [Foundbarcodes Addobject:barcode];    }    }]; Dispatch_sync (Dispatch_get_main_queue (), ^{//Remove all old layers//5 Nsarray *allsublayers = [_p        ReviewView.layer.sublayers copy]; [Allsublayers enumerateobjectsusingblock: ^ (calayer*layer, Nsuinteger idx, BOOL *stop) {if (layer = _previewlayer) {[Layer removefromsuperlayer];        }                    }];            ADD new Layers//6 [foundbarcodes enumerateobjectsusingblock: ^ (Barcode *barcode, BOOL *stop) { Cashapelayer *boundingboxlayer = [Cashapelayer new]; Boundingboxlayer.path = Barcode.boundingBoxPath.CGPath; Boundingboxlayer.linewidth = 2.0f; Boundingboxlayer.strokecolor = [Uicolor Greencolor]. Cgcolor; Boundingboxlayer.fillcolor = [Uicolor colorwithred:0.0f green:1.0f blue:0.0f alpha:0 .5f]. Cgcolor;            [_previewview.layer Addsublayer:boundingboxlayer]; Cashapelayer *cornerspathlayer = [Cashapelayer new]; Cornerspathlayer.path = Barcode.cornersPath.CGPath; Cornerspathlayer.linewidth = 2.0f; Cornerspathlayer.strokecolor = [Uicolor Bluecolor]. Cgcolor;               Cornerspathlayer.fillcolor = [Uicolor colorwithred:0.0f green:0.0f blue:1.0f             ALPHA:0.5F]. Cgcolor;        [_previewview.layer Addsublayer:cornerspathlayer];    }]; });}

    1. Create a nsmutableset to traverse the detected QR code.
    2. Handles an object of type Avmetadatamachinereadablecodeobject.
    3. Converts the bounds and corner coordinates of an image. Converts the relative coordinates to the coordinates of the container view.
    4. Process the QR code data and add it to the dictionary.
    5. Removes all the sub-layers in the preview view.
    6. Traverse all detected QR codes and add boundary and angular paths to them. These layers have different colors and the alpha value is set to 0.5 so that we can see the original QR code image through the overlay.

Compile and run, the effect is as follows:


In the next section, we will add speech synthesis functions to the program to read the contents of the QR code automatically.

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


IOS AV Foundation QR code scan 03 Add visualizations for scanned QR codes

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.