提高二維碼,條形碼掃描速度,使用iOS系統(tǒng)框架

Added to support different barcodes 支持二維碼和條形碼

   let supportedBarCodes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeUPCECode, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeAztecCode]

   override func viewDidLoad() {
       super.viewDidLoad()
       //關(guān)于這個(gè)屬性 請(qǐng)看我的另一篇 UI----影響View Frame的因素
       self.edgesForExtendedLayout = UIRectEdge.init(rawValue: 0)
       // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
       // as the media type parameter.
       let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
       
       do {
           // Get an instance of the AVCaptureDeviceInput class using the previous device object.
           let input = try AVCaptureDeviceInput(device: captureDevice)
           
           // Initialize the captureSession object.
           captureSession = AVCaptureSession()
           // Set the input device on the capture session.
           captureSession?.addInput(input)
//            captureDevice?.formats
        
           // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
           let captureMetadataOutput = AVCaptureMetadataOutput()
           captureSession?.addOutput(captureMetadataOutput)
           captureSession?.sessionPreset =  AVCaptureSessionPresetHigh
           // Set delegate and use the default dispatch queue to execute the call back
           captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
              captureSession?.sessionPreset =  AVCaptureSessionPresetHigh
           // Detect all the supported bar code
           captureMetadataOutput.metadataObjectTypes = supportedBarCodes
           
           // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
           videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
           videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
           videoPreviewLayer?.frame = self.view.bounds
           videoContainer.layer.addSublayer(videoPreviewLayer!)
           // Start video capture
           captureSession?.startRunning()

劃重點(diǎn) 這是設(shè)置掃描區(qū)域的范圍
【metadataOutputRectOfInterest】【rectOfInterest 】

            let width = UIScreen.main.bounds.width - 40
            let O_y = (UIScreen.main.bounds.height - width-40) / 2
            let rect = CGRect.init(x: 20, y: O_y, width: width, height: width)
            
            let innerrect = videoPreviewLayer?.metadataOutputRectOfInterest(for: rect)
        captureMetadataOutput.rectOfInterest = innerrect!
         //   這些輸出可以幫你檢查設(shè)置是否有誤 方便理解 調(diào)試
        //  print("===sp===inner\(innerrect)")
        
         //   let rect1 = videoPreviewLayer?.rectForMetadataOutputRect(ofInterest: innerrect!)
        
        //     print("====sp===\(rect1)")



給它畫個(gè)框
思路就是先畫個(gè)遮罩,然后選中一個(gè)區(qū)域,然后取反,會(huì)用ps就特別好理解

            let maskView = UIView.init(frame: self.view.bounds);
            maskView.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.4);
            self.view.addSubview(maskView)
            let maskPath = UIBezierPath.init(rect: self.view.bounds)
          
            maskPath.append(UIBezierPath.init(roundedRect: rect, cornerRadius: 10).reversing())
            
            let maskLayer = CAShapeLayer()
            
            maskLayer.path = maskPath.cgPath;
            maskView.layer.mask = maskLayer;

            
        } catch {
            // If any error occurs, simply print it out and don't continue any more.
            print(error)
            return
        }


    }

這是一個(gè)函數(shù) ,所以想拿來直接用的朋友可以從上到下代碼部分直接復(fù)制黏貼就好了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容