Skip to content
Image docs0.3

Spring Boot OCR API workshop

Latest stable Based on Image release 0.3.0

Runnable example

This Spring Boot 4 workshop exposes bluetape4k-images-ocr through a multipart endpoint. It parses language codes, passes host tessdata configuration to an injectable OcrEngine, and separates invalid requests from native OCR availability failures.

Use it when introducing OCR into a Spring MVC application or when you need a testable boundary around Tess4J/Tesseract. It is a focused endpoint example, not a document-processing platform.

The example itself is not published. Choose one bluetape4k-dependencies version and consume bluetape4k-images-ocr; keep the OCR module aligned through that central version rather than pinning it independently.

  • OcrEngine is injected; TesseractOcrEngine is only the production default.
  • eng+kor and eng,kor become listOf("eng", "kor").
  • example.ocr.tessdata-path is host configuration and is not accepted from each request.
  • IllegalArgumentException maps to 400; OcrException maps to 503.

Tests require JDK 21+ only. Real OCR requires Tesseract and matching traineddata.

Terminal window
brew install tesseract tesseract-lang
tesseract --list-langs
./gradlew :spring-boot-ocr-api:bootRun
Terminal window
curl -F "file=@sample-ko.png;type=image/png" \
"http://localhost:8080/api/ocr?languages=eng+kor"

Configure a non-default traineddata directory with example.ocr.tessdata-path.

TaskRelease API
Submit OCRPOST /api/ocr multipart field file
Select languagesquery parameter languages
Configure native dataExampleOcrProperties.tessdataPath
Run recognitionImmutableImage.suspendExtractText(options, engine)
Report resultOcrTextResponse(text, languages, characterCount)

Inject the native engine, keep host paths in configuration, validate content type before decoding, and give native-unavailable failures a different status from client errors. Fake the engine in controller tests; reserve host OCR smoke tests for a compatible environment.

The workshop uses bluetape4k-images-ocr and core image decoding. Pair it with spring-boot-image-api only after deciding whether OCR runs before or after durable storage.

Supported request media types are JPEG, PNG, WebP, and GIF. The language default is eng. Set example.ocr.tessdata-path when Tesseract cannot find traineddata through its host defaults or TESSDATA_PREFIX.

  • 400 bad_request: empty upload, missing content type, unsupported media type, or malformed languages.
  • 503 ocr_unavailable: Tesseract/native bridge or requested traineddata is unavailable.
  • Incorrect text: verify installed languages, input quality, orientation, and preprocessing; successful HTTP execution does not guarantee OCR accuracy.

Add request-size limits, timeouts, concurrency controls, authentication, and rate limiting in the owning service. Avoid logging source documents or full recognized text unless the data policy explicitly permits it.

Terminal window
./gradlew :spring-boot-ocr-api:test

MockMvc and a fake OcrEngine verify multipart success, eng+kor parsing, tessdata propagation, unsupported media rejection, and 503 failure mapping without host Tesseract.

  1. Read the OCR module manual and run this workshop’s fake-engine tests.
  2. Install Tesseract and confirm language packages with tesseract --list-langs.
  3. Exercise a real text-heavy image and observe quality separately from API correctness.
  4. Compare ktor-ocr-api to see the same domain boundary with explicit multipart streaming.

Authentication, queues, persistence, batch OCR, preprocessing policy, and rate limiting are outside this quickstart. OCR quality and language coverage are host-dependent.

These diagrams are loaded directly from README assets published with the 0.3.0 release and pinned to its immutable commit. They describe this manual’s released structure and runtime flows, not later Snapshot changes. Select a preview to open the SVG at the same release commit.

Spring Boot OCR API Architecture

Release README: examples/spring-boot-ocr-api/README.md

Spring Boot OCR API Scenario

Release README: examples/spring-boot-ocr-api/README.md

Spring Boot OCR API Sequence

Release README: examples/spring-boot-ocr-api/README.md