I have a SwiftUI app that I want to limit the size of a barcode scanner to a AVCaptureVideoPreviewLayer with a size of CGRect(x: 0, y: 0, width: 335, height: 150). (see image) I am using Apple Vision to detect the barcode. However barcodes that are out of the CGRect are also getting picked up and I would like to limit the area to my preview layer. I notice the pixel buffer is using the entire screen of width=1920 height=1080 as my device is an iPhone 11 Pro. Can I limit the buffer size before passing to Vision?
extension CameraViewController: AVCaptureVideoDataOutputSampleBufferDelegate {
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
return
}
print("output \(output)")
print("sampleBuffer \(sampleBuffer)")
print("connection \(connection)")
print("pixelBuffer \(pixelBuffer)")
let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation: .up)
do {
try imageRequestHandler.perform([detectBarcodeRequest])
} catch {
print(error)
}
}//captureOutput
}//extension