二维码(设置设别区域)DEMO

二维码Demo

     
    @IBOutlet weak var QROutputLabel: UILabel!
    var captureSession:AVCaptureSession?
    var videoPreviewLayer:AVCaptureVideoPreviewLayer?
    var qrCodeFrameView:UIView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
        // as the media type parameter.
        let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        
        // Get an instance of the AVCaptureDeviceInput class using the previous device object.
        
        do {
            
            let input: AnyObject! =
            try AVCaptureDeviceInput(device: captureDevice)
            
            // Initialize the captureSession object.
            captureSession = AVCaptureSession()
            // Set the input device on the capture session.
            captureSession?.addInput(input as! AVCaptureInput)
            // Do any additional setup after loading the view.
            
        } catch let error as NSError {
            
            printLog("\(error.localizedDescription)")
            
            return
        }
        
        // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
        
        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
        videoPreviewLayer?.frame = view.layer.bounds
        view.layer.addSublayer(videoPreviewLayer!)
        let captureMetadataOutput = AVCaptureMetadataOutput()
        //标记识别区域
        captureMetadataOutput.rectOfInterest = CGRectMake(0.2, 0.2, 0.5, 0.5)
        /* 
        let cover = UIView(frame: CGRect(x: 0.2 * screenWidth, y: 0.2 * screenHeight, width: 0.5 * screenWidth, height: 0.5 * screenHeight))
        cover.backgroundColor = UIColor.blueColor()
        view.addSubview(cover)
        view.bringSubviewToFront(cover)
        */
        captureSession?.addOutput(captureMetadataOutput)
        
        // Set delegate and use the default dispatch queue to execute the call back
        captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
        captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
        
        // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
        
        // Start video capture.
        captureSession?.startRunning()

        qrCodeFrameView = UIView()
        qrCodeFrameView?.layer.borderColor = UIColor.greenColor().CGColor
        qrCodeFrameView?.layer.borderWidth = 2
        view.addSubview(qrCodeFrameView!)
        view.bringSubviewToFront(qrCodeFrameView!)
        view.bringSubviewToFront(QROutputLabel)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
        // Check if the metadataObjects array is not nil and it contains at least one object.
        if metadataObjects == nil || metadataObjects.count == 0 {
            qrCodeFrameView?.frame = CGRectZero
            QROutputLabel.text = "No QR code is detected"
            return
        }
        
        // Get the metadata object.
        let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
        
        if metadataObj.type == AVMetadataObjectTypeQRCode {
            // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
            let barCodeObject =
            videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj
                as AVMetadataMachineReadableCodeObject) as!
            AVMetadataMachineReadableCodeObject
            qrCodeFrameView?.frame = barCodeObject.bounds
            
            if metadataObj.stringValue != nil {
                QROutputLabel.text = metadataObj.stringValue
                captureSession?.stopRunning()
            }
        }
    }


必须继承AVCaptureMetadataOutputObjectsDelegate 实现识别二维码功能 captureOutput()
captureMetadataOutput.rectOfInterest 设置二维码的识别区域 CGRect(y / screenH, x / screenW, h / screenH, w / screenW) 一定要注意 第一个是Y坐标/屏幕高,第二个参数是X坐标/屏幕宽,之后就是高,宽的比,总之取值范围就是0...1

你可能感兴趣的:(二维码(设置设别区域)DEMO)