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

How to process a taken image then view the results to "text View" using Swift?

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

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

 tesseract.image = UIImage(named: selectedImage)

给我这个错误:

Cannot convert value of type 'UIImage' to expected argument type 'String'.

注意: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)
    }

}

替换

tesseract.image = UIImage(named: selectedImage)

tesseract.image = selectedImage

UIImage(named:<#string#>) 接受一个字符串值来抑制 bundle 中图像的名称,但在这里你不需要它,而是直接提供图像