Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...