簡體   English   中英

VNDetectBarcodesRequest 不適用於 iOS16

[英]VNDetectBarcodesRequest not working on iOS16

我在 iOS 16 上遇到VNDetectBarcodesRequest問題。我的代碼在 iOS 15 上按預期工作,但在 iOS 16 上它沒有在圖像中找到任何條形碼。

我已將我的代碼分離到一個操場上,並且在這里遇到了同樣的問題。 在 Xcode 13.4.1 的操場上運行下面的代碼,我得到了結果:

“谷歌鏈接:可選(“https://www.google.com”)”

在 Xcode 14 上運行相同的代碼,結果nil 在帶有 Xcode 14 的 iOS15 模擬器中運行它會給出一個肯定的結果,只有在 iOS16 和 playground 上它不讀取 QR 碼。

添加到它,也不會拋出異常。

有沒有人經歷過同樣的事情並設法解決這個問題?

這是我的游樂場代碼:

import UIKit
import Vision

extension UIImage {
    func qrCodeLink(completion: @escaping (String?) -> Void) {
        guard let ciImage = CIImage(image: self) else {
            completion(nil)
            return
        }
        let imageRequestHandler = VNImageRequestHandler(ciImage: ciImage,
                                                        orientation: .up,
                                                        options: [:])       
        let request = VNDetectBarcodesRequest { (request,error) in
            guard error == nil else {
                completion(nil)
                return
            }
            
            guard let observations = request.results as? [VNDetectedObjectObservation] else {
                completion(nil)
                return
            }
            
            let result = (observations.first as? VNBarcodeObservation)?.payloadStringValue
            completion(result)
        }
        try? imageRequestHandler.perform([request])
    }
}

if let google = UIImage(named: "google") {
    google.qrCodeLink { link in
        debugPrint("Google link: \(link)")
    }
} else {
    debugPrint("No google image")
}

通過上面的代碼,我使用了這張圖片,它只是指向https://www.google.com的鏈接: 谷歌

我想我找到了問題所在。 在 Xcode 14 和 iOS 16 上運行請求時,請求修訂在VNDetectBarcodesRequestRevision3上運行(尚未在VNDetectBarcodesRequest頁面上記錄)。 但是,使用VNDetectBarcodesRequestRevision1VNDetectBarcodesRequestRevision2有效。

在執行任務之前添加以下內容對我有用:

request.revision = VNDetectBarcodesRequestRevision1

詳細說明@Paul Peelen 的解決方案,以確保僅在需要時使用解決方法(Xcode 14 + iOS 16 + 模擬器),我們使用了:

#if targetEnvironment(simulator) && compiler(>=5.7)
if #available(iOS 16, *) {
    request.revision = VNDetectBarcodesRequestRevision1
}
#endif

在為 iOS 15 或 16 進行編譯時,我發現在掃描帶邊框的條碼或不帶邊框的條碼時,VNDetectedBarcodesRequest 行為有所不同。 那個邊框似乎在開始時觸發掃描代碼。

帶邊框:它可以正確掃描黑色背景圖像和白色背景圖像。

WITHOUT a border:它可以正確掃描顯示器上黑色背景的條碼圖像,但是當該圖像打印在白紙上並掃描時會失敗。 無邊框 = 無條形碼。

新問題:當它掃描那個單邊條形碼時,它返回了一組 17 個相同的條形碼觀察結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM