我一直在实施对苹果在WWDC2017中引入的新Vision视觉框架的测试。我专门研究条形码检测 - 从相机/图库扫描图像后,我能够得到它是否是条形码图像。但是,在查看barcodeDescriptor时,我看不到实际的条形码值或有效负载数据。在https://developer.apple.com/documentation/coreimage/cibarcodedescriptor页面上似乎没有任何内容可以识别任何属性。视觉框架条码检测iOS 11
我得到这些错误:
- 无法连接到远程服务:错误域= NSCocoaErrorDomain代码= 4097 “连接服务命名为
com.apple.BarcodeSupport.BarcodeNotificationService”- libMobileGestalt MobileGestalt.c:555:无法访问InverseDeviceID(请参阅问题/ 11744455>)
- 连接到名为com.apple.BarcodeSupport.BarcodeNotificationService的服务错误
域= NSCocoaErrorDomain代码= 4097
有什么办法来访问从VNBarcodeObservation条形码价值? 任何帮助将不胜感激。谢谢! 这里是我使用的代码:
@IBAction func chooseImage(_ sender: Any) {
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
@IBAction func takePicture(_ sender: Any) {
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)){
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
self .present(imagePicker, animated: true, completion: nil)
}
else{
let alert = UIAlertController(title: "Warning", message: "Camera not available", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
//PickerView Delegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker .dismiss(animated: true, completion: nil)
classificationLabel.text = "Analyzing Image…"
guard let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
else { fatalError("no image from image picker") }
guard let ciImage = CIImage(image: pickedImage)
else { fatalError("can't create CIImage from UIImage") }
imageView.image = pickedImage
inputImage = ciImage
// Run the rectangle detector, which upon completion runs the ML classifier.
let handler = VNImageRequestHandler(ciImage: ciImage, options: [.properties : ""])
DispatchQueue.global(qos: .userInteractive).async {
do {
try handler.perform([self.barcodeRequest])
} catch {
print(error)
}
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
picker .dismiss(animated: true, completion: nil)
print("picker cancel.")
}
lazy var barcodeRequest: VNDetectBarcodesRequest = {
return VNDetectBarcodesRequest(completionHandler: self.handleBarcodes)
}()
func handleBarcodes(request: VNRequest, error: Error?) {
guard let observations = request.results as? [VNBarcodeObservation]
else { fatalError("unexpected result type from VNBarcodeRequest") }
guard observations.first != nil else {
DispatchQueue.main.async {
self.classificationLabel.text = "No Barcode detected."
}
return
}
// Loop through the found results
for result in request.results! {
// Cast the result to a barcode-observation
if let barcode = result as? VNBarcodeObservation {
// Print barcode-values
print("Symbology: \(barcode.symbology.rawValue)")
if let desc = barcode.barcodeDescriptor as? CIQRCodeDescriptor {
let content = String(data: desc.errorCorrectedPayload, encoding: .utf8)
// FIXME: This currently returns nil. I did not find any docs on how to encode the data properly so far.
print("Payload: \(String(describing: content))\n")
print("Error-Correction-Level: \(desc.errorCorrectedPayload)\n")
print("Symbol-Version: \(desc.symbolVersion)\n")
}
}
}
}
退房[活动#510 - 进展核心图片:过滤器,金属,视觉,和更多 - 2017年WWDC(https://developer.apple .com/videos/play/wwdc2017/510 /),他们开始谈论CIBarcodeDescriptor和errorCorrectedPayload。可悲的是,我无法阅读有效载荷中的消息。 – nathan
是的,我可以通过在“Edit Scheme” - > Run-> Arguments中添加“OS_ACTIVITY_MODE”作为禁用来修复这些错误,但我无法从有效负载中提取数据。我能够扫描条形码,然后在视频中显示图像,但我仍然坚持从数据类型中提取信息字符串。 –
我已经使用你的代码作为我自己的测试的基础,并且我有*运气(iOS 11 beta 3),但结果令人费解。 'errorCorrectedPayload'包含*有时*可读的数据。 https://stackoverflow.com/questions/45037418/zlib-stream-in-ios-11-vision-framework-barcodes-sometimes-decompress-sometimes – oelna