Generation and use of QR codes in iOS (introductory article)

Source: Internet
Author: User

Here is a brief summary of the two-dimensional code scanning and generation, with the original avfoundation framework, in fact, the framework is still adequate for the current function, but here is recommended a two-dimensional code scanning of the third party (face++), the URL is not affixed, the direct degree Niang OK, the contents of the content is rich enough, Interested friends can go to dealing things for themselves.

A. Two-dimensional code scanning

Steps: 1. Create an Input video device (default is a rear camera) 2. Create an input Device Object 3. Create a meta-data output device 4. Create a session to link inputs and outputs 5. Add the output device to the session, Add the input device to the SESSION6. Set the type of the data source (requires a real machine to have a value), specify the agent (listener results in Proxy method) 7. Start scanning 8. Add a video preview interface (here is the open Camera 9. Implementation code implementation of the Proxy method:
1- (void) Viewdidload {2 [Super Viewdidload];3 4 //1. Create an input video device5     //The default is the rear-facing camera6Avcapturedevice * device =[Avcapturedevice Defaultdevicewithmediatype:avmediatypevideo];7 //2. Create an input device object8Nserror * ERROR =Nil;9Avcapturedeviceinput * InputDevice = [[Avcapturedeviceinput alloc]initwithdevice:device error:&ERROR];Ten      One //3. Create a meta-data output device AAvcapturemetadataoutput * Outputdevice =[[Avcapturemetadataoutput alloc]init]; - //4. Create a session (function to connect inputs and outputs) -Self.session =[[Avcapturesession alloc]init]; the //5. Add the output device to the session -     if([self.session Canaddoutput:outputdevice]) { - [Self.session Addoutput:outputdevice]; -     } + //6. Add a session to the input device -     if([self.session Canaddinput:inputdevice]) { + [Self.session Addinput:inputdevice]; A     } at //7. Set the data source type, this value will have the value of the real machine - //NSLog (@ "%@", outputdevice.availablemetadataobjecttypes); -Outputdevice.metadataobjecttypes = @[@"Org.iso.QRCode"]; -     //set the proxy for the output - [Outputdevice setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()]; - //8. Start Scanning in [Self.session startrunning]; -      to //9. Setting up the preview screen +     //Video preview Layer -Avcapturevideopreviewlayer * PREVIEWL =[[Avcapturevideopreviewlayer alloc]initwithsession:self.session]; the     //Set Frame *Previewl.frame =self.view.bounds; $     //add to view of controllerPanax Notoginseng [Self.view.layer ADDSUBLAYER:PREVIEWL]; -      the } +  A #pragmaMark-avcapturemetadataoutputobjectsdelegate the //The result of printing output in this agent +- (void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects fromConnection :(Avcaptureconnection *) Connection - { $Avmetadatamachinereadablecodeobject *Object=[Metadataobjects Firstobject]; $NSLog (@"%@",Object. stringvalue); -     //Stop the scan so the camera will stop. - [Self.session stoprunning]; the}

Two. Generation of two-dimensional code (this column is a picture of a two-dimensional code generated from a piece of text)
1  1.1Create a filter to generate two-dimensional code2Cifilter *qrfilter = [Cifilter filterwithname:@"Ciqrcodegenerator"];3NSData *messagedata = [@"How are you doing recently? "datausingencoding:nsutf8stringencoding];4 //1.2 Setting the input of a two-dimensional filter5[Qrfilter setvalue:messagedata Forkey:@"InputMessage"];6 //1.3 Take out the picture7Ciimage *ciimage =Qrfilter.outputimage;8 //1.4 Enlarge Image9Ciimage = [Ciimage Imagebyapplyingtransform:cgaffinetransformmakescale (8,8)];Ten          One #pragmaMark-Modify the foreground and background colors of the QR code A //Modify the foreground and background colors of the QR code, and when you modify the foreground or background color, it will decrease the image's recognition level . -Cifilter *falsecolorfilter = [Cifilter filterwithname:@"Cifalsecolor"]; - //inputimage, Photo the //inputColor0, foreground color - //InputColor1 Background Color - //These three parameters are all objects in the Coreimage - //NSLog (@ "%@", Falsecolorfilter.inputkeys); + //Image -[Falsecolorfilter setvalue:ciimage Forkey:@"Inputimage"]; + //Front View A[Falsecolorfilter setvalue:[cicolor colorwithred:1Green0Blue0] Forkey:@"InputColor0"]; at //Background Color -[Falsecolorfilter setvalue:[cicolor colorwithred:0Green0Blue1] Forkey:@"InputColor1"]; -     -      -  - //Remove the picture from the filter inCiimage =Falsecolorfilter.outputimage; -      to //Two-dimensional code above the addition of avatars, will also reduce the recognition of two-dimensional code +UIImage *qrimage =[UIImage imagewithciimage:ciimage]; -      the #pragmaMark-Draw a picture of the original QR code * //draw a picture of the original QR code $ //Open Picture ContextPanax Notoginseng Uigraphicsbeginimagecontext (qrimage.size); - //drawing two-dimensional code images the[Qrimage Drawinrect:cgrectmake (0,0, QrImage.size.width, QrImage.size.height)]; + //Drawing an avatar AUIImage *headimage = [UIImage imagenamed:@"Cang"]; theCGFloat HEADW = QrImage.size.width *0.2; +CGFloat headh = QrImage.size.height *0.2; -CGFloat HEADX = (QRIMAGE.SIZE.WIDTH-HEADW) *0.5; $CGFloat heady = (qrimage.size.height-headh) *0.5; $ [Headimage drawinrect:cgrectmake (HEADX, Heady, HEADW, headh)]; - //remove a picture from the picture context -Qrimage =Uigraphicsgetimagefromcurrentimagecontext (); the      - //Close Picture ContextWuyi Uigraphicsendimagecontext (); the      - //1.6 Convert Ciimage to UIImage WuSelf.imageView.image =Qrimage; -     

PS: This column is relatively simple, write relatively humble, follow-up demand will gradually improve, we have what good code can also be affixed to the exchange.

Generation and use of QR codes in iOS (introductory 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.