Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iPhone 14 Pro Minimum Focus Distance Scanning Issues #68

Open
MikeMilzz opened this issue Oct 14, 2022 · 6 comments
Open

iPhone 14 Pro Minimum Focus Distance Scanning Issues #68

MikeMilzz opened this issue Oct 14, 2022 · 6 comments

Comments

@MikeMilzz
Copy link
Contributor

The minimum focus distance on the default camera in the iPhone 14Pro has changed and can result in barcodes that are too far away to successfully scan. This Apple Developer forum thread links to a 2021 WWDC session and sample code for addressing this issue.

@MikeMilzz
Copy link
Contributor Author

MikeMilzz commented Nov 30, 2022

For anyone looking for a fix to this issue, I'm currently testing the following change in CameraPreview.swift

In SetupCamera() and setCamera(), add .builtInUltraWideCamera to the array. So far in testing this uses the macro lens on iPhone 14 and on iPhoneSE2 the main (and only) camera will still be found and no errors are produced. I'll continue testing on various devices before submitting a PR.

let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInUltraWideCamera, .builtInWideAngleCamera], mediaType: AVMediaType.video, position: cameraPosition)

@MikeMilzz
Copy link
Contributor Author

MikeMilzz commented Dec 13, 2022

After more testing, I discovered that the iPhone 13 will provide the Ultra Wide lens as well but it is significantly poorer quality resulting in a worse scanning situation. Given that, I'm testing the following solution to ONLY provide UltraWide to iPhone 14 Pro.

provide this function somewhere CameraPreview can access it.

var unameMachine: String { var utsnameInstance = utsname() uname(&utsnameInstance) var optionalString: String? = withUnsafePointer(to: &utsnameInstance.machine) { $0.withMemoryRebound(to: CChar.self, capacity: 1) { ptr in String.init(validatingUTF8: ptr) } } let parsedString = optionalString?.components(separatedBy: ",") var validPart = parsedString?.first if let reallyValidPart = validPart { if let range = reallyValidPart.range(of: "iPhone") { validPart!.removeSubrange(range) optionalString = validPart } } return optionalString ?? "N/A" }

Then in CameraPreview.swift in func setupCamera()
#if !targetEnvironment(macCatalyst) if let validModelInt = Int(unameMachine) { // check for < iPhone14 and if so then don't try UltraWideCamera if validModelInt >= 15 { // only use UltraWideCamera on iPhone14 and newer //print("!!! Device Name: \(unameMachine) and using UltraWide") deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInUltraWideCamera, .builtInWideAngleCamera, .builtInTrueDepthCamera], mediaType: AVMediaType.video, position: cameraPosition) //.builtInWideAngleCamera } else { //print("!!! Device Name: \(unameMachine) and using WideAngle") deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .unspecified) } } else { //print("!!! Device Name: \(unameMachine) NOT VALID so using WideAngle or Dual") deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera, .builtInDualCamera], mediaType: AVMediaType.video, position: .unspecified) } #else // position needs to be .unspecified in macOS and no devices will be found deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTrueDepthCamera, .builtInWideAngleCamera], mediaType: AVMediaType.video, position: .unspecified) #endif

The performance on macOS is still poor even with Catalyst enabled, but it's functional just not fast.

@heart
Copy link
Owner

heart commented Dec 15, 2022

Sorry I only have an iPhone 12
If you know the problem please tell me more information

@xaviRodri
Copy link

Hey! Any update on this?
I've found this thread on Apple's support https://developer.apple.com/forums/thread/719885
I have an iPhone 14 Pro I could test this on.. so let me know if I can help here 👍

@MikeMilzz
Copy link
Contributor Author

Hey! Any update on this?
I've found this thread on Apple's support https://developer.apple.com/forums/thread/719885
I have an iPhone 14 Pro I could test this on.. so let me know if I can help here 👍

In CameraPreview, under setupCamera() I added the following lines to test for MacCatalyst and if not then to use this array of camera options. It's still slow to launch in Catalyst but it does work on the Mac and the new camera options work for my 14Pro and 13 test devices.

#if !targetEnvironment(macCatalyst)
        deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTripleCamera, .builtInDualWideCamera, .builtInUltraWideCamera, .builtInWideAngleCamera, .builtInTrueDepthCamera], mediaType: AVMediaType.video, position: cameraPosition) // based on https://developer.apple.com/forums/thread/719885 to resolve iPhone 14 Pro UltraWideCamera 
        #else // position needs to be .unspecified in macOS and no devices will be found
        deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTrueDepthCamera, .builtInWideAngleCamera], mediaType: AVMediaType.video, position: .unspecified)
        #endif

@heart
Copy link
Owner

heart commented Jul 12, 2023

Hello everyone
I'm sorry I didn't come to debug for everyone.
I'm still here and I want to continue developing this project. But I've been pretty busy lately.

I run my own startup and over the past year I have worked very hard to make my startup a success.

You all can send PR to this project.
But I don't want to pressure everyone to solve these problems, I will try to come back to solve this problem as soon as possible.

I'm sorry again for being gone for a long time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants