How to control what Camera sees
How to make the camera see only those objects that are in the frame

Hi @ThunZEN thankyou to let me know the problem I will fix it in the next version
Hi @ThunZEN Keep it open that will remind me for this
hello, i have the same question. I cropped the screen scan preview, but the scanner reads the codes outside this area. How to fix this?
The same problem, is there any way how to detect codes only from the frame? It would be very useful
Try this setupCamera function:
func setupCamera() { let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: cameraPosition)
if let selectedCamera = deviceDiscoverySession.devices.first {
if let input = try? AVCaptureDeviceInput(device: selectedCamera) {
let session = AVCaptureSession()
session.sessionPreset = .hd1920x1080//.vga640x480
if session.canAddInput(input) {
session.addInput(input)
cameraInput = input
}
if session.canAddOutput(metadataOutput) {
session.addOutput(metadataOutput)
metadataOutput.metadataObjectTypes = supportBarcode
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
}
previewLayer?.removeFromSuperlayer()
self.session = session
self.selectedCamera = selectedCamera
self.backgroundColor = UIColor.gray
// calculate a centered square rectangle with red border
//let size = 200
let screenWidth = CGFloat(1080) //bounds.size.width
let screenHeight = CGFloat(1920)
// let xPos = (CGFloat(screenWidth) / CGFloat(2)) - (CGFloat(size) / CGFloat(2))
//let scanRect = CGRect(x: Int(xPos), y: 150, width: size, height: size)
//width is height :-)
/// windows height
let w = screenWidth / 2.5
/// window width
let h = screenHeight
/// Middle on window height
let ws = screenWidth / 2 - w / 2
let scanRect = CGRect(x: ws , y: 0, width: w, height: h)
let x = scanRect.origin.x/screenWidth
let y = scanRect.origin.y/screenHeight
let width = scanRect.width/screenWidth
let height = scanRect.height/screenHeight
let scanRectTransformed = CGRect(x: x, y: y, width: width, height: height)
//create area for scanning
let scanAreaView = UIView()
scanAreaView.layer.borderColor = UIColor.red.cgColor
scanAreaView.layer.borderWidth = 4
scanAreaView.frame = scanRect
// self.addSubview(scanAreaView)
DispatchQueue.global().async {
Thread.sleep(forTimeInterval: 0.2)
session.startRunning()
DispatchQueue.main.async {
let previewLayer = AVCaptureVideoPreviewLayer(session: session)
//Preview layer je 0x0 = nelze použít metadataOutputRectConverted
//let rectOfInterest = self.previewLayer?.metadataOutputRectConverted(fromLayerRect: CGRect(x: 0, y: 0, width: 300, height: 300)) // videoPreviewLayer is AVCaptureVideoPreviewLayer
//print ("width: \(rectOfInterest?.width); heght: \(rectOfInterest?.height)")
// print ("rect of int :\(self.metadataOutput.rectOfInterest)")
self.metadataOutput.rectOfInterest = scanRectTransformed
try? selectedCamera.lockForConfiguration()
do {
self.selectedCamera!.focusMode = .continuousAutoFocus
}
selectedCamera.unlockForConfiguration()
//rectOfInterest ?? CGRect(x: 0, y: 0, width: 1, height: 1)
// print ("rect of int 2:\(self.metadataOutput.rectOfInterest)")
previewLayer.videoGravity = .resizeAspectFill
// print ("previewLayer: width: \(previewLayer.frame.width); heght: \(previewLayer.frame.height)")
self.layer.addSublayer(previewLayer)
self.previewLayer = previewLayer
self.updateCameraView()
}
}
}
}
}