JimmyLee
JimmyLee

Reputation: 569

How to make AVCaptureSession scan full screen by swift?

I have a question, have any idea to scan qrcode/barcode at everywhere in my layer.
I only put my qrcode/barcode in the middle of layer and get value.
When I put my qrcode/barcode at the right top side not work to get value.

Have any idea to fix it?

private func doInitialSetup() {
    clipsToBounds = true
    captureSession = AVCaptureSession()
        
    guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
    let videoInput: AVCaptureDeviceInput
    do {
        videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
    } catch let error {
        print(error)
        return
    }
        
    if (captureSession?.canAddInput(videoInput) ?? false) {
        captureSession?.addInput(videoInput)
    } else {
        scanningDidFail()
        return
    }
       
    let metadataOutput = AVCaptureMetadataOutput()
        
    if (captureSession?.canAddOutput(metadataOutput) ?? false) {
        captureSession?.addOutput(metadataOutput)
           
        metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        metadataOutput.metadataObjectTypes = [.qr, .ean8, .ean13, .pdf417, .code128]
        metadataOutput.rectOfInterest = UIScreen.main.bounds
    } else {
        scanningDidFail()
        return
    }
        
    self.layer.session = captureSession
    self.layer.videoGravity = .resizeAspectFill
        
    captureSession?.startRunning()
}

img

Upvotes: 0

Views: 465

Answers (1)

Gereon
Gereon

Reputation: 17844

metadataOutput.rectOfInterest = UIScreen.main.bounds

is certainly incorrect, this should be a CGRect containing normalized (i.e 0.0 ... 1.0) values.

Just remove this line, the default is "full screen" as you want anyway.

Upvotes: 1

Related Questions