扫描二维码 swift 代码实现

import UIKit

let screenWidth: CGFloat = UIScreen.main.bounds.size.width
let screenHeight: CGFloat = UIScreen.main.bounds.size.height
let screenRadio: CGFloat  = screenWidth / 375.0
let scanQRCodeWidth: CGFloat = 250 * screenRadio
let scanQRCodeTop: CGFloat = 124 * screenRadio

class SCanMaskView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
        guard let con = UIGraphicsGetCurrentContext() else {
            return
        }
        con.setFillColor(UIColor.black.withAlphaComponent(0.5).cgColor)
        con.fill(rect)
        con.clear(CGRect(x: (screenWidth - scanQRCodeWidth) / 2.0, y: scanQRCodeTop, width: scanQRCodeWidth, height: scanQRCodeWidth))
        
        //设置线条样式
        con.setLineCap(.square)
        //设置线条粗细宽度
        con.setLineWidth(4.0)
        
        //设置颜色
        con.setStrokeColor(UIColor(red: 229 / 255.0, green: 0, blue: 50 / 255.0, alpha: 1.0).cgColor)
        //开始一个起始路径
        con.beginPath()
        //起始点设置为(100,100):注意这是上下文对应区域中的相对坐标,
        con.move(to: CGPoint(x: (screenWidth - scanQRCodeWidth) / 2 + 10, y: scanQRCodeTop))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x:  (screenWidth - scanQRCodeWidth ) / 2, y: scanQRCodeTop))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x: (screenWidth - scanQRCodeWidth ) / 2, y: scanQRCodeTop + 10))
        //连接上面定义的坐标点
        con.strokePath()
        
        //开始一个起始路径
        con.beginPath()
        //起始点设置为(100,100):注意这是上下文对应区域中的相对坐标,
        con.move(to: CGPoint(x: (screenWidth - scanQRCodeWidth) / 2 + scanQRCodeWidth - 10, y: scanQRCodeTop))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x:  (screenWidth - scanQRCodeWidth ) / 2 + scanQRCodeWidth , y: scanQRCodeTop))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x: (screenWidth - scanQRCodeWidth ) / 2 + scanQRCodeWidth, y: scanQRCodeTop + 10))
        //连接上面定义的坐标点
        con.strokePath()
        
        //开始一个起始路径
        con.beginPath()
        //起始点设置为(100,100):注意这是上下文对应区域中的相对坐标,
        con.move(to: CGPoint(x: (screenWidth - scanQRCodeWidth) / 2 + scanQRCodeWidth - 10, y: scanQRCodeTop + scanQRCodeWidth))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x:  (screenWidth - scanQRCodeWidth ) / 2 + scanQRCodeWidth, y: scanQRCodeTop + scanQRCodeWidth))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x: (screenWidth - scanQRCodeWidth ) / 2 + scanQRCodeWidth, y: scanQRCodeTop + scanQRCodeWidth - 10))
        //连接上面定义的坐标点
        con.strokePath()
        
        //开始一个起始路径
        con.beginPath()
        //起始点设置为(100,100):注意这是上下文对应区域中的相对坐标,
        con.move(to: CGPoint(x: (screenWidth - scanQRCodeWidth) / 2 + 10, y: scanQRCodeTop + scanQRCodeWidth))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x:  (screenWidth - scanQRCodeWidth ) / 2, y: scanQRCodeTop + scanQRCodeWidth))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x: (screenWidth - scanQRCodeWidth ) / 2, y: scanQRCodeTop + scanQRCodeWidth - 10))
        //连接上面定义的坐标点
        con.strokePath()
    }
    
}
import UIKit
import AVFoundation

class ScanQRCodeViewController: UIViewController {
    
    var session: AVCaptureSession!
    var scanLine: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        buildUI()
    }
    
    func buildUI() {
        scanQRCode()
        
        let maskView = SCanMaskView(frame: view.bounds)
        view.addSubview(maskView)
        
    }
    
    func scanQRCode() {
        guard let device = AVCaptureDevice.default(for: .video) else { return }
        do {
            //创建输入流
            let input = try AVCaptureDeviceInput(device: device)
            //创建输出流
            let output = AVCaptureMetadataOutput()
            //设置会话
            session = AVCaptureSession()
            if session.canSetSessionPreset(.high) {
                session.sessionPreset = .high
            }
            //连接输入输出流
            if session.canAddInput(input) {
                session.addInput(input)
            }
            if session.canAddOutput(output) {
                session.addOutput(output)
            }
            //设置扫描码的类型
            output.metadataObjectTypes = [.qr, .ean13, .ean8, .code128]
            //设置扫描区域
            output.rectOfInterest = CGRect(x: scanQRCodeTop / screenHeight, y: (( screenWidth - scanQRCodeWidth ) / 2 ) / screenWidth, width: scanQRCodeWidth / screenHeight, height: scanQRCodeWidth / screenWidth)
            //捕捉图层
            let previewLayer = AVCaptureVideoPreviewLayer(session: session)
            previewLayer.videoGravity = .resizeAspectFill
            previewLayer.frame = view.layer.bounds
            view.layer.insertSublayer(previewLayer, at: 0)
            session.startRunning()
        }catch {
            
        }
        
    }

}

extension ScanQRCodeViewController: AVCaptureMetadataOutputObjectsDelegate {
    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        session.stopRunning()
        guard let readableObject = metadataObjects.first as? AVMetadataMachineReadableCodeObject else {
            return
        }
        
        if let str = readableObject.stringValue {
            print(str)
        }
    }
}

你可能感兴趣的:(扫描二维码 swift 代码实现)