I am building a UIView
that has an overlaid box and the end-goal is to have the QR Code reader only fire when the QR falls within the box. I know that I need to set the .rectOfInterest()
to be the same as the yellow box, but in the current implementation (code below), the reader doesn't work.
public override init(frame: CGRect) {
super.init(frame: frame)
if let captureDevice = AVCaptureDevice.default(for: .video) {
do {
let input = try AVCaptureDeviceInput(device: captureDevice)
session.addInput(input)
} catch {
print("Error")
}
let scannerRect = CGRect(x: self.center.x - (self.frame.width * 0.667 / 2), y: self.frame.width * 0.667 / 4, width: self.frame.width * 0.667, height: self.frame.width * 0.667)
let output = AVCaptureMetadataOutput()
output.rectOfInterest = scannerRect
session.addOutput(output)
output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
output.metadataObjectTypes = [.qr]
video = AVCaptureVideoPreviewLayer(session: session)
video.frame = self.layer.bounds
scannerBounds.frame = scannerRect
scannerBounds.borderColor = UIColor.yellow.cgColor
scannerBounds.borderWidth = 5
self.layer.addSublayer(video)
self.layer.insertSublayer(scannerBounds, above: video)
}
}
Please help me align the box and the qr code reader.