版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2019.05.26 星期日 |
前言
Tesseract OCR
是谷歌Google維護的一個框架,用于從圖片中提取文字,有iOS
和C++
多種版本,在很多領域都有廣泛的應用荔燎。接下來我們就一起走進這個框架(iOS部分)。感興趣的可以看下面幾篇.
1. Tesseract OCR框架(一) —— 基本概覽(一)
2. Tesseract OCR框架(二) —— 基于Tesseract OCR iOS框架的圖片中的文字識別簡單示例(一)
源碼
1. Swift
首先看下工程組織結構
接著看sb中的內容
下面就是源碼了
1. ViewController.swift
import UIKit
import MobileCoreServices
import TesseractOCR
import GPUImage
class ViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
}
// IBAction methods
@IBAction func backgroundTapped(_ sender: Any) {
view.endEditing(true)
}
@IBAction func takePhoto(_ sender: Any) {
let imagePickerActionSheet =
UIAlertController(title: "Snap/Upload Image",
message: nil,
preferredStyle: .actionSheet)
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let cameraButton = UIAlertAction(
title: "Take Photo",
style: .default) { (alert) -> Void in
self.activityIndicator.startAnimating()
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.mediaTypes = [kUTTypeImage as String]
self.present(imagePicker, animated: true, completion: {
self.activityIndicator.stopAnimating()
})
}
imagePickerActionSheet.addAction(cameraButton)
}
let libraryButton = UIAlertAction(
title: "Choose Existing",
style: .default) { (alert) -> Void in
self.activityIndicator.startAnimating()
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String]
self.present(imagePicker, animated: true, completion: {
self.activityIndicator.stopAnimating()
})
}
imagePickerActionSheet.addAction(libraryButton)
let cancelButton = UIAlertAction(title: "Cancel", style: .cancel)
imagePickerActionSheet.addAction(cancelButton)
present(imagePickerActionSheet, animated: true)
}
// Tesseract Image Recognition
func performImageRecognition(_ image: UIImage) {
let scaledImage = image.scaledImage(1000) ?? image
let preprocessedImage = scaledImage.preprocessedImage() ?? scaledImage
if let tesseract = G8Tesseract(language: "eng+fra") {
tesseract.engineMode = .tesseractCubeCombined
tesseract.pageSegmentationMode = .auto
tesseract.image = preprocessedImage
tesseract.recognize()
textView.text = tesseract.recognizedText
}
activityIndicator.stopAnimating()
}
}
// MARK: - UINavigationControllerDelegate
extension ViewController: UINavigationControllerDelegate {
}
// MARK: - UIImagePickerControllerDelegate
extension ViewController: UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedPhoto =
info[.originalImage] as? UIImage else {
dismiss(animated: true)
return
}
activityIndicator.startAnimating()
dismiss(animated: true) {
self.performImageRecognition(selectedPhoto)
}
}
}
// MARK: - UIImage extension
extension UIImage {
func scaledImage(_ maxDimension: CGFloat) -> UIImage? {
var scaledSize = CGSize(width: maxDimension, height: maxDimension)
if size.width > size.height {
scaledSize.height = size.height / size.width * scaledSize.width
} else {
scaledSize.width = size.width / size.height * scaledSize.height
}
UIGraphicsBeginImageContext(scaledSize)
draw(in: CGRect(origin: .zero, size: scaledSize))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage
}
func preprocessedImage() -> UIImage? {
let stillImageFilter = GPUImageAdaptiveThresholdFilter()
stillImageFilter.blurRadiusInPixels = 15.0
let filteredImage = stillImageFilter.image(byFilteringImage: self)
return filteredImage
}
}
后記
本篇主要講述了基于
Tesseract OCR iOS
框架的圖片中的文字識別簡單示例的基本情況销钝,感興趣的給個贊或者關注~~~