gpt4 book ai didi

ios - 条形码扫描后屏幕卡住

转载 作者:行者123 更新时间:2023-11-28 14:16:28 24 4
gpt4 key购买 nike

我正在学习 SWIFT 并且越来越好。我拥有的条形码应用程序可以正常工作,但它会无休止地扫描条形码,直到我强制弹出屏幕“确定”、“取消”以接受或拒绝扫描。在研究中,我发现了非常相似的代码(如下所示)。所以我创建了一个只有一个 View Controller 的测试项目来尝试这段代码。它工作得很好,但一旦扫描条形码就会卡住。我的意图是解冻屏幕,接受并保存它并允许用户扫描更多条形码。任何帮助是极大的赞赏。我尝试在 found(code:) 行下方停止扫描,但它无助于解冻。我可能是错的,但我怀疑 viewilldisapper 永远不会在这里被调用。


import AVFoundation
import UIKit

class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!

override func viewDidLoad() {
super.viewDidLoad()

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()
}
}

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], 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
}


编辑后的代码:

 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)

barcodeValue = stringValue
}

// dismiss(animated: true)
previewLayer.removeFromSuperlayer()

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

let alertController = UIAlertController(title: "Barcode Scanned", message: barcodeValue!, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {(alert: UIAlertAction!) in self.restartScan()}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:nil))
present(alertController, animated: true, completion: nil)
//captureSession.startRunning()
}

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

func restartScan() {
captureSession.startRunning()
}

最佳答案

这不是卡住,当检测到第一个条形码时,您可以在 func metadataOutput(_ output: AVCaptureMetadataOutput 中使用 captureSession.stopRunning() 停止进程,您需要这个

previewLayer.removeFromSuperlayer()

而不是这个

dismiss(animated: true)

因为它是一个层而不是一个 vc 可以忽略你之前添加的

view.layer.addSublayer(previewLayer)

注意:你的 View 背景是黑色的

关于ios - 条形码扫描后屏幕卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52152388/

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