iOS comes with scan and generate QR code

Source: Internet
Author: User

1. Two-dimensional code generation

Libqrencode Introduction: is a C language to parse the two-dimensional barcode (QRCode) library, Libqrencode through the mobile phone's CCD camera to scan the two-dimensional barcode.

To import the Libqrencode library, the following direct code:

#import <Foundation/Foundation.h> @interface qrcodegenerator:nsobject+ (UIImage *) qrimageforstring: (NSString * ) string imageSize: (cgfloat) size;+ (UIImage *) Twodimensioncodeimage: (UIImage *) Twodimensioncode withavatarimage: ( UIImage *) Avatarimage; @end

#import "QRCodeGenerator.h" #import "Qrencode.h" enum {qr_margin = 3}; @implementation qrcodegenerator+ (void) Drawqrcode :(QRCode *) Code context: (CGCONTEXTREF) CTX Size: (cgfloat) Size {unsigned char *data = 0;int Width;data = Code->data;widt H = code->width;float Zoom = (double) size/(code->width + 2.0 * qr_margin); CGRect Rectdraw = CGRectMake (0, 0, zoom, zoom);//Drawcgcontextsetfillcolor (CTX, cgcolorgetcomponents ([Uicolor Blackcolor]. Cgcolor)) (int i = 0; i < width; ++i) {for (int j = 0; J < width; ++j) {if (*data & 1) {Rectdraw.origin = Cgpoi Ntmake ((j + qr_margin) * Zoom, (i + qr_margin) * zoom); Cgcontextaddrect (CTX, Rectdraw);} ++data;}} Cgcontextfillpath (CTX);} + (UIImage *) qrimageforstring: (NSString *) string imageSize: (cgfloat) Size {if (![ String length]) {return nil;} QRCode *code = qrcode_encodestring ([string utf8string], 0, qr_eclevel_l, qr_mode_8, 1); if (!code) {return nil;} Create Contextcgcolorspaceref colorspace = Cgcolorspacecreatedevicergb (); Cgcontextref CTX =Cgbitmapcontextcreate (0, size, size, 8, size * 4, colorspace, kcgimagealphapremultipliedlast); Cgaffinetransform TranslateTransform = cgaffinetransformmaketranslation (0,-size); Cgaffinetransform ScaleTransform = Cgaffinetransformmakescale (1,-1); CGCONTEXTCONCATCTM (CTX, Cgaffinetransformconcat (TranslateTransform, ScaleTransform));//Draw QR on this context[ Qrcodegenerator drawqrcode:code context:ctx size:size];//get imagecgimageref qrcgimage = CGBitmapContextCreateImage ( CTX); UIImage * Qrimage = [UIImage imagewithcgimage:qrcgimage];//some releasescgcontextrelease (CTX); Cgimagerelease (Qrcgimage); Cgcolorspacerelease (ColorSpace); Qrcode_free (code); return qrimage;} + (UIImage *) Twodimensioncodeimage: (UIImage *) Twodimensioncode withavatarimage: (UIImage *) avatarimage{//Two-dime        Nsion code QR code cgsize size = twodimensioncode.size;                Cgsize size2 =cgsizemake (1.0/5.5 * size.width, 1.0/5.5 * size.height);                Uigraphicsbeginimagecontext (size); [TwodiMensioncode drawinrect:cgrectmake (0, 0, Size.width, size.height)]; [Self avatarimage:avatarimage] Drawinrect:cgrectmake ((size.width-size2.width)/2.0, (Size.height-size2.height)/2.                0, Size2.width, size2.height)];        UIImage *resultingimage =uigraphicsgetimagefromcurrentimagecontext ();        Uigraphicsendimagecontext ();    return resultingimage; }+ (UIImage *) Avatarimage: (UIImage *) avatarimage{UIImage * avatarbackgroudimage = [UIImage imagenamed:@ "Icon.png"        ];        Cgsize size = avatarbackgroudimage.size;                Uigraphicsbeginimagecontext (size);        [Avatarbackgroudimage drawinrect:cgrectmake (0, 0, Size.width, size.height)];                [Avatarimage Drawinrect:cgrectmake (Ten, size.width-20, size.height-20)];        UIImage *resultingimage =uigraphicsgetimagefromcurrentimagecontext ();        Uigraphicsendimagecontext ();    return resultingimage; } @end

2. Scanning

IOS7 Apple introduced its own scanning function, the following direct code:


-(void) Viewwillappear: (BOOL) animated{[self setupcamera];}        -(void) setupcamera{//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 [Email protected][avmetadataobjecttypecode 128code,avmetadataobjecttypeupcecode,avmetadataobjecttypecode39code,avmetadataobjecttypecode39mod43code, Avmetadataobjecttypeean13code,avmetadataobjecttypEean8code,avmetadataobjecttypecode93code,avmetadataobjecttypepdf417code,avmetadataobjecttypeqrcode, Avmetadataobjecttypeazteccode,avmetadataobjecttypeinterleaved2of5code,avmetadataobjecttypeitf14code,        Avmetadataobjecttypedatamatrixcode];    Preview _preview =[avcapturevideopreviewlayer layerWithSession:self.session];    _preview.videogravity = Avlayervideogravityresizeaspectfill;    _preview.frame =cgrectmake (20,110,280,280);            [Self.view.layer InsertSublayer:self.preview atindex:0]; Start [_session startrunning];} #pragma mark avcapturemetadataoutputobjectsdelegate-(void) Captureoutput: (Avcaptureoutput *) captureoutput Didoutputmetadataobjects: (Nsarray *) metadataobjects fromconnection: (avcaptureconnection *) connection{NSString *str        Ingvalue; if ([metadataobjects count] >0) {avmetadatamachinereadablecodeobject * metadataobject = [Metadataobjects obj        ECTATINDEX:0];    StringValue = Metadataobject.stringvalue; } [_session SToprunning]; Uialertview * alert = [[Uialertview alloc]initwithtitle:@ "Prompt" message:[nsstring stringwithformat:@ "Result:%@", StringValue    ] delegate:self cancelbuttontitle:nil otherbuttontitles:@ "Know", @ "rescan", nil]; [Alert show];}        -(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) buttonindex{if (Buttonindex = = 0) {         [Self dismissviewcontrolleranimated:yes completion:^ {[timer invalidate];    }];    } else {[_session startrunning]; }}


iOS comes with scan and generate QR code

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.