Two-dimensional Code and album recognition in iOS generated QR code

Source: Internet
Author: User

IOS uses cidetector scan album QR Code, native scan native scan
  • After iOS7, avfoundation let us finally use the native scan for scanning code (QR code and barcode can be) avfoundation allows us to get the input stream and output stream from the device, thus obtaining the information contained in the QR code.

  • Implementing native scanning is straightforward.
    1. Import the Avfoundation framework first.
    <AVFoundation/AVFoundation.h>
    2. Then set up the proxy to implement the proxy callback method
    Avcapturemetadataoutputobjectsdelegate
    3. Then create several classes:
    Equipment Avcapturedevice
    Capture Session Avcapturesession
    Input stream Avcapturedeviceinput
    Output stream Avcapturemetadataoutput
    Preview Layer Avcapturevideopreviewlayer

  • Here is an example of a simple code implementation

      Nserror *error =NilAvcapturedevice *capturedevice = [Avcapturedevice Defaultdevicewithmediatype:Avmediatypevideo];EquipmentAvcapturesession *session = [[Avcapturesession alloc] init];Capture session [Session Setsessionpreset:Avcapturesessionpresethigh];Set acquisition rateAvcapturedeviceinput *input = [Avcapturedeviceinput Deviceinputwithdevice:capturedevice error:&error];Input streamAvcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc] init];Output streamAdd to capture session [session Addinput:input]; [Session Addoutput:output];Scan type: The output stream needs to be added to the capture session before settingHere only can scan QR code, there is bar code need, continue to add in the array [output setmetadataobjecttypes:@[Avmetadataobjecttypeqrcode]];//output stream delegate, refresh UI in main thread [output Setmetadataobjectsdelegate: Self Queue:dispatch_get_main_queue ()]; avcapturevideopreviewlayer *videopreviewlayer = [[//preview Videopreviewlayer.frame =  Self.view.bounds; [self.view.layer Insertsublayer:videopreviewlayer atindex:0]; //Add preview layer //can also set scan range output.rectofinterest do not set default to full screen //start scanning [session startrunning];           
  • Then implements the callback method for the successful sweep code

    -(avcaptureoutput *) captureoutput didoutputmetadataobjects: (nsarray *) metadataobjects fromconnection: (AVCaptureConnection * ) connection {nsstring *content = @ ""; avmetadatamachinereadablecodeobject *metadataobject = Metadataobjects.firstObject ; Content = Metadataobject.stringvalue; Gets the information string in the QR code //This string (sound, url analysis, page jump, etc.)}      
  • We can also add sound and vibration effects after a successful sweep to improve the user experience
    - (void)playBeep{  SystemSoundID soundID;  AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"滴-2"ofType:@"mp3"]], &soundID); AudioServicesPlaySystemSound(soundID); AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);}
Get QR code from album
  • After iOS8, you can use Cidetector (Cidetector for face recognition) to parse the image, so that we can easily get the QR code from the album.
  • 1. Call the System album and choose a picture from the system album
    2. Use the detector (cidetector) to process the selected picture, obtain the data information contained in the QR code of the picture.
  • Here is an example of a simple code implementation

    - (void) choicephoto{Call albumUiimagepickercontroller *imagepicker = [[Uiimagepickercontroller Alloc]init]; Imagepicker. sourcetype =Uiimagepickercontrollersourcetypephotolibrary; Imagepicker. Delegate =Self [Self Presentviewcontroller:imagepicker Animated:YES Completion:NIL];}Callback for the selected picture-(void) Imagepickercontroller: (uiimagepickercontroller*) Picker Didfinishpickingmediawithinfo: (Nsdictionary *) info{NSString *content =@"" ;Remove the selected pictureUIImage *pickimage = info[Uiimagepickercontrolleroriginalimage];NSData *imagedata =uiimagepngrepresentation (pickimage); ciimage *ciimage = [ciimage Imagewithdata:imagedata]; //create probe cidetector *detector = [ Cidetector detectoroftype:cidetectortypeqrcode context: Nil Options:@{cidetectoraccuracy: CIDetectorAccuracyLow} ]; nsarray *feature = [detector featuresinimage:ciimage]; //take out the detected data for ( Ciqrcodefeature *result in feature) {content = Result//for processing (sound, URL analysis, page jump, etc.)}         

Generate QR codes in iOS and identify QR codes in albums

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.