iOS development-two-dimensional code generation and reading

Source: Internet
Author: User

Generation of two-dimensional code

The generation and reading of two-dimensional code has been integrated from iOS7
The previously widely used ZBARSDK does not currently support 64-bit processors

Steps to generate a QR code:
Import Coreimage Framework
Generate two-dimensional code with filter Cifilter

The contents of the QR code (the traditional barcode can only put the numbers):

纯文本名片URL
Generate two-dimensional code
// 1. 实例化二维码滤镜CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];// 2. 恢复滤镜的默认属性[filter setDefaults];// 3. 将字符串转换成NSDataNSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];// 4. 通过KVO设置滤镜inputMessage数据[filter setValue:data forKey:@"inputMessage"];// 5. 获得滤镜输出的图像CIImage *outputImage = [filter outputImage];// 6. 将CIImage转换成UIImage。并放大显示return [UIImage imageWithCIImage:outputImage scale:20.0 orientation:UIImageOrientationUp];
Read QR code

Reading QR code requires importing Avfoundation frame
Use the camera to identify the contents of the QR code (simulator not available)

Input (webcam)
Conversion of a QR code image from a camera collection to a string data by a session
Output (data)
Show scan scene by preview layer

Set up a shooting session
//1. Instance shooting deviceAvcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];//2. Setting the input deviceAvcapturedeviceinput *input = [avcapturedeviceinput deviceinputwithdevice:device error:Nil];//3. Setting meta-data output//3.1 instantiation of the shot meta-data outputAvcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc] init];//3.3 Set output data proxy[Output setmetadataobjectsdelegate: SelfQueue:dispatch_get_main_queue ()];//4. Join the shooting session//4.1 instancing of shooting sessionsAvcapturesession *session = [[Avcapturesession alloc] init];//4.2 Join Session Input[Session Addinput:input];//4.3 Join session output[Session Addoutput:output];///4.3 Set output data type, need to add meta-data output to the session, ability to specify the metadata type, otherwise it will error[Output SETMETADATAOBJECTTYPES:@[AVMETADATAOBJECTTYPEQRCODE]];
Set up capture video preview layer
// 5. 视频预览图层// 5.1 实例化预览图层AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:_self.view.bounds;// 5.2 将图层插入当前视图[self.view.layer insertSublayer:preview atIndex:100];self.previewLayer = preview;// 6. 启动会话[_session startRunning];
Generate Baidu QR code example
#import <CoreImage/CoreImage.h>  @interface viewcontroller ()@property(Weak,nonatomic)Iboutlet Uiimageview*iv;@end @implementation viewcontroller - (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{//1. Instantiate a QR Code filterCifilter *filter = [Cifilter filterwithname:@"Ciqrcodegenerator"];//2. Restore the default properties of the filter (since the filter might have saved the last property)[Filter SetDefaults];//3. Convert String to NSDataNSData *data = [@"Http://www.baidu.com"Datausingencoding:nsutf8stringencoding];//4. Set the filter via KVO, pass in data, and future filters will know to generate a QR code from the incoming[Filter Setvalue:data forkey:@"InputMessage"];//5. Generate two-dimensional codeCiimage *outputimage = [Filter outputimage];UIImage*image = [UIImageImagewithciimage:outputimage];//6. Set the generated QR code to ImageView     Self. IV. Image= Image;}@end

Example Diagram

Read the example of Baidu two-dimensional code
#import <AVFoundation/AVFoundation.h>  @interface viewcontroller () <avcapturemetadataoutputobjectsdelegate> @property(nonatomic,Strong) Avcapturesession *session;@property(nonatomic,Strong) Avcapturevideopreviewlayer *previewlayer;@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];//1. Instance shooting deviceAvcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];//2. Setting the input deviceAvcapturedeviceinput *input = [avcapturedeviceinput deviceinputwithdevice:device error:Nil];//3. Setting meta-data output    //3.1 instantiation of the shot meta-data outputAvcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc] init];//3.3 Set output data proxy[Output setmetadataobjectsdelegate: SelfQueue:dispatch_get_main_queue ()];//4. Join the shooting session    //4.1 instancing of shooting sessionsAvcapturesession *session = [[Avcapturesession alloc] init];//4.2 Join Session Input[Session Addinput:input];//4.3 Join session output[Session Addoutput:output];///4.3 to set the output data type, you need to add the meta-data output to the session. Ability to specify the metadata type. Otherwise it will be an error[Output SETMETADATAOBJECTTYPES:@[AVMETADATAOBJECTTYPEQRCODE]]; Self. Session= Session;//5. Video Preview Layer    //5.1 Instantiate a preview layer, passing _session to tell the layer what to display in the futureAvcapturevideopreviewlayer *preview = [Avcapturevideopreviewlayer layerwithsession:_session]; Preview. Videogravity= Avlayervideogravityresizeaspectfill; Preview. Frame= Self. View. Bounds;//5.2 Inserting a layer into the current view[ Self. View. LayerInsertsublayer:preview Atindex: -]; Self. Previewlayer= Preview;//6. Start the session[_session startrunning];} - (void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray*) metadataobjects fromconnection: (avcaptureconnection *) connection{//frequent scan, invoke proxy method    //1. Assume the scan is complete. Stop Session[ Self. SessionStoprunning];//2. Delete the preview layer[ Self. PreviewlayerRemovefromsuperlayer];NSLog(@"%@", metadataobjects);//3. Set interface to display scan results    if(metadataobjects. Count>0) {Avmetadatamachinereadablecodeobject *obj = metadataobjects[0];//Hint: If you need to scan a URL or a business card, you can expand it here.         NSLog(@"%@"Obj. StringValue); }}@end

iOS development-two-dimensional code generation and reading

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.