如何使用Swift扫描UIImage的QR码

afdcj2ne  于 2023-05-05  发布在  Swift
关注(0)|答案(2)|浏览(255)

我正在尝试使用Swift从静态图像中读取QR码。
我可以很容易地阅读它使用视频源,虽然它似乎是非常不同的图像,我不能找到太多的资源在网上这一点。
任何帮助感激不尽,谢谢。

62o28rlo

62o28rlo1#

您可以使用ZXingObjC制作一个很棒的QRCode扫描器。它是一个条形码图像处理库,旨在用于iOS设备和Mac应用程序。它可以从实时视频或照片库中的图像进行扫描,并支持所有主要的QRCode格式。

**这只是为了让你在正确的方向开始。**你需要更多的方法来设置相机等。ZXingObjC包括示例项目,并且有相机设置解决方案,所以它非常简单。

你需要安装ZXingObjC pods 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];
}
wdebmtf2

wdebmtf22#

func readQRCode(from image: UIImage) -> Result<String, QRReadingError> {
    guard let ciImage = CIImage(image: image) else {
        print("Couldn't create CIImage")
        return .failure(.generic)
    }

    guard let detector = CIDetector(
        ofType: CIDetectorTypeQRCode,
        context: nil,
        options: [CIDetectorAccuracy: CIDetectorAccuracyHigh]
    ) else {
        print("Detector not intialized")
        return .failure(.generic)
    }
    let features = detector.features(in: ciImage)
    let qrCodeFeatures = features.compactMap { $0 as? CIQRCodeFeature }
    guard let qrCode = qrCodeFeatures.first?.messageString else {
        print("No QR code found in the image")
        return .failure(.qrCodeNotFoundInImage)
    }
    return .success(qrCode)
}

enum QRReadingError: Error {
    case generic
    case qrCodeNotFoundInImage
}

相关问题