Using CIDetector to scan barcodes (1 and 2 sizes)

I want to scan qr and barcodes from both camera and image. I have previously used the ZBar library to scan codes. It does not scan certain types of qr and barcodes. Plus, Apple's AVFoundation system appears to be faster and more accurate when scanning codes from a live camera.

So I don't want to use ZBar. To scan code from images selected from the gallery, I am using CIDetector. But it looks like CIDetector cannot scan barcodes from images. I searched the whole stack above the CIDetector stream for other types of barcodes , Scanning a barcode from UIImage natively (i.e. without using ZBar)

but I have not found a way to scan barcodes generated by images selected from gallery using CIDetector. Can I scan barcodes from UIImages with CIDetector?

Don't suggest another third party library. I want to use apple default framework to do the job.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{

     [picker dismissViewControllerAnimated:YES completion:nil];

     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
     CIImage *img = [[CIImage alloc]initWithImage:image];

     CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
     if (detector) 
    {
        NSArray* featuresR = [detector featuresInImage:img];
        NSString* decodeR;
        for (CIQRCodeFeature* featureR in featuresR) 
        {
            NSLog(@"decode %@ ",featureR.messageString);
            decodeR = featureR.messageString;

            [self showAlertWithTitle:@"Success" withMessage:decodeR];
           return;
         }

    [self showAlertWithTitle:@"Error" withMessage:@"Invalid Image"];
     }

}

      

+3


source to share


3 answers


Until now, Apple's AVFoundation Framework does not provide a way to scan barcodes selected from an image library.



So I solved the problem using AVFoundation Framework to scan QR and barcode in live camera, whereas when user takes photos from gallery, I use ZBar Framework to scan qr and barcodes.

0


source


Use the ZXing library.



ZXingObjC is a full Objective-C port of ZXing ("Zebra Crossing"), a Java barcode image processing library. It is designed to be used on both iOS devices and Mac apps.

0


source


Currently iOS 11 CIDetector

only has the following types to detect from image

  • CIDetectorTypeFace

  • CIDetectorTypeRectange

  • CIDetectorTypeQRCode

  • CIDetectorTypeText

As per your request, we cannot really depend on CIDectector

to read the barcode and QR code from the image. However, we have other options like AVFoundation

frameworks AVCaptureMetadataOutput

, but we need to have a live view of the camera for barcode and QR code detection.

For future readers, you may want to register a Swift Camrea post to detect metadata from a custom camera.


If you are targeting iOS11 and up, we have a new set of Vision APIs to read barcodes from an image. Just create VNDetectBarcodesRequest

and receive barcode results. Check out the iOS 11 QR Code Sample from Github. Please note that the documentation for the Vision API is not mature enough as iOS 11 is only in beta.

0


source







All Articles