1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| fileprivate func addScaningVideo(){ guard let device = AVCaptureDevice.default(for: .video) else { return } guard let deviceInput = try? AVCaptureDeviceInput(device: device) else { return } let metadataOutput = AVCaptureMetadataOutput() metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main) if session.canAddInput(deviceInput) { session.addInput(deviceInput) } if session.canAddOutput(metadataOutput) { session.addOutput(metadataOutput) } metadataOutput.metadataObjectTypes = [.qr, .code128, .code39, .code93, .code39Mod43, .ean8, .ean13, .upce, .pdf417, .aztec] let previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: session) previewLayer.videoGravity = .resizeAspectFill previewLayer.frame = view.bounds view.layer.insertSublayer(previewLayer, at: 0) let rect = CGRect(x: scanImageView.frame.minY / kScreenHeight, y: scanImageView.frame.minX / kScreenWidth, width: scanImageView.frame.height / kScreenHeight, height: scanImageView.frame.width / kScreenWidth) metadataOutput.rectOfInterest = rect session.startRunning() }
|