The generation and use of ios-depth analytic two-dimensional code

Source: Internet
Author: User

Using a small demo to learn the QR code, a total of four interface (main interface, the generation of two-dimensional code interface, the identification of two-dimensional code interface, scanning QR code interface)I. Introduction of two-dimensional code1. What are two-dimensional codes? Two-dimensional barcode/Two-D code is a certain geometric pattern in the plane distribution of the black-and-white graphics data symbol Information Summary: the use of graphical records to mark some information, easy to obtain information through the graphic Recognition 2 application scenario information Acquisition (business card, map, WiFi password, data) mobile phone e-commerce (User scan code, mobile phone direct shopping order) mobile phone payment (scan the product QR code, through the bank or third-party payment provided by the mobile phone channel to complete payment) Add friendsTwo. Construction of two-dimensional code interface1. A total of four interfaces, you can use storyboard to build 2. Four storyboard placed on an interface that looks ugly and easy to mix, is there an optimization scheme?     The four storyboard can be separated into a single interface 3. How to put storyboard in a separate interface, but also to make these interfaces connected (wired)? Can use storyboard reference to solve is to use a reference to replace the storyboard, to maintain the connection between storyboard (Wired) 4. Final effect

three. Generation of two-dimensional code1. Steps to generate a QR Code 1.1 create a filter cifilter filter belongs to the Coreimage frame, to import the frame it will be used to process the picture (generated glass effect/two-D code) 1.2 to set the content of the filter (in KVC mode assignment)     Content must be NSData type 1.3 Gets the generated QR code picture gets the picture is the Ciimage type, uses the words to be converted 2. Run the program to find the generated QR code picture is very vague, why?     Born into a QR code image size of 27 * 27 is stretched too large, so not clear 3. How do I display a clear QR code? Apple provides an API (Ciimage method) to enlarge the image without affecting the sharpness
1  // 1. Create transform    orginalimage with a data type of Ciimage2let scale  = imageView.bounds.width/  OrginalImage.extent.width3let  transform = cgaffinetransformmakescale (scale, scale) 4  // 2. Enlarge image 5  Let Hdimage = Orginalimage.imagebyapplyingtransform (transform)

4. Set foreground picture 4.1 Why do you want to set a foreground picture?          General two-dimensional Code Center has a small picture, is the foreground image generation QR Code no foreground picture, need to manually add foreground picture 4.2 How to add a foreground picture?          is to make two pictures together as a picture, with the drawing can be easily done 4.3 drawing step 4.31 to open the graphics context 4.32 to draw a QR code picture to the graphics context (size of the QR code = size of the graphics context) 4.33 draw the foreground picture to the graphics context (center of the foreground image = center of the graphics context) 4.34 get a new picture from the graphics context 4.35 close the graphics contextFour. Identification of two-dimensional code1. Get the QR code in the album 1.1 How do I get an album? 1.11 Creating a photo selection controller 1.12 Setting the source type of the photo 1.13 setting up the proxy 1.14 eject controller
         //1. Create a photo selection controllerLet IPC =Uiimagepickercontroller ()//2. Set the type of sourceIpc.sourcetype = . Photolibrary//3. Setting up the agentIpc.Delegate= Self//4. Eject the controllerPresentviewcontroller (IPC, animated:true, Completion:nil) implements the selected picture in the proxy method dismiss the controller func Imagepickercontroller (Picker:uiimagepickercontro Ller, didfinishpickingmediawithinfo info: [String:anyobject]) {//Select a photoImageview.image = Info[uiimagepickercontrolleroriginalimage] as?UIImage picker.dismissviewcontrolleranimated (true, Completion:nil)}
 2. Step 2.1 to identify the QR code create a Cidetector object (recognizer) 2.2 Get the picture and turn the picture into Ciiimage 2.3 identify the QR code in the picture (get an array, there may be more than one QR code in the picture) 2.4 iterating over the array
       //1. Create a recognizerLet detector =Cidetector (Oftype:cidetectortypeqrcode, Context:nil, Options:nil)//2. Get the picture and turn the picture into CiiimageLet image = Imageview.image!Guard Let Ciimage= Ciimage (Image:image)Else {            return        }         //3. Identify the QR code in the imageLet features =detector.featuresinimage (ciimage)//4. Iterate through all the elements in the array         forFinchFeatures {//the feature type is cifeature to be converted to a two-dimensional code type CiqrcodefeatureGuard Let Qrcodef = f as? CiqrcodefeatureElse {                Continue            }            //Print the QR code informationprint (qrcodef.messagestring)}
Five. Scan QR code (requires real machine operation)1. Scanning two-dimensional code interface Building 1.1 is mainly the scanning frame of the building scan box plus a imageview, give ImageView an animated simulation is scanning (shockwave) 1.2 Scan box is a picture, Shockwave is also a picture, their position and ruler          Inch is the same. To make it easier to change the position of the control later, you can use a view to enclose the scan box and the shockwave in the inside 1.3 scan animation (shockwave) animation How to do? 1.31 Set the bottom constraint of the shockwave relative to the parent control (view) there is a gap of 1.32 to change the spacing of the constraint to achieve the effect of the animation
        // 1. Change the constraint (original constraint is -240)         -          // 2. Performing animations        Uiview.animatewithduration (1.0) {            uiview.setanimationrepeatcount (maxfloat)            Self.qrCodeView.layoutIfNeeded ()        }
2. Scan QR Code 2.1 Scan Step 2.11 Create a Capture session (import avfoundation frame required) 2.12 Set input (webcam) 2.13 Set Output Metadata 2.14 Add preview layer (can not) preview layer is to let users know where to scan, generally for the user experience, will add 2.15 to start scanning source code: It is recommended not to rote, the use of direct copy
         //1. Create a capture sessionLet session =avcapturesession ()//2. Setting the input (camera)Let device =Avcapturedevice.defaultdevicewithmediatype (Avmediatypevideo) guard let input=Try? Avcapturedeviceinput (Device:device)Else {            return} session.addinput (input)//3. Setting output (Metadata)Let output =avcapturemetadataoutput ()//Set up proxyoutput.setmetadataobjectsdelegate (Self, Queue:dispatch_get_main_queue ()) session.addoutput (output) //sets the type of output for outputs (the settings of this type must be added after the session)Output.metadataobjecttypes =[Avmetadataobjecttypeqrcode]//4. Add a preview layer (not available)Let Previewlayer =Avcapturevideopreviewlayer (session:session) previewlayer.frame=view.bounds View.layer.insertSublayer (Previewlayer, Atindex:0)               //5. Start scanningSession.startrunning ()

3. Get Scan result 3.1 set proxy, get result proxy method in Proxy method
Func captureoutput (captureoutput:avcaptureoutput!, didoutputmetadataobjects metadataobjects: [AnyObject]!, Fromconnection connection:avcaptureconnection! {  aselse  {    return  }    Print (Objc.stringvalue)} 

5. Set the scan area.     5.1 Why should I set the scan area?     Scan the QR code, found that as long as the two-dimensional code into the camera area, you can directly scan the request is to enter the scan box before scanning 5.2 How to set the scanning area? Set the scan area by setting the Output.rectofinterest property
// set the scanned area        Let Screenw = uiscreen.mainscreen (). bounds.width        = uiscreen.mainscreen (). bounds.height        = qrcodeview.frame.origin.x/ screenw        = QRCODEVIEW.FRAME.ORIGIN.Y/ screenh        = QrCodeView.frame.width/ screenw        = qrCodeView.frame.height/ screenh        = CGRect (x:y, y: X, Width:h, height:w)

Note: The coordinate system of the scan area is exactly the opposite of the screen's coordinate system (scan area x = screen coordinate system y)

5.3 Setting the scan area code where to write? The scan area is a property of the output and should be written behind the creation of the output code

The generation and use of ios-depth analytic two-dimensional 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.