QRScanner

一个简单的 iOS 平台的二维码扫描框架。提供了类似于 iOS 13+ 的扫描效果。使用 Swift 编写。

iOS 13.0+ 在 iOS 10.0+ 中使用 QRScanner

“QR Code” 是 DENSO WAVE INCORPORATED 的注册商标

特性

开发要求

安装

QRScanner 支持多种方式将库安装到项目中。

使用 CocoaPods 安装

  platform :ios, '11.0'
  pod 'MercariQRScanner'
  pod install
  import MercariQRScanner

使用 Swift Package Manager 安装

一旦您设置好 Swift Package,添加 QRScanner 作为依赖项就像将其添加到 Package.swift 的 dependencies 值一样简单。

dependencies: [
    .package(url: "https://github.com/mercari/QRScanner.git", .upToNextMajor(from: "1.9.0"))
]
import QRScanner

使用 Carthage 安装

github "mercari/QRScanner"
import QRScanner

用法

请参见 QRScannerSample

Privacy - Camera Usage Description 添加到 Info.plist 文件

基本用法

import QRScanner // If use the Pod way, please import MercariQRScanner
import AVFoundation

final class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupQRScanner()
    }

    private func setupQRScanner() {
        switch AVCaptureDevice.authorizationStatus(for: .video) {
        case .authorized:
            setupQRScannerView()
        case .notDetermined:
            AVCaptureDevice.requestAccess(for: .video) { [weak self] granted in
                if granted {
                    DispatchQueue.main.async { [weak self] in
                        self?.setupQRScannerView()
                    }
                }
            }
        default:
            showAlert()
        }
    }

    private func setupQRScannerView() {
        let qrScannerView = QRScannerView(frame: view.bounds)
        view.addSubview(qrScannerView)
        qrScannerView.configure(delegate: self, input: .init(isBlurEffectEnabled: true))
        qrScannerView.startRunning()
    }

    private func showAlert() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
            let alert = UIAlertController(title: "Error", message: "Camera is required to use in this application", preferredStyle: .alert)
            alert.addAction(.init(title: "OK", style: .default))
            self?.present(alert, animated: true)
        }
    }
}

extension ViewController: QRScannerViewDelegate {
    func qrScannerView(_ qrScannerView: QRScannerView, didFailure error: QRScannerError) {
        print(error)
    }

    func qrScannerView(_ qrScannerView: QRScannerView, didSuccess code: String) {
        print(code)
    }
}

自定义

源码方式

override func viewDidLoad() {
        super.viewDidLoad()

        let qrScannerView = QRScannerView(frame: view.bounds)

        // Customize focusImage, focusImagePadding, animationDuration
        qrScannerView.focusImage = UIImage(named: "scan_qr_focus")
        qrScannerView.focusImagePadding = 8.0
        qrScannerView.animationDuration = 0.5

        qrScannerView.configure(delegate: self)
        view.addSubview(qrScannerView)
        qrScannerView.startRunning()
}

Interface Builder 方式

设置自定义类 自定义

添加闪光灯按钮

FlashButtonSample

final class ViewController: UIViewController {

    ...

    @IBOutlet var flashButton: FlashButton!

    @IBAction func tapFlashButton(_ sender: UIButton) {
        qrScannerView.setTorchActive(isOn: !sender.isSelected)
    }
}

extension ViewController: QRScannerViewDelegate {

    ...

    func qrScannerView(_ qrScannerView: QRScannerView, didChangeTorchActive isOn: Bool) {
        flashButton.isSelected = isOn
    }
}

添加模糊效果

源码方式

     qrScannerView.configure(delegate: self, input: .init(isBlurEffectEnabled: true))

Interface Builder 方式

自定义

提交者

贡献

在向 Mercari 提交您的贡献之前,请仔细阅读 CLA。在任何情况下,通过提交您的贡献,您都被视为接受并同意受 CLA 条款和条件的约束。

https://www.mercari.com/cla/

许可协议

Copyright 2019 Mercari, Inc.

在 MIT 许可证下发布。