快速拍摄扫描的条形码

时间:2019-02-26 07:09:27

标签: ios swift avfoundation qr-code avcaptureoutput

我用 AVCaptureStillImageOutput 拍摄了扫描条形码的照片。

问题在于,有时它可以很好地获取图像,并且大多会出现错误

  

Error Domain = AVFoundationErrorDomain代码= -11800“该操作可能   未完成” UserInfo = {NSLocalizedFailureReason =未知错误   发生(-16800),NSLocalizedDescription =无法执行该操作   已完成,NSUnderlyingError = 0x281b8bba0 {错误   Domain = NSOSStatusErrorDomain Code = -16800“(null)”}}

这是我的代码

开始阅读代码

func startReading() -> Bool {
        let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
        do {
            let input = try AVCaptureDeviceInput(device: captureDevice)
            captureSession = AVCaptureSession()
            captureSession?.addInput(input)
            // Do the rest of your work...
        } catch let error as NSError {
            // Handle any errors
            print(error)
            return false
        }

        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        videoPreviewLayer.frame = viewPreview.layer.bounds

        self.stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]

        if (captureSession?.canAddOutput(self.stillImageOutput))! {
            captureSession?.addOutput(self.stillImageOutput)
        }

        viewPreview.layer.addSublayer(videoPreviewLayer)

        /* Check for metadata */
        let captureMetadataOutput = AVCaptureMetadataOutput()
        captureSession?.addOutput(captureMetadataOutput)
        captureMetadataOutput.metadataObjectTypes = captureMetadataOutput.availableMetadataObjectTypes
        print(captureMetadataOutput.availableMetadataObjectTypes)
        captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        captureSession?.startRunning()

        return true
    }

停止阅读代码

func stopReading() {
    captureSession?.stopRunning()
    captureSession = nil
    videoPreviewLayer.removeFromSuperlayer()
}

捕获委托方法

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

        for data in metadataObjects {
            let metaData = data as! AVMetadataObject
            print(metaData.description)
            let transformed = videoPreviewLayer?.transformedMetadataObject(for: metaData) as? AVMetadataMachineReadableCodeObject
            if let unwraped = transformed {
                print(unwraped.stringValue)
                lblString.text = unwraped.stringValue
                btnStartStop.setTitle("Start", for: .normal)

                if let videoConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) {
                    stillImageOutput.captureStillImageAsynchronously(from: videoConnection) {
                        (imageDataSampleBuffer, error) -> Void in

                        if imageDataSampleBuffer != nil{
                            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer!)

                            if let image = UIImage(data: imageData!){

                                print(image) // Image is Here
                            }
                        }else{
                            print(imageDataSampleBuffer)
                            print(error)
                        }
                    }
                }
                self.performSelector(onMainThread: #selector(stopReading), with: nil, waitUntilDone: false)
                isReading = false;
            }
        }
    }

0 个答案:

没有答案