swift-二维码扫描实现

override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = COLORA(255, g: 255, b: 255, a: 0.3)

        let boundImageView = UIImageView(frame:CGRectMake((SCREEN_WIDTH240)/2, 200, 240, 240))

        var image = IMAGE(“qrcode_border”)

        //拉伸图片

        image = image.resizableImageWithCapInsets(UIEdgeInsetsMake(25, 25, 25, 25))

        boundImageView.image = image

        self.view.addSubview(boundImageView)

        //减掉超出边界的内容

        boundImageView.clipsToBounds = true

        self.animationImgView.frame = CGRectMake(0, 20, 240, 20)

        //来回扫描的图片

        self.animationImgView.image = IMAGE(“barscan_cursor”)

        boundImageView.addSubview(self.animationImgView)

        let timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector(“changeImageFrame:”), userInfo: nil, repeats: true) //NSTimer(timeInterval: 0.03, target: self, selector: Selector(“changeImageFrame:”), userInfo: nil, repeats: true)

        timer.fire()

        self.initCapture()

    }

private func initCapture(){

        //获取摄像设备

        let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

        let error:NSError?

        //创建输入流

        var input : AVCaptureDeviceInput = AVCaptureDeviceInput()

        do {

            input = try AVCaptureDeviceInput(device: device)

        } catch let error as NSError {

            print(error.debugDescription)

        }

        //创建输出流

        let output = AVCaptureMetadataOutput.init()

        //设置代理 在主线程刷新

        output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())

        //初始化链接对象

        self.session = AVCaptureSession.init()

        //高质量采集率

        self.session.canSetSessionPreset(AVCaptureSessionPresetHigh)

        self.session.addInput(input)

        self.session.addOutput(output)

        //设置扫码支持的编码格式(设置条形码和二维码兼容)

        output.metadataObjectTypes = [AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode128Code]

        let layer = AVCaptureVideoPreviewLayer.init(session: self.session)

        layer.videoGravity = AVLayerVideoGravityResizeAspectFill

        //扫描框的位置和大小

        layer.frame = CGRectMake((SCREEN_WIDTH240)/2, 200, 240, 240)

        self.view.layer.insertSublayer(layer, atIndex: 0)

        //开始捕获

        self.session.startRunning()

    }

//实现代理方法

extension BarCodeViewController:AVCaptureMetadataOutputObjectsDelegate{

    func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {

        var stringValue:String?

        if metadataObjects.count>0 {

            var metadataObject = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

            stringValue = metadataObject.stringValue

            print(stringValue)

            if stringValue != nil {

                self.session.stopRunning()

            }

        }

        self.session.stopRunning()

    }

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注