扫描从快照中从图像库中拾取的相机和图像的qrcode和条形码 [英] Scan qrcode and barcode from camera and image which picked from image library in swift

查看:171
本文介绍了扫描从快照中从图像库中拾取的相机和图像的qrcode和条形码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ios的新手。我正在学习快速且被忽视的对象c。



目前,我正在编写一个带有swift和xcode 6.1的演示,它可以扫描来自摄像机的qrcode和条形码或者来自图像库。



我知道AVFoundation框架支持扫描qrcode和条形码,但它只能从相机扫描。



<我搜索并找到zbarSDK,它支持来自摄像头和图像的扫描码。我按照。
在我的情况下,我从图书馆拿一张照片(应该是一个二维码,这里 qrcodeImg ),然后在 CIImage 由 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 feature! [CIQRCodeFeature] {
qrCodeLink + = feature.messageString
}

if qrCodeLink =={
print(nothing)
} else {
print(message:\(qrCodeLink))
}


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.

解决方案

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

这篇关于扫描从快照中从图像库中拾取的相机和图像的qrcode和条形码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆