4

I want to scan qr and bar codes both from live camera and from image. I previously used ZBar library to scan codes. It doesn't scan specific types of qr and bar codes. Also Apple's AVFoundation framework seems like more fast and accurate while scanning codes from live camera.

So I don't want to use ZBar. In order to scan code from images picked from gallery I'm using CIDetector. But there seems like CIDetector can't scan bar codes from images. I have searched all over stack over flow CIDetector For other Barcode Types , Scanning barcode from UIImage natively (i.e., not using ZBar)

but I haven't found a way to scan bar codes form images picked from gallery using CIDetector. Is it possible to scan barcodes from UIImages using CIDetector ?

Don't suggest other third party library. I want to use apple's 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"];
     }

}
Community
  • 1
  • 1
Jaffer Sheriff
  • 1,444
  • 13
  • 33
  • You already scan on UIImages? You convert them to a CIImage ([[CIImage alloc]initWithImage:image]) and then pass them to CIDetector. – Lepidopteron May 04 '17 at 13:31
  • That work only for scanning QR codes but not for bar codes. For images containing barcodes I'm getting empty array while calling `featuresInImage `. – Jaffer Sheriff May 04 '17 at 13:34

3 Answers3

1

Till now apple's AVFoundation Framework does not provide a way to scan bar codes picked from image library.

So I solved my issue by using AVFoundation Framework for scanning QR and Bar code in live camera, whereas when user pics photos from gallery I use ZBar Framework to scan qr and bar codes.

Jaffer Sheriff
  • 1,444
  • 13
  • 33
  • Zbar framework only i am using but some bar codes its not scanning which image selected from gallery is there any solution please let me know. – vijay May 12 '21 at 16:34
1

As of now iOS 11 CIDetector has only following types to detect from image

  • CIDetectorTypeFace
  • CIDetectorTypeRectange
  • CIDetectorTypeQRCode
  • CIDetectorTypeText

So as per your request, we can not really depend on CIDectector to read barcode and QR code from an image. However, we do have other options, like AVFoundation frameworks AVCaptureMetadataOutput but we need to have a live camera view to detect barcode and QR code.

For future readers, you can check out Swift Camrea blog post for detecting meta data's from a custom camera.


If you are targeting iOS11 and above, we do have a new set of Vision API's to read barcodes from an image. Simply create a VNDetectBarcodesRequest and get the barcode results. Check out iOS 11 QR Code Example project from Github. Note that documentations for vision API are not mature enough since iOS 11 is only in beta stage.

Bluewings
  • 3,438
  • 3
  • 18
  • 31
0

Use 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 in Mac applications.

Sargis
  • 1,196
  • 1
  • 18
  • 31