Andrew
Andrew

Reputation: 622

iOS How to close camera after successfully scanning QR code Swift 4

How I can close camera after successfully scanning QR code? My problem is when I scan code from QR, camera is not closed after scanning. How I can close camera? My code:

var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
var qrCode = String()

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
    }

    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)
        qrCode = code
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

Button which open camera:

 @IBAction func scanQrButton(_ sender: Any) {
    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 = [.qr]
    } else {
        failed()
        return
    }

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

    captureSession.startRunning()
}

As you can see, I have code which opens the camera and scan QR, but I want to close camera after the code is scanned without segue.

Upvotes: 4

Views: 5318

Answers (2)

adrgrondin
adrgrondin

Reputation: 668

You can use the instance method stopRunning() on your session after you perform the scan to stop the capture :

captureSession.stopRunning()

And then dismiss the view with the preview of the camera output (here removeFromSuperlayer() will work I think). You can use the following method to detect when a QR code was scanned after provided by the AVCaptureMetadataOutputObjectsDelegate:

optional func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection)

You should also put startRunning() on a serial queue as stated by Apple documentation :

The startRunning() method is a blocking call which can take some time, therefore you should perform session setup on a serial queue so that the main queue isn't blocked (which keeps the UI responsive).

Upvotes: 2

yveszenne
yveszenne

Reputation: 746

Have you tried this?

self.captureSession?.stopRunning()

Upvotes: 2

Related Questions