SwiftQRTools
扫描 QR 码、生成 QR 码图像和从图像中读取 QR 码
用法
1. 扫描 QR 码
SwiftQRTools 提供了一个 SwiftQRScanController
和一个默认视图 SwiftQRScanView
,如上图所示。您可以自己使用视图,只需继承 SwiftQRScanView
即可。在 SwiftQRScanControllerDelegate
中还有一些有用的委托方法。以下是一个示例
class ViewController: UIViewController {
// ...
@IBAction func scanQR(_ sender: Any?) {
let scanVC = SwiftQRScanController()
scanVC.delegate = self
self.present(scanVC, animated: true, completion: nil)
}
// ...
}
extension ViewController: SwiftQRScanControllerDelegate {
func scanController(_ controller: SwiftQRScanController, didFinishScanWithInfo info: String) {
print(info)
controller.dismiss(animated: false, completion: nil)
}
func scanMetadataObjectTypes(_ for: SwiftQRScanController) -> [AVMetadataObject.ObjectType] {
return [.qr, .code128, .ean13, .ean8]
}
// SwiftQRScanViewConfig used to configure the interest rect.
func scanViewConfigure(_ for: SwiftQRScanController) -> SwiftQRScanViewConfig {
return SwiftQRScanViewConfig(colorExceptInterestRect: UIColor.init(white: 0, alpha: 0.8),
interstRectWidth: 300.0,
interstRectHeight: 100.0,
interestRectCornerColor: UIColor.red,
InterestRectCornerLineLength: 40,
InterestRectCornerLineWidth: 10)
}
func scanView(_ for: SwiftQRScanController, in superView: UIView) -> SwiftQRScanView {
let scanView = ScanViewWithBack(frame: superView.bounds)
scanView.delegate = self
return scanView
}
}
2. 生成 QR 码
let image = SwiftQRUtils.generateQRCode(with: "123", tintColor: UIColor.black, imageScale: 10, centerImage: UIImage(named: "top"), centerImageSize: CGSize(width: 40, height: 40))
或直接
let image = SwiftQRUtils.generateQRCode(with: "123")
2. 从图像中读取 QR 码信息
let image = UIImage(named: "test")
let result = SwiftQRUtils.getQRCodeInfo(in: image)
print("info is : \(result)")
SimpleQR 可通过 CocoaPods 使用。要安装它,只需将以下行添加到您的 Podfile 中:
pod 'SwiftQRTools'
SwiftQRUtils 受 MIT 许可证发布。有关详细信息,请参阅 LICENSE。