如何处理拍摄的图像,然后使用 Swift 查看结果以"text View"?



我正在尝试使用一个按钮构建一个简单的应用程序。单击按钮拍摄图片,然后使用"TesseractOCR"我将图像中的书面文本转换为字符串文本,然后在我的"文本视图"中查看。

我完成了一切,相机和" Tesseractocr",我面临的唯一问题是:

 tesseract.image = UIImage(named: selectedImage)

给我这个错误:

无法将'uiimage'类型的值转换为预期参数类型'string'。

注意:选定图像是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)
    }
}

替换

tesseract.image = UIImage(named: selectedImage)

tesseract.image = selectedImage

UIImage(named:<#string#>)采用一个字符串值来阻止捆绑图中图像的名称,但是在这里您不需要它,而是直接提供图像

最新更新