4

I'm a newbie with Ios. i'm learning swift and overlooked object c.

Currently, i'm writing an demo with swift and xcode 6.1 which can scan qrcode and barcode from camera or an image from image library.

I know that AVFoundation framework support scanning qrcode and barcode, but it can only scan from camera.

I searched and found zbarSDK which support scan code from camera and image. I do as instructions in http://zbar.sourceforge.net/iphone/sdkdoc/ and NSFastEnumeration in Swift (convert code to swift). However, when i run app, after choosing image from library, it happen error.

This's my code

import UIKit
    import AVFoundation

    extension ZBarSymbolSet: SequenceType {
        public func generate() -> NSFastGenerator {
            return NSFastGenerator(self)
        }
    }

    class FirstViewController: UIViewController, ZBarReaderDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

        let reader = ZBarReaderController()

        @IBOutlet weak var lblResult: UILabel!
        @IBOutlet weak var imgView: UIImageView!

        override func viewDidLoad() {
            super.viewDidLoad()
            reader.delegate = self
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        @IBAction func scanCode(sender: AnyObject) {
            let scanner = reader.scanner
            scanner.setSymbology(ZBAR_I25, config: ZBAR_CFG_ENABLE, to: 0)
            reader.modalPresentationStyle = .Popover
            presentViewController(reader, animated: true, completion: nil)
        }

        func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
            var results: NSFastEnumeration = info["ZBarReaderControllerResults"] as NSFastEnumeration

            var symbolFound : ZBarSymbol?

            // =============== Error here ==================
            for symbol in results as ZBarSymbolSet {
                symbolFound = symbol as? ZBarSymbol
                break
            }
            var resultString = NSString(string: symbolFound!.data)
            println(resultString)

        }

    }

here is error image enter image description here

I will very grateful if someone let me know why it happen error and how to fix it or there's any way to scan code with an image using AVFoundation or there a other library (with detail document and sample) to do this (please give detail instructions because i have just learned swift and ios for 3 weeks). Thanks.

Community
  • 1
  • 1
Son Tran
  • 81
  • 1
  • 2
  • 6

2 Answers2

15

I was also looking to read a QR code from an image and without Zbar.

You can use CIDetector instead. I found the solution here. In my case, I pick up a photo from the library (supposed to be a QR code, here qrcodeImg) and then convert it in CIImage to be decoded by CIDetector.

qrCodeImageView.image=qrcodeImg

let detector:CIDetector=CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])
let ciImage:CIImage=CIImage(image:qrcodeImg)
var qrCodeLink=""

let features=detector.featuresInImage(ciImage)
for feature in features as! [CIQRCodeFeature] {
   qrCodeLink += feature.messageString
}

if qrCodeLink=="" {
    print("nothing")
}else{
    print("message: \(qrCodeLink)")
}
Community
  • 1
  • 1
Marie Dm
  • 2,637
  • 3
  • 24
  • 43
  • Hi Marie Dm, thank for your answer. However i found other library. I posted it here http://stackoverflow.com/questions/28402422/zxingobjc-converting-object-c-to-swift – Son Tran Mar 16 '15 at 08:09
  • 2
    Hi @SonTran, I already saw this library but it seems you need to import a SDK and I didn't want that. That is why I chose this solution. Thanks anyway ;) – Marie Dm May 29 '15 at 09:11
  • @UmeshSharma I think so but I only tried with qr codes. – Marie Dm Jun 07 '16 at 13:21
  • @UmeshSharma it is the same CIDectetor type to use (see detector type down: https://developer.apple.com/library/ios/documentation/CoreImage/Reference/CIDetector_Ref/#//apple_ref/doc/constant_group/Detector_Types): "CIDetectorTypeQRCode: A detector that searches for Quick Response codes (a type of 2D barcode) in a still image or video, returning CIQRCodeFeature objects that provide information about detected barcodes." – Marie Dm Jun 07 '16 at 13:38
  • @UmeshSharma Did you actually try to detect a barcode with this code? – Marie Dm Jun 07 '16 at 13:39
  • Works only for 2-d barcodes. I want to work on 1-d barcodes – Umesh Sharma Jun 08 '16 at 05:55
  • @MarieDm is that works for live camera stream as well? Or only for images? – Saad Nov 18 '16 at 07:04
  • @Saad No, I use AVCaptureSession & AVCaptureMetadataOutput to detect AVMetadataObjectTypeQRCode – Marie Dm Nov 18 '16 at 10:00
  • Shouldn't this be included in the library itself. – Saad Nov 18 '16 at 21:35
  • Anyhow, I was just doing some R&D. If I have to develop to this app, Possibly I will make a component having all features in it (IF not there already) Thanks for your reply btw – Saad Nov 18 '16 at 21:37
0

For Swift3 following code should work for you to get result from ZBarSDK

extension ZBarSymbolSet: Sequence {
    public func makeIterator() -> NSFastEnumerationIterator {
        return NSFastEnumerationIterator(self)
    }
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    // ADD: get the decode results
    let results: NSFastEnumeration = info[ZBarReaderControllerResults] as! NSFastEnumeration

    var symbolFound : ZBarSymbol?

    for symbol in results as! ZBarSymbolSet {
        symbolFound = symbol as? ZBarSymbol
        break
    }
    let resultString = symbolFound!.data
    print(resultString)
}
Abdullah Md. Zubair
  • 3,312
  • 2
  • 30
  • 39