So I've been playing around with this: https://apps.apple.com/us/app/qr-code-barcode-scanner/id1048473097
barcode app. I've noticed that it's focusing near all while still maintaining autofocus. Furthermore, the object in front of the camera has nearly no motion blur (when moved around).
So I went ahead and made some changes to my barcode/QR/data matrix code to be similar to the app. The reason I did so is that their app was able to capture a REALLY small data matrix code (can't show here) while my app wasn't. After the tweaking, I am now able to read the SMALL data matrix code but not as fast as them. Mine can do it as fast (if not faster) if it's placed in the right area but theirs is more consistent and doesn't require as much "finagel-ing" to find the position for the data matrix code to then be extracted.
Below is my AVFoundation code.
view.backgroundColor = UIColor.black
captureSession = AVCaptureSession()
captureSession.sessionPreset = AVCaptureSession.Preset.hd1920x1080;
guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
do {
try videoCaptureDevice.lockForConfiguration()
} catch {
return
}
// videoCaptureDevice.focusMode = AVCaptureDevice.FocusMode.autoFocus
videoCaptureDevice.autoFocusRangeRestriction = .near
// videoCaptureDevice.activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: 30)
// videoCaptureDevice.activeVideoMinFrameDuration = CMTimeMake(value: 1, timescale: 30)
// videoCaptureDevice.focusMode = AVCaptureDevice.FocusMode.continuousAutoFocus
// videoCaptureDevice.setFocusModeLocked(lensPosition: 0.45) { (hi) in
// print(hi)
// }
videoCaptureDevice.focusMode = .continuousAutoFocus
videoCaptureDevice.unlockForConfiguration()
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 = types
} else {
failed()
return
}
types just holds the ObjectType (e.g: [.qr, .dataMatrix, .etc])
If there is anything you guys see that would make this more physically consistent with a smaller data matrix barcode, that would be awesome!