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

為{幸葍}努か 提交于 2019-11-28 04:49:52

问题


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

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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/28317071/scan-qrcode-and-barcode-from-camera-and-image-which-picked-from-image-library-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!