swift 3.0 Xcode 8.1

import UIKit

class ViewController: UIViewController {
    
    var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        imageView = UIImageView.init(frame: CGRect.init(x: 100, y: 100, width: 200, height: 200))
        imageView.image = UIImage.init(named: "123.jpeg")
        imageView.isUserInteractionEnabled = true
        self.view.addSubview(imageView)
        
        //长按识别二维码
        let longPress = UILongPressGestureRecognizer.init(target: self, action: #selector(QRLongPress(gesture:)))
        longPress.minimumPressDuration = 1
        imageView.addGestureRecognizer(longPress)
        
    }

    //MARK: - 长按二维码识别
    func QRLongPress(gesture: UILongPressGestureRecognizer) {
        
        if (gesture.state == UIGestureRecognizerState.began) {
            
            //1.初始化扫描仪,设置设别类型和识别质量
            let options = ["IDetectorAccuracy" : CIDetectorAccuracyHigh]
            let detector: CIDetector = CIDetector.init(ofType: "CIDetectorTypeQRCode", context: nil, options: options)!
            //2.扫描获取的特征组
            let features = detector.features(in: CIImage.init(cgImage: (self.imageView.image?.cgImage)!))
            //3.获取扫描结果
            let feature = features[0] as! CIQRCodeFeature
            let scannedResult = feature.messageString
            //4.获取之后的操作
            print(scannedResult!)
            
        } else if (gesture.state == UIGestureRecognizerState.ended) {
            
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}


ZhugeZhan
78 声望5 粉丝

引用和评论

0 条评论