1. Products
  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

SwiftyTesseract is an advanced, open-source OCR (Optical Character Recognition) library built for Swift, enabling software developers to integrate powerful text recognition capabilities into iOS and macOS applications with ease. Utilizing the robust Tesseract OCR engine, SwiftyTesseract is uniquely tailored for the Swift ecosystem, providing an intuitive interface and a streamlined process for accurately extracting text from images, documents, and other media. This tool is ideal for software developers building applications that need to scan, recognize, or process textual data, from simple reading apps to complex data processing tools.

SwiftyTesseract makes it straightforward for software developers to integrate OCR functionality into their Swift applications. It wraps the Tesseract library, providing a simple API to work with. It supports multiple languages out of the box. Users can choose from a variety of languages, making it versatile for international applications. It includes image preprocessing capabilities, which enhance the quality of text recognition by adjusting the image before processing. Developers can customize Tesseract’s configuration options, such as setting the OCR engine mode or adjusting the page segmentation mode. Its ease of use, support for multiple languages, and customizable options make it a versatile tool for a wide range of applications as well as an excellent choice for developers looking to implement OCR capabilities in their Swift applications.

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)")
    }
}