WWScanViewController

main

Use AVFoundation Framework to make a simple QRCode / BarCode scanner.
William-Weng/WWScanViewController

WWScanViewController

Swift-5.6 iOS-14.0 Swift Package Manager-SUCCESS LICENSE

dependencies: [
    .package(url: "https://github.com/William-Weng/WWScanViewController.git", .upToNextMajor(from: "1.0.0"))
]

Function - 可用函式

函式 功能
configure(types:delegate:queue:) 設定可以掃到的類型
startRunning() 開啟掃瞄
stopRunning() 關閉掃瞄

Example

import UIKit
import WWPrint
import AVFoundation
import WWScanViewController

final class ViewController: UIViewController {
    
    @IBOutlet weak var qrCodeImageView: UIImageView!
    @IBOutlet weak var qrCodeLabel: UILabel!
    
    private let feedback = UIImpactFeedbackGenerator._build(style: .soft)
    private var scanViewController: WWScanViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        guard let scanViewController = segue.destination as? WWScanViewController else { return }
        
        scanViewController.configure(types: [.code39, .qr], delegate: self)
        self.scanViewController = scanViewController
    }
    
    @IBAction func startRunning(_ sender: UIBarButtonItem) {
        scanViewController?.startRunning()
    }
    
    @IBAction func stopRunning(_ sender: UIBarButtonItem) {
        scanViewController?.stopRunning()
    }
}

// MARK: - WWScanDelegate
extension ViewController: WWScanDelegate {
    
    func metadataOutput(_ result: Result<AVMetadataMachineReadableCodeObject, Error>) {
        
        switch result {
        case .failure(let error): wwPrint(error)
        case .success(let object):
            
            feedback._impact()
            
            qrCodeLabel.text = object.stringValue
            qrCodeImageView.translatesAutoresizingMaskIntoConstraints = true
            qrCodeImageView.frame = object.bounds
            
            switch object.type {
            case .qr:
                qrCodeImageView.image = #imageLiteral(resourceName: "QRCode")
                qrCodeImageView.backgroundColor = .clear
            case .code39:
                qrCodeImageView.image = nil
                qrCodeImageView.backgroundColor = .systemPink
            default: break
            }
        }
    }
}

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Oct 18 2024 16:39:49 GMT-0900 (Hawaii-Aleutian Daylight Time)