我试图用一个按钮构建一个简单的应用程序。单击按钮拍照,然后使用"TesseractOCR"
将图像中的书面文本转换为字符串文本并在“文本视图”中查看。
我已经完成了一切,相机和“ TesseractOCR”,我面临的唯一问题是:
tesseract.image = UIImage(named: selectedImage)
给我这个错误:
无法将类型“ UIImage”的值转换为预期的参数类型“字符串”。
注意:selectedImage假设是Tesseract将图像转换为文本所用图像的名称。
这是我的代码:
import UIKit
import TesseractOCR
class secondViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, G8TesseractDelegate {
@IBOutlet weak var viewText: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func takePhoto(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
if let tesseract = G8Tesseract(language: "eng") {
tesseract.delegate = self
tesseract.image = UIImage(named: selectedImage)
tesseract.recognize()
textView.text = tesseract.recognizedText
}
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
}
答案 0 :(得分:1)
替换
tesseract.image = UIImage(named: selectedImage)
使用
tesseract.image = selectedImage
UIImage(named:<#string#>)
采用字符串值来压制捆绑包中的图像名称,但是在这里您不需要它,而是直接提供图像