Swift3.0扫描条形码和二维码实例

前端之家收集整理的这篇文章主要介绍了Swift3.0扫描条形码和二维码实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Swift 3.0 Xcode 8.0 分离的代码,不能直接运行

import UIKit
import AVFoundation

class SaomaViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate {

    // 扫描相关字段
    var avSession:AVCaptureSession!
    var avInput:AVCaptureDeviceInput!
    var avOutput:AVCaptureMetadataOutput!
    var avLayer:AVCaptureVideoPreviewLayer!

    let mainFrame = UIScreen.main.bounds
    // 扫描区域的
    var scanRect:CGRect!
    var imgScan:UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let scanWidth:CGFloat = 250
        let scanTop:CGFloat = 100
        let scanLeft:CGFloat = (mainFrame.width - scanWidth) / 2
        // 第一件事,就是确定扫描的位置
        scanRect = CGRect(x: scanLeft,y: scanTop,width: scanWidth,height: scanWidth)
        // 扫描的边框
        let imgViewBorder = UIImageView(frame: scanRect)
        imgViewBorder.loadImage(named: "saomaBorder")
        self.view.addSubview(imgViewBorder)

        // 创建扫描的遮罩
        createAllMask()

        // 创建动画
        createScanAnimat()
        // 开始扫描操作
        initScan()

        let lbTips = UILabel()
        lbTips.text = "放入框中,自动扫描"
        lbTips.font = UIFont.systemFont(ofSize: 15)
        lbTips.textColor = UIColor.white

        lbTips.frame = Jiajiao.SW.cgRect(0,0,200,20)
        lbTips.textAlignment = .center
        lbTips.center = CGPoint(x: self.view.center.x,y: scanTop + scanWidth
            + 30)

        self.view.addSubview(lbTips)
    }

    // 创建扫描动画
    func createScanAnimat() {
        let viewWrap = UIView()
        viewWrap.frame = scanRect
        self.view.addSubview(viewWrap)

        imgScan = UIImageView()
        imgScan.loadImage(named: "scan_light")
        imgScan.frame = Jiajiao.SW.cgRect(1,1,scanRect.size.width - 2,5)
        viewWrap.addSubview(imgScan)
        scanAnimation()
    }

    func scanAnimation() {
        UIView.animate(withDuration: 2.5,delay: 0,options: UIViewAnimationOptions.curveLinear,animations: {
            self.imgScan.frame = Jiajiao.SW.cgRect(1,self.scanRect.size.height - 6,self.scanRect.size.width - 2,5)
        }) { (b) in
            if b {
                self.imgScan.frame = Jiajiao.SW.cgRect(1,5)
                self.scanAnimation()
            }
        }
    }

    func initScan() {

        // 检测权限
        let deviceAuth = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
        // 会直接退出当前页面
        if deviceAuth == AVAuthorizationStatus.denied || deviceAuth == AVAuthorizationStatus.restricted {
            // 请在iPhone的“设置-隐私-相机”中允许访问相机
            })
        }

        // 建立连接
        avSession = AVCaptureSession()
        avSession.sessionPreset = AVCaptureSessionPresetHigh

        do {
            let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
            avInput = try AVCaptureDeviceInput(device: device)

            avSession.addInput(avInput)

            avOutput = AVCaptureMetadataOutput()
            avSession.addOutput(avOutput)

            avOutput.setMetadataObjectsDelegate(self,queue: DispatchQueue.main)
            avOutput.MetadataObjectTypes = avOutput.availableMetadataObjectTypes

            avLayer = AVCaptureVideoPreviewLayer(session: self.avSession)
            avLayer.frame = self.view.frame
            self.view.layer.insertSublayer(avLayer,at: 0)

            let x = scanRect.origin.y / self.view.frame.size.height
            let y = scanRect.origin.x / self.view.frame.size.width
            let w = scanRect.size.height / self.view.frame.size.height
            let h = scanRect.size.width / self.view.frame.size.width

            self.avOutput.rectOfInterest = Jiajiao.SW.cgRect(x,y,w,h)

            avSession.startRunning()

        } catch {
            print(error)
        }
    }

    func createAllMask() {
        // 围着扫描框四周创建半透明遮罩,只漏出扫描框
    }

    func captureOutput(_ captureOutput: AVCaptureOutput!,didOutputMetadataObjects MetadataObjects: [Any]!,from connection: AVCaptureConnection!) {

        if MetadataObjects.count > 0 {

            self.avSession.stopRunning()

            guard let mm = MetadataObjects.first as? AVMetadataMachineReadableCodeObject else {
                // 扫描失败,请重新扫描
                return
            }
        }
    }
}

猜你在找的Swift相关文章