尝试在纵向模式下使用 MLVision,但是在我以纵向模式拍照后,由于旋转不正确,它只会输出几个字符。图片必须在横向拍摄,否则无法识别文字。试图查看 Firebase 提供的示例,当这种情况发生在横向和纵向工作时,我遇到了完全相反的问题,但是除了元数据之外,我无法找到决定图像方向的设置,我试图改变但没有任何成功。
var vision: Vision?
override func viewDidLoad() {
super.viewDidLoad()
vision = Vision.vision()
}
func recognize(Image: VisionImage){
let textRecognizer = vision?.onDeviceTextRecognizer()
textRecognizer?.process(Image) { result, error in
guard error == nil, let result = result else {
// ...
return
}
print(result.text)
// Recognized text
}
}
@IBAction func scanDocument(_ sender: Any) {
let vc = UIImagePickerController()
vc.sourceType = .camera
vc.allowsEditing = false
vc.delegate = self
present(vc, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
recognize(Image: VisionImage(image: selectedImage))
}