我正在尝试使用Swift从静态图像中读取QR码。
我可以使用视频源轻松阅读它,虽然图像看起来非常不同,但我无法在网上找到太多资源。
感谢任何帮助,谢谢。
答案 0 :(得分:1)
您可以使用ZXingObjC制作出色的QRCode扫描仪。它是一个条形码图像处理库,设计用于iOS设备和Mac应用程序。它可以从实时视频或照片库中的图像进行扫描,并支持所有主要的QRCode格式。
这只是为了让您开始朝着正确的方向前进。您需要更多方法来设置相机等.ZXingObjC包含示例项目,并且有相机设置解决方案,所以所以这很直接。
您需要安装ZXingObjC pod pod 'ZXingObjC'
以及创建bridging-header.h
文件,以便能够使用ZXingObjC库。
ViewController.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行更改Objective-C代码
+ (id)parsedResultWithType:(ZXParsedResultType)type {
return [[self alloc] initWithType:type];
}
到
+ (id)parsedResultWithType:(ZXParsedResultType)type {
return [(ZXParsedResult *)[self alloc] initWithType:type];
}