代码之家  ›  专栏  ›  技术社区  ›  Dharma

条码扫描后,屏幕冻结

  •  0
  • Dharma  · 技术社区  · 6 年前

    我学得很快,而且越来越好。我使用的条码应用程序可以工作,但它会不断地扫描条码,直到我强制弹出一个屏幕“确定”、“取消”来接受或拒绝扫描。在研究中,我发现了非常相似的代码(如下所示)。所以我用一个视图控制器创建了一个测试项目来尝试这个代码。它工作得很好,但一旦扫描条码就会冻结。我的目的是解冻屏幕,接受并保存这个,允许用户扫描更多的条形码。非常感谢您的帮助。我尝试停止扫描找到的行(代码:)下,但它没有帮助解冻。我可能是错的,但我怀疑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()
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Shehata Gamal    6 年前

    captureSession.stopRunning() func metadataOutput(_ output: AVCaptureMetadataOutput

    previewLayer.removeFromSuperlayer()
    

    dismiss(animated: true)
    

    view.layer.addSublayer(previewLayer)