我试着用swift从静态图像中读取二维码。
我可以很容易地阅读它使用一个视频源,虽然它似乎是非常不同的图像,我找不到太多的在线资源为这一点。
谢谢你的帮助。

最佳答案

您可以使用ZXingObjC制作一个很棒的qrcode扫描仪。它是一个条形码图像处理库,设计用于iOS设备和Mac应用程序。它可以从实时视频或照片库中的图像进行扫描,并支持所有主要的qrcode格式。
这只是为了让你朝着正确的方向开始。你需要更多的方法来设置相机等。zxingobjc包括样本项目和相机设置解决方案各地,所以这是非常直接的。
您需要安装zxingobjc podspod 'ZXingObjC'并创建一个bridging-header.h文件才能使用zxingobjc库。
视图控制器.swift

import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var labelOutput: UILabel!
@IBOutlet weak var QRImage: UIImageView!

var imagePicker = UIImagePickerController()

// imagePicker delegate is itself (UIImagePickerController)
override func viewDidLoad() {
    super.viewDidLoad()
    imagePicker.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

@IBAction func scanQRCode(sender: AnyObject) {
    imagePicker.sourceType = .PhotoLibrary
    imagePicker.allowsEditing = false
    presentViewController(imagePicker, animated: true, completion: nil)
}

// set up the picker
// initialize luminance source, scanning algorithm, decoding of bitmap, reader helpers, decoder
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    let placeHolderImage:UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    QRImage.contentMode = .ScaleAspectFit
    QRImage.image = placeHolderImage
    dismissViewControllerAnimated(true, completion: nil)

    let luminanceSource: ZXLuminanceSource = ZXCGImageLuminanceSource(CGImage: placeHolderImage.CGImage)
    let binarizer = ZXHybridBinarizer(source: luminanceSource)
    let bitmap = ZXBinaryBitmap(binarizer: binarizer)
    let hints: ZXDecodeHints = ZXDecodeHints.hints() as! ZXDecodeHints
    let QRReader = ZXMultiFormatReader()

    // throw/do/catch and all that jazz
    do {
        let result = try QRReader.decode(bitmap, hints: hints)
        labelOutput.text = result.text
    } catch let err as NSError {
        print(err)
    }
}

// Conform to ZXCaptureDelegate
func captureResult(capture: ZXCapture!, result: ZXResult!) {
    // do some stuff
    return
 }
}

一个注意事项:截至本文,库的ZXParsedResult.m文件中存在已知的初始值设定项错误。安装库后,文件在xcode中的位置是:Project -> Pods -> ZXingObjC -> All -> ZXParsedResult.m
在第29行,更改目标C代码
+ (id)parsedResultWithType:(ZXParsedResultType)type {
return [[self alloc] initWithType:type];
}


+ (id)parsedResultWithType:(ZXParsedResultType)type {
return [(ZXParsedResult *)[self alloc] initWithType:type];
}

09-26 06:29