博客> swift4二维码、条形码扫描
swift4二维码、条形码扫描
2019-09-16 06:11 评论:0 阅读:467 陈正其
ios swift 二维码 Swift4 条形码

小伙伴们开发常常会有扫描二维码或条形码的需求,那么让我们来看看在swift开发中怎么扫描二维码、条形码。 准备工作 1.在info.plist文件中添加Privacy - Camera Usage Description(访问首相头权限)!  A73AF415-B371-4D04-94E4-5430D99482EE.png

2.创建一个二维码扫描控制器并引入AVFoundation 框架以及实现AVCaptureMetadataOutputObjectsDelegate, UIAlertViewDelegate协议

3.正式代码来了 var scanRectView:UIView! var device:AVCaptureDevice! var input:AVCaptureDeviceInput! var output:AVCaptureMetadataOutput! var session:AVCaptureSession! var preview:AVCaptureVideoPreviewLayer! var line = UIImageView()/*上下滚动的扫描线/ private var zhs_timer : Timer! override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title="车位锁扫描"

    do{
        self.device = AVCaptureDevice.default(for: AVMediaType.video)
        self.input = try AVCaptureDeviceInput(device: device)
        self.output = AVCaptureMetadataOutput()
        output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        self.session = AVCaptureSession()
        if UIScreen.main.bounds.size.height<500 xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed label.text = "请将车位锁二维码放入框内、即可自助扫描" xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed> 0 {
        let metadataObject = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
        stringValue = metadataObject.stringValue
        print(metadataObjects)
        if stringValue != nil{
            self.session.stopRunning()
        }
    }
    self.session.stopRunning()
    //输出结果
    let alertController = UIAlertController(title: "扫描结果", message: stringValue,preferredStyle: .alert)
    let okAction = UIAlertAction(title: "确定", style: .default, handler: {
        action in
        self.session.startRunning()
    })
    alertController.addAction(okAction)
    self.present(alertController, animated: true, completion: nil)

}

/** 开启定时器*/
private func openTimer()  {

    self.zhs_timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(timerRuning), userInfo: nil, repeats: true)
}
/** 定时器跑起来*/
@objc private func timerRuning () {

    print(self.scanRectView.X+H(5))
    print(self.scanRectView.Y)
    print(self.scanRectView.Width)
    print((self.scanRectView.Width)/960*36)
    line.frame = CGRect(x: self.scanRectView.X+H(5), y: self.scanRectView.Y, width: self.scanRectView.Width, height: (self.scanRectView.Width)/960*36)

    UIView.animate(withDuration: 3) {
        self.line.frame = CGRect(x: self.scanRectView.X, y: self.scanRectView.MaxY-H(5), width: self.scanRectView.Width, height: 4)
    }
}

func openTorch(){/**开启关闭手电筒*/

    guard AVCaptureDevice.default(for: .video) != nil else{
        return
    }
    if device.hasFlash && device.isTorchAvailable {
        try? device.lockForConfiguration()
        device.torchMode = device.torchMode == .off ? .on : .off
        device.unlockForConfiguration()
    }
}

好了,到这里功能就基本实现了,想要dome的小伙伴欢迎到[ Enter your link description here: ]( Enter your link description here: )下载!要是能给个星星就完美了!!!

收藏
0
sina weixin mail 回到顶部