WeScan 相机自动对焦或快速选择相机

问题描述 投票:0回答:1

我使用 WeScan git repo 在 Swift 中制作了一个成分扫描应用程序,因为我只想一次扫描一张图像,并在尝试使用本机库时发现了问题。

https://github.com/WeTransfer/WeScan/

一切都已完成,但我正在一台只有 1 个后置摄像头的旧 iPhone 上进行测试。在 iPhone 15 上进行测试后,我意识到后置摄像头不是自动对焦,而且我在库中看不到在哪里定义要使用哪个摄像头(它需要最接近的视觉摄像头来扫描小文本)或如何制作它自动对焦(例如,就像在本机相机应用程序中一样,您点击自动对焦,它将切换到适当的相机)。

我一直在尝试使用 ImageScannerController 和 CameraScannerViewController 来尝试找到 autoFocus 或 selectCamera 方法,但无济于事。

func scanFromCamera() {
    let scannerViewController = ImageScannerController()
    scannerViewController.imageScannerDelegate = self
    scannerViewController.modalPresentationStyle = .popover
    scannerViewController.navigationBar.backgroundColor = .black
    scannerViewController.navigationBar.tintColor = .white
    scannerViewController.toolbar.backgroundColor = .black
    present(scannerViewController, animated: true)
    
}

 private func configureOCR() {
    ocrRequest = VNRecognizeTextRequest { (request, error) in
        guard let observations = request.results as? [VNRecognizedTextObservation] else { return }
        
        var ocrText = ""
        for observation in observations {
            guard let topCandidate = observation.topCandidates(1).first else { return }
            
            ocrText += topCandidate.string + "\n"
        }
        
        DispatchQueue.main.async {
            self.scannedIngedients = ocrText;
            self.performSegue(withIdentifier: "ScanResults", sender: self)
        }
    }
    
    ocrRequest.recognitionLevel = .accurate
    ocrRequest.recognitionLanguages = ["en-US", "en-GB", "en-AU"]
    ocrRequest.usesLanguageCorrection = true
}

private func processImage(_ image: UIImage) {
    guard let cgImage = image.cgImage else { return }
    
    let requestHandler = VNImageRequestHandler(cgImage: cgImage, options: [:])
    do {
        try requestHandler.perform([self.ocrRequest])
    } catch {
        print(error)
    }
}

@objc func openCamera(sender: Any) {
    scanFromCamera()
}

非常感谢。

ios swift camera ocr autofocus
1个回答
0
投票

我已经修复了它,将其放在这里供其他有相同问题的人使用,或者希望贡献者可以将其添加为应用程序的功能来选择要使用的镜头。

  1. 我下载了 git 的本地副本

  2. 在 Xcode 中,我从 git 中删除了对包的依赖关系(单击主蓝色项目图标、包依赖项、WeScan,然后单击 - )

  3. 根据本地副本添加了包依赖项(这样我就可以更改包的文件)-XCode,文件,添加包依赖项,单击“添加本地”按钮,浏览本地副本并添加

  4. 以下 4 个文件是我发现代码选择 AVCaptureDevice(在 Scan 和 Session 文件夹中)的位置:CameraScannerViewController、CaptureSessionManager、ScannerViewController、CaptureSession

  5. 搜索 AVCaptureDevice 并将其替换为:guard let device = AVCaptureDevice.default(for: AVMediaType.video) 并将以下内容替换为 CameraScannerViewController 和 ScannerViewController:

    let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTripleCamera, .builtInDualWideCamera, .builtInDualCamera, .builtInWideAngleCamera], mediaType: .video, 位置: .back) 让 selectedDevice = discoverySession.devices.first 守卫 让 device = selectedDevice else { return }

  6. 将其添加到 CaptureSession:

     let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTripleCamera, .builtInDualWideCamera, .builtInDualCamera, .builtInWideAngleCamera], mediaType: .video, position: .back)
    

    让 selectedDevice = discoverySession.devices.first self.device = 选定的设备

  7. 并将其添加到 CaptureSessionManager 中:

    让 discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTripleCamera, .builtInDualWideCamera, .builtInDualCamera, .builtInWideAngleCamera], mediaType: .video, 位置: .back)

    let selectedDevice = discoverySession.devices.firstguard let device = selectedDevice else { let error = ImageScannerControllerError.inputDevice delegate?.captureSessionManager(self, didFailWithError: error) return nil }

我发现一些有用的链接:

https://teng.pub/technical/2022/6/9/let-ios-decide-the-best-lens-to-use https://developer.apple.com/documentation/avfoundation/capture_setup/choosing_a_capture_device

© www.soinside.com 2019 - 2024. All rights reserved.