0

我使用 tesseract 进行文本识别。我的问题是从照片库中获取照片,然后使用 tesseract。

我的代码:

import UIKit
import TesseractOCR

class ViewController: UIViewController, G8TesseractDelegate, 
UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet weak var TextView: UITextView!
@IBAction func takePhoto(_ sender: UIButton) {

    let image = UIImagePickerController()
    image.delegate = self

    image.sourceType = UIImagePickerControllerSourceType.photoLibrary

    image.allowsEditing = false


    self.present(image, animated: true){

    }


    if let tesseract = G8Tesseract(language: "dan+eng") {
        tesseract.delegate = self
        tesseract.image = UIImage(named: image)?.g8_blackAndWhite()
        tesseract.recognize()

        TextView.text = tesseract.recognizedText
    }
    func progressImageRecognition(for tesseract: G8Tesseract!) {
        print("Recognition Progress \(tesseract.progress) %")
    }

}

在行中:

tesseract.image = UIImage(named: image)?.g8_blackAndWhite()

它说:

无法转换 UIImagePickerController 类型的值

我该如何解决?

4

1 回答 1

1

您将image对象声明为 type UIImagePickerController

let image = UIImagePickerController()

但是,您将它作为字符串传递给UIImage(named: image)?....

您需要为初始化程序放置一个字符串,UIImage(named: String)例如UIImage(named: "myImage.png").
如果您希望用户能够选择图像然后对其进行处理,则需要从图像中选择图像然后处理该图像UIImagePickerController
网上有很多关于这个主题的教程,比如这个

于 2017-09-26T13:26:27.613 回答