1. Produkty
  2.   OCR
  3.   Swift
  4.   SwiftyTesseract
 
  

Free Swift OCR Library to Perform OCR on Scanned Images & PDFs

Open Source Swift Optical Character Recognition (OCR) Library allows to Scan, Recognize, and Extract Text from Camera Photos Scanned Images & PDFs for free inside iOS and macOS

What is SwiftyTesseract?

SwiftyTesseract is a premier, open-source OCR library designed specifically for the Swift ecosystem, empowering software developers to seamlessly integrate sophisticated optical character recognition into their iOS and macOS applications. By leveraging the powerful, industry-proven Tesseract OCR engine, it provides an intuitive and streamlined Swift interface for accurately extracting text from images, scanned documents, and various digital media. This makes it an ideal foundation for developers building everything from simple text-reading utilities to complex data processing tools that require reliable text scanning and recognition capabilities within the Apple environment.

This library simplifies OCR integration by wrapping Tesseract's functionality into a straightforward and developer-friendly API. It offers extensive versatility right out of the box, including robust support for multiple languages—a critical feature for creating international applications. To ensure high accuracy, SwiftyTesseract incorporates essential image preprocessing capabilities that enhance text clarity before recognition. Furthermore, developers retain fine-grained control by customizing key Tesseract configuration options, such as the OCR engine mode and page segmentation mode. Its combination of ease of use, multi-language support, and deep customizability makes SwiftyTesseract a uniquely versatile and powerful tool for software developers tackling a wide spectrum of text recognition projects.

Previous Next

Getting Started with SwiftyTesseract

The recommend way to install SwiftyTesseract is using CocoaPods. Please use the following command for a smooth installation.

Install SwiftyTesseractvia CocoaPods

 pod 'SwiftyTesseract' 

You can also install it manually; download the latest release files directly from GitHub repository.

Perform OCR on an Image via Swift Library

The open source SwiftyTesseract library makes it easy for software developers to load various types of images and perform OCR operation according to their needs inside Swift applications. You can use an image from your app's assets or any other source. The library can recognize text in various fonts, including Serif, Arial, Sans-serif, and script fonts. Here’s a sample code snippet that demonstrates how software developers can recognize text from an image inside Swift applications.

How to Recognize Text from an Image inside Swift Applications?

import SwiftyTesseract

// Load the image
let image = UIImage(named: "image.jpg")!

// Create a Tesseract instance
let tesseract = Tesseract()

// Set the language to English
tesseract.language = "eng"

// Set the image
tesseract.image = image

// Recognize the text
tesseract.recognize() { result in
    if let text = result.text {
        print("Recognized text: \(text)")
    } else {
        print("Error recognizing text")
    }
}

Image Preprocessing Support

The SwiftyTesseract library has provided complete support for preprocessing images before performing OCR operations inside Swift applications. For instance, you can convert images to grayscale, resize, or adjust contrast, de-skew an image, binarization and so on. Here’s an example that shows how developers can resize an image before performing OCR. In this code example, resizeImage resizes the image before it’s processed, which can be helpful when working with high-resolution images.

How to Preprocessing Images before OCR Operations inside Swift Apps?

func resizeImage(_ image: UIImage, newSize: CGSize) -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    image.draw(in: CGRect(origin: .zero, size: newSize))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

if let resizedImage = resizeImage(image, newSize: CGSize(width: 300, height: 300)) {
    tesseract.performOCR(on: resizedImage) { recognizedText in
        print("Resized Image OCR result: \(recognizedText ?? "No text found")")
    }
}

Multi-language OCR Operations via Swift Library

The open source SwiftyTesseract library supports recognition of text in over 100 languages, including English, Spanish, French, German, Chinese, and many more. You can set the language of your choices when initializing the library. This feature broadens its application scope for international users and multi-language processing requirements. Moreover, developers can create custom dictionaries to improve the accuracy of the OCR process. Here’s an example of configuring the library for English and Spanish. This code enables OCR in both English and Spanish, which is useful when processing documents with mixed-language content.

How to Select Languages for Multi-language OCR Operations inside Swift Apps?

let tesseract = SwiftyTesseract(language: [.english, .spanish])

Customizable OCR Parameters

Using open source SwiftyTesseract library, software developers have the ability to customize OCR settings to improve accuracy for specific types of documents or languages. It allow developers to fine-tune OCR processing, making the library adaptable for unique or complex OCR scenarios. This includes the ability to specify languages and OCR variables based on the document’s needs. Below is an example that shows how developers can customize the OCR engine mode and page segmentation mode.

How to Customize OCR Engine Mode and Page Segmentation Mode via Swift API?

let tesseract = SwiftyTesseract(language: .english, engineMode: .lstmOnly)
tesseract.performOCR(on: image, configuration: [.psm(.auto)]) { recognizedString in
    if let recognizedString = recognizedString {
        print("Recognized text with custom PSM: \(recognizedString)")
    }
}

 Polski