gpt4 book ai didi

ios - Swift 4 - 如何扫描二维码

转载 作者:搜寻专家 更新时间:2023-10-31 22:40:53 24 4
gpt4 key购买 nike

<分区>

我正在尝试使用我的应用程序(Swift 4、iOS 11)扫描 QR 码,并且我遵循了本教程:https://www.hackingwithswift.com/example-code/media/how-to-scan-a-barcode

但是当我运行我的应用程序时,相机确实出现了,但是我的 captureOutput 方法没有被调用,我出于某些原因改变了我的那个方法:

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!)

对此

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!)

仍然没有被调用。我做错了什么?

这是我所有的代码:

import AVFoundation
import UIKit

class QRCode: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

view.backgroundColor = UIColor.black
captureSession = AVCaptureSession()

guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
let videoInput: AVCaptureDeviceInput

do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
return
}

if (captureSession.canAddInput(videoInput)) {
captureSession.addInput(videoInput)
} else {
failed()
return
}

let metadataOutput = AVCaptureMetadataOutput()

if (captureSession.canAddOutput(metadataOutput)) {
captureSession.addOutput(metadataOutput)

metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
metadataOutput.metadataObjectTypes = [.ean8, .ean13, .pdf417]
} else {
failed()
return
}

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer)

captureSession.startRunning()
}

func failed() {
let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
captureSession = nil
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

if (captureSession?.isRunning == false) {
captureSession.startRunning()
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

if (captureSession?.isRunning == true) {
captureSession.stopRunning()
}
}

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

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
captureSession.stopRunning()

if let metadataObject = metadataObjects.first {
guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
guard let stringValue = readableObject.stringValue else { return }
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
found(code: stringValue)
}

dismiss(animated: true)
}

func found(code: String) {
print(code)
}

override var prefersStatusBarHidden: Bool {
return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}


}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com