【问题标题】:Swift : Scan QR from photos gallery using AVFoundation frameworkSwift:使用 AVFoundation 框架从照片库中扫描 QR
【发布时间】:2018-01-22 08:11:43
【问题描述】:

我想扫描从照片库中提取的二维码。这个link 有类似的东西,但没有多大帮助。 我已经成功使用相机实现了扫描 QR 功能。下面是代码:

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!)
    {
        // Check if the metadataObjects array is not nil and it contains at least one object.
        if metadataObjects == nil || metadataObjects.count == 0 {
            qrCodeFrameView?.frame = CGRect.zero
//            lblMesage.text = QRCaptureFailedMessage
            return
        }

        // Get the metadata object.
        metadataObj = metadataObjects[0] as? AVMetadataMachineReadableCodeObject

        // Here we use filter method to check if the type of metadataObj is supported
        // Instead of hardcoding the AVMetadataObjectTypeQRCode, we check if the type
        // can be found in the array of supported bar codes.
        if supportedBarCodes.contains(metadataObj!.type) {
            //        if metadataObj.type == AVMetadataObjectTypeQRCode {
            // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
            let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
            qrCodeFrameView?.frame = barCodeObject!.bounds

            if metadataObj!.stringValue != nil {

                . . .
            }
        }
   }


func scanQRFromGallery(qrcodeImg : UIImage) {

        let detector:CIDetector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])!
        let ciImage:CIImage = CIImage(image:qrcodeImg)!
        var qrCodeLink=""

        let features=detector.features(in: ciImage)

        for feature in features as! [CIQRCodeFeature] {
            qrCodeLink += feature.messageString!
        }

        if qrCodeLink=="" {
            print("qrCodeLink is empty")
        }
        else{
            print("message: \(qrCodeLink)")
        }
    }

任何帮助将不胜感激。

【问题讨论】:

标签: ios swift qr-code


【解决方案1】:

您可以使用此代码扫描图库照片中的代码。有关代码的更多信息:

import UIKit
import AVFoundation

class QRScanner: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        guard
            let qrcodeImg = info[UIImagePickerController.InfoKey.originalImage.rawValue] as? UIImage,
            let detector: CIDetector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh]),
            let ciImage: CIImage = CIImage(image:qrcodeImg),
            let features = detector.features(in: ciImage) as? [CIQRCodeFeature]
        else {
            print("Something went wrong")
            return
        }
        var qrCodeLink = ""
        features.forEach { feature in
            if let messageString = feature.messageString {
                qrCodeLink += messageString
            }
        }
        if qrCodeLink.isEmpty {
            print("qrCodeLink is empty!")
        } else {
            print("message: \(qrCodeLink)")
        }
        self.dismiss(animated: true, completion: nil)
    }
}

您可以阅读这篇文章: Scan QR Code From Gallery Swift

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2016-08-26
    • 2020-10-04
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    相关资源
    最近更新 更多