Scan qrcode and barcode from camera and image which picked from image library in swift

Son Tran picture Son Tran · Feb 4, 2015 · Viewed 10.6k times · Source

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.

Answer

Marie Dm picture Marie Dm · Mar 12, 2015

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)")
}