// 输入设备 fileprivate lazy var input: AVCaptureDeviceInput? = { let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) return try? AVCaptureDeviceInput(device: device) }() // 输出对象 fileprivate lazy var output : AVCaptureMetadataOutput = { let output = AVCaptureMetadataOutput() output.metadataObjectTypes = output.availableMetadataObjectTypes output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main) let centerViewFrame = self.centerView.frame let screenFrame = UIScreen.main.bounds // 测试发现系统是按照右上角作为参照, 并不是以左上角 let x = centerViewFrame.origin.y / screenFrame.size.height let y = centerViewFrame.origin.x / screenFrame.size.width let width = centerViewFrame.size.height / screenFrame.size.height let height = centerViewFrame.size.width / screenFrame.size.width output.rectOfInterest = CGRect(x: x, y: y, width: width, height: height) return output }() // 会话 fileprivate lazy var session = AVCaptureSession() // 预览图层 fileprivate lazy var previewlayer: AVCaptureVideoPreviewLayer = { let layer = AVCaptureVideoPreviewLayer(session: self.session) layer?.frame = self.view.bounds return layer! }()
// MARK: - 二维码扫描 extension QRCodeViewController: AVCaptureMetadataOutputObjectsDelegate { fileprivate func setupScanQRCode() { if !session.canAddInput(input) || !session.canAddOutput(output) { return } session.addInput(input) session.addOutput(output) view.layer.insertSublayer(previewlayer, at: 0) session.startRunning() } func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) { for objc in metadataObjects { let tempObjc = objc as! AVMetadataMachineReadableCodeObject CCLog(tempObjc.stringValue) } } }
不走后面的代理方法, 如果是因为没有扫描到数据而没有走代理方法, 那这里写的有什么问题吗? 望指出
付费偷看金额在0.1-10元之间
一周热门 更多>