This is an automated email from the ASF dual-hosted git repository.
tballison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git
The following commit(s) were added to refs/heads/main by this push:
new 7a039f3115 Add OCR encode parser module (#2769)
7a039f3115 is described below
commit 7a039f311599a92e422ec50163d4cb1da3017b33
Author: Cristian Zamfir <[email protected]>
AuthorDate: Mon Apr 20 14:22:24 2026 +0200
Add OCR encode parser module (#2769)
Add a new parser module (tika-parser-ocr-encode-module) under
tika-parsers-extended that base64-encodes image content instead of
performing OCR text extraction. This is useful when image data needs
to be preserved in the parsed output for downstream processing by an
external OCR service.
The module handles the same media types as TesseractOCRParser
(ocr-png, ocr-jpeg, ocr-tiff, etc.) and supports configurable
file size limits and per-parse image count limits via
EncodeOCRConfig.
Includes 27 unit tests covering encoding, skip-OCR, file size
filtering, image limits, supported types, config clone-and-update,
and base64 round-trip validation.
---
tika-bom/pom.xml | 5 +
tika-parsers/tika-parsers-extended/pom.xml | 1 +
.../tika-parser-ocr-encode-module/pom.xml | 70 ++++
.../tika/parser/ocrencode/EncodeOCRConfig.java | 121 ++++++
.../tika/parser/ocrencode/EncodeOCRParser.java | 380 ++++++++++++++++++
.../tika/parser/ocrencode/EncodeOCRConfigTest.java | 112 ++++++
.../tika/parser/ocrencode/EncodeOCRParserTest.java | 427 +++++++++++++++++++++
.../configs/tika-config-encodeocr-full.json | 12 +
.../configs/tika-config-encodeocr-partial.json | 10 +
.../configs/tika-config-encodeocr-skip.json | 9 +
.../test-configs/tika-config-encodeocr-full.xml | 29 ++
.../test-configs/tika-config-encodeocr-partial.xml | 27 ++
.../test-configs/tika-config-encodeocr-skip.xml | 26 ++
.../resources/test-documents/testOCR_encode.jpg | Bin 0 -> 284 bytes
.../resources/test-documents/testOCR_encode.png | Bin 0 -> 70 bytes
15 files changed, 1229 insertions(+)
diff --git a/tika-bom/pom.xml b/tika-bom/pom.xml
index 2f0c313317..5914047dcb 100644
--- a/tika-bom/pom.xml
+++ b/tika-bom/pom.xml
@@ -223,6 +223,11 @@
<artifactId>tika-parser-ocr-module</artifactId>
<version>${revision}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.tika</groupId>
+ <artifactId>tika-parser-ocr-encode-module</artifactId>
+ <version>${revision}</version>
+ </dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parser-pdf-module</artifactId>
diff --git a/tika-parsers/tika-parsers-extended/pom.xml
b/tika-parsers/tika-parsers-extended/pom.xml
index 47317aa5db..a034e58430 100644
--- a/tika-parsers/tika-parsers-extended/pom.xml
+++ b/tika-parsers/tika-parsers-extended/pom.xml
@@ -33,6 +33,7 @@
<modules>
<module>tika-parser-sqlite3-module</module>
<module>tika-parser-scientific-module</module>
+ <module>tika-parser-ocr-encode-module</module>
<module>tika-parser-sqlite3-package</module>
<module>tika-parser-scientific-package</module>
<module>tika-parsers-extended-integration-tests</module>
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/pom.xml
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/pom.xml
new file mode 100644
index 0000000000..ad317186e6
--- /dev/null
+++ b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/pom.xml
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>tika-parsers-extended</artifactId>
+ <groupId>org.apache.tika</groupId>
+ <version>${revision}</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>tika-parser-ocr-encode-module</artifactId>
+ <name>Apache Tika OCR encode parser module</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>tika-serialization</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-slf4j2-impl</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <configuration>
+ <archive>
+ <manifestEntries>
+
<Automatic-Module-Name>org.apache.tika.parser.ocrencode</Automatic-Module-Name>
+ </manifestEntries>
+ </archive>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <scm>
+ <tag>3.0.0-rc1</tag>
+ </scm>
+</project>
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/main/java/org/apache/tika/parser/ocrencode/EncodeOCRConfig.java
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/main/java/org/apache/tika/parser/ocrencode/EncodeOCRConfig.java
new file mode 100644
index 0000000000..a0fbb50799
--- /dev/null
+++
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/main/java/org/apache/tika/parser/ocrencode/EncodeOCRConfig.java
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.ocrencode;
+
+import java.io.Serializable;
+
+/**
+ * Configuration for {@link EncodeOCRParser}. This parser base64-encodes image
+ * bytes into the XHTML output instead of running OCR text extraction locally,
+ * so the size/count limits below govern which images are accepted for
+ * encoding, not text recognition.
+ * <p>
+ * The {@code *Ocr} field and setter names are retained to keep the
+ * tika-config/JSON parameter names stable; treat them as "OCR-encode".
+ * <p>
+ * This class is not thread safe and must be synchronized externally.
+ */
+public class EncodeOCRConfig implements Serializable {
+
+ private static final long serialVersionUID = -1761942486845717891L;
+
+ // Default maximum size of a single image accepted for base64 encoding.
+ // Base64 output is ~4/3 the input size, and the encoded characters are
+ // buffered in the XHTML content handler, so keep this conservative by
+ // default. Override via config to raise for larger workloads.
+ public static final long DEFAULT_MAX_FILE_SIZE_TO_OCR = 100L * 1024 * 1024;
+
+ private long maxFileSizeToOcr = DEFAULT_MAX_FILE_SIZE_TO_OCR;
+ private long minFileSizeToOcr = 0;
+ private boolean skipOcr = false;
+ private int maxImagesToOcr = 50;
+ private boolean inlineContent = false;
+
+ public void setInlineContent(boolean inlineContent) {
+ this.inlineContent = inlineContent;
+ }
+
+ public boolean isInlineContent() {
+ return inlineContent;
+ }
+
+ /**
+ * @see #setMinFileSizeToOcr(long minFileSizeToOcr)
+ */
+ public long getMinFileSizeToOcr() {
+ return minFileSizeToOcr;
+ }
+
+ /**
+ * Set the minimum image size (in bytes) accepted for base64 encoding.
+ * Images smaller than this are skipped. Default is 0 (no lower bound).
+ */
+ public void setMinFileSizeToOcr(long minFileSizeToOcr) {
+ this.minFileSizeToOcr = minFileSizeToOcr;
+ }
+
+ /**
+ * @see #setMaxFileSizeToOcr(long maxFileSizeToOcr)
+ */
+ public long getMaxFileSizeToOcr() {
+ return maxFileSizeToOcr;
+ }
+
+ /**
+ * Set the maximum image size (in bytes) accepted for base64 encoding.
+ * Images larger than this are skipped. Default is
+ * {@value #DEFAULT_MAX_FILE_SIZE_TO_OCR} bytes (100 MB).
+ */
+ public void setMaxFileSizeToOcr(long maxFileSizeToOcr) {
+ this.maxFileSizeToOcr = maxFileSizeToOcr;
+ }
+
+ public boolean isSkipOcr() {
+ return skipOcr;
+ }
+
+ /**
+ * If set to <code>true</code>, disables base64 encoding at runtime: the
+ * parser reports no supported types and parse() is a no-op. Use this to
+ * turn the parser off for a specific file without rewiring tika-config.
+ *
+ * @param skipOcr
+ */
+ public void setSkipOcr(boolean skipOcr) {
+ this.skipOcr = skipOcr;
+ }
+
+ public int getMaxImagesToOcr() {
+ return maxImagesToOcr;
+ }
+
+ /**
+ * Sets the maximum number of images to base64-encode per parse (across
+ * the whole document, tracked via ParseContext). Further images beyond
+ * this count are skipped. Default is 50.
+ *
+ * @param maxImagesToOcr maximum number of images to encode; must be >= 0
+ */
+ public void setMaxImagesToOcr(int maxImagesToOcr) {
+ if (maxImagesToOcr < 0) {
+ throw new IllegalArgumentException(
+ "maxImagesToOcr must be >= 0"
+ );
+ }
+ this.maxImagesToOcr = maxImagesToOcr;
+ }
+}
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/main/java/org/apache/tika/parser/ocrencode/EncodeOCRParser.java
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/main/java/org/apache/tika/parser/ocrencode/EncodeOCRParser.java
new file mode 100644
index 0000000000..4174992206
--- /dev/null
+++
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/main/java/org/apache/tika/parser/ocrencode/EncodeOCRParser.java
@@ -0,0 +1,380 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.ocrencode;
+
+import static org.apache.tika.sax.XHTMLContentHandler.XHTML;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Base64;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+import org.apache.tika.config.ConfigDeserializer;
+import org.apache.tika.config.Initializable;
+import org.apache.tika.config.JsonConfig;
+import org.apache.tika.config.TikaComponent;
+import org.apache.tika.exception.TikaConfigException;
+import org.apache.tika.exception.TikaException;
+import org.apache.tika.extractor.ParentContentHandler;
+import org.apache.tika.io.TikaInputStream;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.metadata.TikaCoreProperties;
+import org.apache.tika.mime.MediaType;
+import org.apache.tika.parser.AbstractExternalProcessParser;
+import org.apache.tika.parser.ParseContext;
+import org.apache.tika.sax.BodyContentHandler;
+import org.apache.tika.sax.EmbeddedContentHandler;
+import org.apache.tika.sax.TeeContentHandler;
+import org.apache.tika.sax.XHTMLContentHandler;
+
+/**
+ * Parser that base64-encodes image content instead of performing OCR
+ * text extraction. This is useful when you need to preserve the original
+ * image data in the parsed output for downstream processing by an
+ * external service.
+ * <p>
+ * To configure this parser, pass an {@link EncodeOCRConfig} object
+ * through the ParseContext, or configure it via tika-config.xml/json.
+ */
+@TikaComponent(spi = false)
+public class EncodeOCRParser
+ extends AbstractExternalProcessParser
+ implements Initializable {
+
+ private static final String OCR = "ocr-";
+ private static final Logger LOG = LoggerFactory.getLogger(
+ EncodeOCRParser.class);
+ private static final Object[] LOCK = new Object[0];
+ private static final long serialVersionUID = -8167538283213097266L;
+ private static final Set<MediaType> SUPPORTED_TYPES =
+ Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
+ MediaType.image(OCR + "png"),
+ MediaType.image(OCR + "jpeg"),
+ MediaType.image(OCR + "tiff"),
+ MediaType.image(OCR + "bmp"),
+ MediaType.image(OCR + "gif"),
+ // these are not currently covered by other parsers
+ MediaType.image("jp2"),
+ MediaType.image("jpx"),
+ MediaType.image("x-portable-pixmap"),
+ // add the ocr- versions as well
+ MediaType.image(OCR + "jp2"),
+ MediaType.image(OCR + "jpx"),
+ MediaType.image(OCR + "x-portable-pixmap")
+ )));
+ private static volatile boolean hasWarned = false;
+
+ private EncodeOCRConfig defaultConfig = new EncodeOCRConfig();
+
+ public EncodeOCRParser() {
+ }
+
+ public EncodeOCRParser(EncodeOCRConfig config) {
+ this.defaultConfig = config;
+ }
+
+ /**
+ * Constructor for JSON configuration.
+ * Requires Jackson on the classpath.
+ *
+ * @param jsonConfig JSON configuration
+ */
+ public EncodeOCRParser(JsonConfig jsonConfig) {
+ this(ConfigDeserializer.buildConfig(
+ jsonConfig, EncodeOCRConfig.class));
+ }
+
+ @Override
+ public void initialize() throws TikaConfigException {
+ //no-op
+ }
+
+ public void checkInitialization() throws TikaConfigException {
+ //no-op
+ }
+
+ @Override
+ public Set<MediaType> getSupportedTypes(ParseContext context) {
+ EncodeOCRConfig config = resolveConfig(context);
+ if (config.isSkipOcr()) {
+ return Collections.emptySet();
+ }
+ return SUPPORTED_TYPES;
+ }
+
+ @Override
+ public void parse(
+ TikaInputStream tis,
+ ContentHandler handler,
+ Metadata metadata,
+ ParseContext parseContext
+ ) throws IOException, SAXException, TikaException {
+ normalizeOCRMimeMetadata(metadata);
+
+ ParseContext workingContext =
+ parseContext != null ? parseContext : new ParseContext();
+
+ EncodeOCRConfig config = resolveConfig(workingContext);
+ if (config.isSkipOcr()) {
+ return;
+ }
+
+ ContentHandler baseHandler = getContentHandler(
+ config.isInlineContent(),
+ handler,
+ metadata,
+ workingContext);
+ XHTMLContentHandler xhtml = new XHTMLContentHandler(
+ baseHandler, metadata, workingContext);
+ xhtml.startDocument();
+ doEncode(tis, xhtml, workingContext, config);
+ xhtml.endDocument();
+ }
+
+ private EncodeOCRConfig resolveConfig(ParseContext context) {
+ EncodeOCRConfig userConfig = context.get(EncodeOCRConfig.class);
+ return userConfig != null ? userConfig : defaultConfig;
+ }
+
+ private ContentHandler getContentHandler(
+ boolean isInlineContent,
+ ContentHandler handler,
+ Metadata metadata,
+ ParseContext parseContext) {
+ if (!isInlineContent) {
+ return handler;
+ }
+ ParentContentHandler parentContentHandler = parseContext.get(
+ ParentContentHandler.class);
+ if (parentContentHandler == null) {
+ return handler;
+ }
+ String embeddedType = metadata.get(
+ TikaCoreProperties.EMBEDDED_RESOURCE_TYPE);
+ if (!TikaCoreProperties.EmbeddedResourceType.INLINE.name()
+ .equals(embeddedType)) {
+ return handler;
+ }
+ return new TeeContentHandler(
+ new EmbeddedContentHandler(
+ new BodyContentHandler(
+ parentContentHandler.getContentHandler())),
+ handler);
+ }
+
+ private void normalizeOCRMimeMetadata(Metadata metadata) {
+ String parserOverride = metadata.get(
+ TikaCoreProperties.CONTENT_TYPE_PARSER_OVERRIDE);
+ if (parserOverride != null) {
+ MediaType overrideType = MediaType.parse(parserOverride);
+ if (overrideType != null
+ && overrideType.getSubtype().startsWith(OCR)) {
+ metadata.remove(TikaCoreProperties
+ .CONTENT_TYPE_PARSER_OVERRIDE.getName());
+ }
+ }
+ String contentType = metadata.get(Metadata.CONTENT_TYPE);
+ if (contentType != null) {
+ MediaType parsedType = MediaType.parse(contentType);
+ if (parsedType != null
+ && parsedType.getSubtype().startsWith(OCR)) {
+ metadata.set(Metadata.CONTENT_TYPE,
+ new MediaType(parsedType.getType(),
+ parsedType.getSubtype().substring(
+ OCR.length())).toString());
+ }
+ }
+ }
+
+ private void doEncode(
+ TikaInputStream tikaInputStream,
+ ContentHandler xhtml,
+ ParseContext parseContext,
+ EncodeOCRConfig config
+ ) throws IOException, SAXException, TikaException {
+ warnOnFirstParse();
+
+ long size = tikaInputStream.getLength();
+ if (size >= config.getMinFileSizeToOcr()
+ && size <= config.getMaxFileSizeToOcr()) {
+ if (!reserveImageSlot(parseContext, config)) {
+ OCRImageCounter counter = parseContext.get(
+ OCRImageCounter.class);
+ int processed = counter != null
+ ? counter.get()
+ : config.getMaxImagesToOcr();
+ LOG.info("Skipping OCR encode for image because "
+ + "the configured limit of {} images "
+ + "has been reached ({} already processed)",
+ config.getMaxImagesToOcr(), processed);
+ return;
+ }
+ encodeToBase64(tikaInputStream, size, xhtml);
+ } else {
+ LOG.debug("File size {} is outside the allowed "
+ + "range for OCR encode: {} - {}",
+ size,
+ config.getMinFileSizeToOcr(),
+ config.getMaxFileSizeToOcr());
+ }
+ }
+
+ private boolean reserveImageSlot(
+ ParseContext parseContext,
+ EncodeOCRConfig config) {
+ OCRImageCounter counter = parseContext.get(OCRImageCounter.class);
+ if (counter == null) {
+ counter = new OCRImageCounter();
+ parseContext.set(OCRImageCounter.class, counter);
+ }
+ return counter.tryIncrement(config.getMaxImagesToOcr());
+ }
+
+ private void encodeToBase64(
+ InputStream input,
+ long fileSize,
+ ContentHandler xhtml
+ ) throws IOException, SAXException {
+ long startTime = System.nanoTime();
+
+ AttributesImpl attrs = new AttributesImpl();
+ attrs.addAttribute("", "class", "class", "CDATA", "ocr");
+ xhtml.startElement(XHTML, "div", "div", attrs);
+
+ String beginMarker = "\n<<<---IMAGE-BASE64-ENCODED-BEGIN--->>>\n";
+ xhtml.characters(
+ beginMarker.toCharArray(), 0, beginMarker.length());
+
+ CharForwardingOutputStream sink =
+ new CharForwardingOutputStream(xhtml);
+ try (OutputStream base64Out = Base64.getEncoder().wrap(sink)) {
+ input.transferTo(base64Out);
+ } catch (IOException e) {
+ if (e.getCause() instanceof SAXException) {
+ throw (SAXException) e.getCause();
+ }
+ throw e;
+ }
+
+ String endMarker = "\n<<<---IMAGE-BASE64-ENCODED-END--->>>\n";
+ xhtml.characters(
+ endMarker.toCharArray(), 0, endMarker.length());
+ xhtml.endElement(XHTML, "div", "div");
+
+ long durationMs = (System.nanoTime() - startTime) / 1_000_000;
+ LOG.info("OCR encoding - input file size: {} bytes, "
+ + "output size: {} characters, "
+ + "time taken: {} ms",
+ fileSize, sink.totalChars(), durationMs);
+ }
+
+ private void warnOnFirstParse() {
+ if (!hasWarned) {
+ synchronized (LOCK) {
+ if (!hasWarned) {
+ LOG.info("OCR encode is being invoked. "
+ + "This can add greatly to processing time.");
+ hasWarned = true;
+ }
+ }
+ }
+ }
+
+ public EncodeOCRConfig getDefaultConfig() {
+ return defaultConfig;
+ }
+
+ private static final class OCRImageCounter {
+ private final AtomicInteger count = new AtomicInteger(0);
+
+ boolean tryIncrement(int maxAllowed) {
+ while (true) {
+ int current = count.get();
+ if (current >= maxAllowed) {
+ return false;
+ }
+ if (count.compareAndSet(current, current + 1)) {
+ return true;
+ }
+ }
+ }
+
+ int get() {
+ return count.get();
+ }
+ }
+
+ /**
+ * Converts each written byte (which is always an ASCII character
+ * produced by {@link Base64.Encoder}) into a char and forwards it
+ * to the wrapped {@link ContentHandler} via
+ * {@link ContentHandler#characters(char[], int, int)}.
+ */
+ private static final class CharForwardingOutputStream
+ extends OutputStream {
+ private final ContentHandler xhtml;
+ private final AtomicLong totalChars = new AtomicLong();
+
+ CharForwardingOutputStream(ContentHandler xhtml) {
+ this.xhtml = xhtml;
+ }
+
+ @Override
+ public void write(int b) throws IOException {
+ try {
+ char[] c = {(char) (b & 0xFF)};
+ xhtml.characters(c, 0, 1);
+ totalChars.incrementAndGet();
+ } catch (SAXException e) {
+ throw new IOException(e);
+ }
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len) throws IOException {
+ if (len <= 0) {
+ return;
+ }
+ char[] buf = new char[len];
+ for (int i = 0; i < len; i++) {
+ buf[i] = (char) (b[off + i] & 0xFF);
+ }
+ try {
+ xhtml.characters(buf, 0, len);
+ totalChars.addAndGet(len);
+ } catch (SAXException e) {
+ throw new IOException(e);
+ }
+ }
+
+ long totalChars() {
+ return totalChars.get();
+ }
+ }
+}
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/java/org/apache/tika/parser/ocrencode/EncodeOCRConfigTest.java
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/java/org/apache/tika/parser/ocrencode/EncodeOCRConfigTest.java
new file mode 100644
index 0000000000..d1df15ed7c
--- /dev/null
+++
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/java/org/apache/tika/parser/ocrencode/EncodeOCRConfigTest.java
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.ocrencode;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+import org.apache.tika.TikaTest;
+import org.apache.tika.config.loader.TikaLoader;
+import org.apache.tika.parser.CompositeParser;
+
+public class EncodeOCRConfigTest extends TikaTest {
+
+ @Test
+ public void testDefaultConfig() {
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ assertEquals(0, config.getMinFileSizeToOcr(),
+ "Invalid default minFileSizeToOcr value");
+ assertEquals(EncodeOCRConfig.DEFAULT_MAX_FILE_SIZE_TO_OCR,
+ config.getMaxFileSizeToOcr(),
+ "Invalid default maxFileSizeToOcr value");
+ assertFalse(config.isSkipOcr(),
+ "Invalid default skipOcr value");
+ assertEquals(50, config.getMaxImagesToOcr(),
+ "Invalid default maxImagesToOcr value");
+ assertFalse(config.isInlineContent(),
+ "Invalid default inlineContent value");
+ }
+
+ @Test
+ public void testMaxImagesToOcrValidation() {
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ config.setMaxImagesToOcr(0);
+ assertEquals(0, config.getMaxImagesToOcr());
+ config.setMaxImagesToOcr(100);
+ assertEquals(100, config.getMaxImagesToOcr());
+
+ assertThrows(IllegalArgumentException.class, () -> {
+ config.setMaxImagesToOcr(-1);
+ });
+ }
+
+ @Test
+ public void testFullConfig() throws Exception {
+ TikaLoader loader = TikaLoader.load(
+ getConfigPath(EncodeOCRConfigTest.class,
+ "tika-config-encodeocr-full.json"));
+ EncodeOCRParser parser =
+ (EncodeOCRParser) ((CompositeParser) loader.loadParsers())
+ .getAllComponentParsers().get(0);
+ EncodeOCRConfig config = parser.getDefaultConfig();
+ assertEquals(1, config.getMinFileSizeToOcr(),
+ "Invalid overridden minFileSizeToOcr value");
+ assertEquals(2000000, config.getMaxFileSizeToOcr(),
+ "Invalid overridden maxFileSizeToOcr value");
+ assertEquals(25, config.getMaxImagesToOcr(),
+ "Invalid overridden maxImagesToOcr value");
+ assertFalse(config.isSkipOcr(),
+ "Invalid overridden skipOcr value");
+ }
+
+ @Test
+ public void testPartialConfig() throws Exception {
+ TikaLoader loader = TikaLoader.load(
+ getConfigPath(EncodeOCRConfigTest.class,
+ "tika-config-encodeocr-partial.json"));
+ EncodeOCRParser parser =
+ (EncodeOCRParser) ((CompositeParser) loader.loadParsers())
+ .getAllComponentParsers().get(0);
+ EncodeOCRConfig config = parser.getDefaultConfig();
+ assertEquals(1, config.getMinFileSizeToOcr(),
+ "Invalid overridden minFileSizeToOcr value");
+ assertEquals(EncodeOCRConfig.DEFAULT_MAX_FILE_SIZE_TO_OCR,
+ config.getMaxFileSizeToOcr(),
+ "Invalid default maxFileSizeToOcr value");
+ assertEquals(10, config.getMaxImagesToOcr(),
+ "Invalid overridden maxImagesToOcr value");
+ assertFalse(config.isSkipOcr(),
+ "Invalid default skipOcr value");
+ }
+
+ @Test
+ public void testSkipOcrConfig() throws Exception {
+ TikaLoader loader = TikaLoader.load(
+ getConfigPath(EncodeOCRConfigTest.class,
+ "tika-config-encodeocr-skip.json"));
+ EncodeOCRParser parser =
+ (EncodeOCRParser) ((CompositeParser) loader.loadParsers())
+ .getAllComponentParsers().get(0);
+ EncodeOCRConfig config = parser.getDefaultConfig();
+ assertTrue(config.isSkipOcr(),
+ "skipOcr should be true when set in config");
+ }
+}
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/java/org/apache/tika/parser/ocrencode/EncodeOCRParserTest.java
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/java/org/apache/tika/parser/ocrencode/EncodeOCRParserTest.java
new file mode 100644
index 0000000000..f08eb2d5d0
--- /dev/null
+++
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/java/org/apache/tika/parser/ocrencode/EncodeOCRParserTest.java
@@ -0,0 +1,427 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.ocrencode;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Base64;
+
+import org.junit.jupiter.api.Test;
+
+import org.apache.tika.TikaTest;
+import org.apache.tika.io.TikaInputStream;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.metadata.TikaCoreProperties;
+import org.apache.tika.mime.MediaType;
+import org.apache.tika.parser.ParseContext;
+
+public class EncodeOCRParserTest extends TikaTest {
+
+ // Markers as they appear in XML-serialized output
+ // (< and > are escaped by ToXMLContentHandler)
+ private static final String BEGIN_MARKER_XML =
+ "<<<---IMAGE-BASE64-ENCODED-BEGIN---"
+ + ">>>";
+ private static final String END_MARKER_XML =
+ "<<<---IMAGE-BASE64-ENCODED-END---"
+ + ">>>";
+
+ private Metadata getMetadata(MediaType mediaType) {
+ Metadata metadata = new Metadata();
+ MediaType ocrMediaType =
+ new MediaType(mediaType.getType(),
+ "ocr-" + mediaType.getSubtype());
+ metadata.set(
+ TikaCoreProperties.CONTENT_TYPE_PARSER_OVERRIDE,
+ ocrMediaType.toString());
+ return metadata;
+ }
+
+ @Test
+ public void testBasicEncoding() throws Exception {
+ byte[] simplePng = createSimplePng();
+
+ EncodeOCRParser parser = new EncodeOCRParser();
+ TikaInputStream tis = TikaInputStream.get(simplePng);
+ String xml = getXML(tis, parser,
+ getMetadata(MediaType.image("png"))).xml;
+
+ // Should contain the base64 markers
+ assertContains(BEGIN_MARKER_XML, xml);
+ assertContains(END_MARKER_XML, xml);
+
+ // Should contain base64 encoded content
+ assertTrue(xml.contains("iVBOR"),
+ "Should contain base64 PNG signature");
+
+ // Should have ocr class div
+ assertContains("<div class=\"ocr\">", xml);
+ }
+
+ @Test
+ public void testBasicEncodingWithResourceFile() throws Exception {
+ EncodeOCRParser parser = new EncodeOCRParser();
+ String xml = getXML("testOCR_encode.png", parser,
+ getMetadata(MediaType.image("png"))).xml;
+
+ assertContains(BEGIN_MARKER_XML, xml);
+ assertContains(END_MARKER_XML, xml);
+ assertTrue(xml.contains("iVBOR"),
+ "Should contain base64 PNG signature");
+ assertContains("<div class=\"ocr\">", xml);
+ }
+
+ @Test
+ public void testJpegEncoding() throws Exception {
+ EncodeOCRParser parser = new EncodeOCRParser();
+ String xml = getXML("testOCR_encode.jpg", parser,
+ getMetadata(MediaType.image("jpeg"))).xml;
+
+ assertContains(BEGIN_MARKER_XML, xml);
+ assertContains(END_MARKER_XML, xml);
+ // JPEG base64 starts with /9j/
+ assertTrue(xml.contains("/9j/"),
+ "Should contain base64 JPEG signature");
+ }
+
+ @Test
+ public void testBase64OutputIsDecodable() throws Exception {
+ EncodeOCRParser parser = new EncodeOCRParser();
+ String text = getText("testOCR_encode.png", parser,
+ getMetadata(MediaType.image("png")));
+
+ String beginLiteral =
+ "<<<---IMAGE-BASE64-ENCODED-BEGIN--->>>";
+ String endLiteral =
+ "<<<---IMAGE-BASE64-ENCODED-END--->>>";
+
+ int start = text.indexOf(beginLiteral);
+ int end = text.indexOf(endLiteral);
+ assertTrue(start >= 0,
+ "Begin marker not found in text output");
+ assertTrue(end > start,
+ "End marker not found after begin marker");
+
+ String b64 = text.substring(
+ start + beginLiteral.length(), end).trim();
+ // Use MIME decoder because the output may contain
+ // line breaks
+ byte[] decoded = Base64.getMimeDecoder().decode(b64);
+ // PNG magic bytes
+ assertEquals((byte) 0x89, decoded[0]);
+ assertEquals((byte) 0x50, decoded[1]); // 'P'
+ assertEquals((byte) 0x4E, decoded[2]); // 'N'
+ assertEquals((byte) 0x47, decoded[3]); // 'G'
+ }
+
+ @Test
+ public void testSkipOCR() throws Exception {
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ config.setSkipOcr(true);
+ ParseContext context = new ParseContext();
+ context.set(EncodeOCRConfig.class, config);
+
+ EncodeOCRParser parser = new EncodeOCRParser();
+ TikaInputStream tis = TikaInputStream.get(createSimplePng());
+ String xml = getXML(tis, parser,
+ getMetadata(MediaType.image("png")),
+ context).xml;
+
+ assertNotContained(BEGIN_MARKER_XML, xml);
+ assertNotContained(END_MARKER_XML, xml);
+ }
+
+ @Test
+ public void testRuntimeSkipOCRViaConstructor() throws Exception {
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ config.setSkipOcr(true);
+ EncodeOCRParser parser = new EncodeOCRParser(config);
+
+ TikaInputStream tis = TikaInputStream.get(createSimplePng());
+ String xml = getXML(tis, parser,
+ getMetadata(MediaType.image("png"))).xml;
+
+ assertNotContained(BEGIN_MARKER_XML, xml);
+ }
+
+ @Test
+ public void testMinFileSizeFilter() throws Exception {
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ config.setMinFileSizeToOcr(10000); // 10KB minimum
+ ParseContext context = new ParseContext();
+ context.set(EncodeOCRConfig.class, config);
+
+ EncodeOCRParser parser = new EncodeOCRParser();
+ TikaInputStream tis = TikaInputStream.get(createSimplePng());
+ String xml = getXML(tis, parser,
+ getMetadata(MediaType.image("png")),
+ context).xml;
+
+ assertNotContained(BEGIN_MARKER_XML, xml);
+ }
+
+ @Test
+ public void testMaxFileSizeFilter() throws Exception {
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ config.setMaxFileSizeToOcr(10); // 10 bytes maximum
+ ParseContext context = new ParseContext();
+ context.set(EncodeOCRConfig.class, config);
+
+ EncodeOCRParser parser = new EncodeOCRParser();
+ TikaInputStream tis = TikaInputStream.get(createSimplePng());
+ String xml = getXML(tis, parser,
+ getMetadata(MediaType.image("png")),
+ context).xml;
+
+ assertNotContained(BEGIN_MARKER_XML, xml);
+ }
+
+ @Test
+ public void testFileSizeWithinRange() throws Exception {
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ config.setMinFileSizeToOcr(1);
+ config.setMaxFileSizeToOcr(100000);
+ ParseContext context = new ParseContext();
+ context.set(EncodeOCRConfig.class, config);
+
+ EncodeOCRParser parser = new EncodeOCRParser();
+ TikaInputStream tis = TikaInputStream.get(createSimplePng());
+ String xml = getXML(tis, parser,
+ getMetadata(MediaType.image("png")),
+ context).xml;
+
+ assertContains(BEGIN_MARKER_XML, xml);
+ }
+
+ @Test
+ public void testMaxImagesToOcr() throws Exception {
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ config.setMaxImagesToOcr(1);
+ ParseContext context = new ParseContext();
+ context.set(EncodeOCRConfig.class, config);
+
+ EncodeOCRParser parser = new EncodeOCRParser();
+
+ // First image should be processed
+ TikaInputStream tis1 = TikaInputStream.get(createSimplePng());
+ String xml = getXML(tis1, parser,
+ getMetadata(MediaType.image("png")),
+ context).xml;
+ assertContains(BEGIN_MARKER_XML, xml);
+
+ // Second image should be skipped (reusing same context)
+ TikaInputStream tis2 = TikaInputStream.get(createSimplePng());
+ xml = getXML(tis2, parser,
+ getMetadata(MediaType.image("png")),
+ context).xml;
+ assertNotContained(BEGIN_MARKER_XML, xml);
+ }
+
+ @Test
+ public void testMaxImagesToOcrZero() throws Exception {
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ config.setMaxImagesToOcr(0);
+ ParseContext context = new ParseContext();
+ context.set(EncodeOCRConfig.class, config);
+
+ EncodeOCRParser parser = new EncodeOCRParser();
+ TikaInputStream tis = TikaInputStream.get(createSimplePng());
+ String xml = getXML(tis, parser,
+ getMetadata(MediaType.image("png")),
+ context).xml;
+ assertNotContained(BEGIN_MARKER_XML, xml);
+ }
+
+ @Test
+ public void testMaxImagesToOcrMultiple() throws Exception {
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ config.setMaxImagesToOcr(3);
+ ParseContext context = new ParseContext();
+ context.set(EncodeOCRConfig.class, config);
+
+ EncodeOCRParser parser = new EncodeOCRParser();
+
+ // Images 1-3 should be processed
+ for (int i = 0; i < 3; i++) {
+ TikaInputStream tis =
+ TikaInputStream.get(createSimplePng());
+ String xml = getXML(tis, parser,
+ getMetadata(MediaType.image("png")),
+ context).xml;
+ assertContains(BEGIN_MARKER_XML, xml);
+ }
+
+ // Image 4 should be skipped
+ TikaInputStream tis = TikaInputStream.get(createSimplePng());
+ String xml = getXML(tis, parser,
+ getMetadata(MediaType.image("png")),
+ context).xml;
+ assertNotContained(BEGIN_MARKER_XML, xml);
+ }
+
+ @Test
+ public void testSupportedTypes() {
+ EncodeOCRParser parser = new EncodeOCRParser();
+ ParseContext context = new ParseContext();
+
+ assertTrue(parser.getSupportedTypes(context)
+ .contains(MediaType.image("ocr-png")));
+ assertTrue(parser.getSupportedTypes(context)
+ .contains(MediaType.image("ocr-jpeg")));
+ assertTrue(parser.getSupportedTypes(context)
+ .contains(MediaType.image("ocr-tiff")));
+ assertTrue(parser.getSupportedTypes(context)
+ .contains(MediaType.image("ocr-bmp")));
+ assertTrue(parser.getSupportedTypes(context)
+ .contains(MediaType.image("ocr-gif")));
+
+ assertTrue(parser.getSupportedTypes(context)
+ .contains(MediaType.image("jp2")));
+ assertTrue(parser.getSupportedTypes(context)
+ .contains(MediaType.image("jpx")));
+ assertTrue(parser.getSupportedTypes(context)
+ .contains(MediaType.image("x-portable-pixmap")));
+
+ assertTrue(parser.getSupportedTypes(context)
+ .contains(MediaType.image("ocr-jp2")));
+ assertTrue(parser.getSupportedTypes(context)
+ .contains(MediaType.image("ocr-jpx")));
+ assertTrue(parser.getSupportedTypes(context)
+ .contains(MediaType.image(
+ "ocr-x-portable-pixmap")));
+ }
+
+ @Test
+ public void testSupportedTypesWithSkipOcr() {
+ EncodeOCRParser parser = new EncodeOCRParser();
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ config.setSkipOcr(true);
+ ParseContext context = new ParseContext();
+ context.set(EncodeOCRConfig.class, config);
+
+ assertEquals(0, parser.getSupportedTypes(context).size());
+ }
+
+ @Test
+ public void testSupportedTypesWithSkipOcrViaDefaultConfig() {
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ config.setSkipOcr(true);
+ EncodeOCRParser parser = new EncodeOCRParser(config);
+
+ // Empty context — skipOcr comes from defaultConfig
+ ParseContext context = new ParseContext();
+ assertEquals(0, parser.getSupportedTypes(context).size(),
+ "getSupportedTypes should respect defaultConfig.skipOcr");
+ }
+
+ @Test
+ public void testSupportedTypesCount() {
+ EncodeOCRParser parser = new EncodeOCRParser();
+ ParseContext context = new ParseContext();
+ // 5 ocr- types + 3 non-ocr + 3 ocr- versions of non-ocr = 11
+ assertEquals(11, parser.getSupportedTypes(context).size());
+ }
+
+ @Test
+ public void testDefaultConfig() {
+ EncodeOCRParser parser = new EncodeOCRParser();
+ EncodeOCRConfig config = parser.getDefaultConfig();
+ assertNotNull(config);
+ assertFalse(config.isSkipOcr());
+ assertEquals(0, config.getMinFileSizeToOcr());
+ assertEquals(EncodeOCRConfig.DEFAULT_MAX_FILE_SIZE_TO_OCR,
+ config.getMaxFileSizeToOcr());
+ assertEquals(50, config.getMaxImagesToOcr());
+ }
+
+ @Test
+ public void testConstructorWithConfig() {
+ EncodeOCRConfig config = new EncodeOCRConfig();
+ config.setMaxFileSizeToOcr(5000);
+ config.setMinFileSizeToOcr(100);
+ config.setMaxImagesToOcr(10);
+ config.setSkipOcr(true);
+
+ EncodeOCRParser parser = new EncodeOCRParser(config);
+ EncodeOCRConfig defaultConfig = parser.getDefaultConfig();
+ assertEquals(5000, defaultConfig.getMaxFileSizeToOcr());
+ assertEquals(100, defaultConfig.getMinFileSizeToOcr());
+ assertEquals(10, defaultConfig.getMaxImagesToOcr());
+ assertTrue(defaultConfig.isSkipOcr());
+ }
+
+ @Test
+ public void testInitialize() throws Exception {
+ EncodeOCRParser parser = new EncodeOCRParser();
+ // initialize should be a no-op and not throw
+ parser.initialize();
+ }
+
+ @Test
+ public void testCheckInitialization() throws Exception {
+ EncodeOCRParser parser = new EncodeOCRParser();
+ // checkInitialization should be a no-op and not throw
+ parser.checkInitialization();
+ }
+
+ @Test
+ public void testParseWithNoConfig() throws Exception {
+ EncodeOCRParser parser = new EncodeOCRParser();
+ ParseContext context = new ParseContext();
+
+ TikaInputStream tis = TikaInputStream.get(createSimplePng());
+ String xml = getXML(tis, parser,
+ getMetadata(MediaType.image("png")),
+ context).xml;
+ assertContains(BEGIN_MARKER_XML, xml);
+ }
+
+ @Test
+ public void testUserConfigOverridesDefault() throws Exception {
+ EncodeOCRConfig parserConfig = new EncodeOCRConfig();
+ parserConfig.setMaxImagesToOcr(5);
+ EncodeOCRParser parser = new EncodeOCRParser(parserConfig);
+
+ EncodeOCRConfig userConfig = new EncodeOCRConfig();
+ userConfig.setMinFileSizeToOcr(1);
+
+ ParseContext context = new ParseContext();
+ context.set(EncodeOCRConfig.class, userConfig);
+
+ // parser's defaultConfig is unaffected by ParseContext config
+ assertEquals(5, parser.getDefaultConfig().getMaxImagesToOcr());
+
+ TikaInputStream tis = TikaInputStream.get(createSimplePng());
+ String xml = getXML(tis, parser,
+ getMetadata(MediaType.image("png")),
+ context).xml;
+ assertContains(BEGIN_MARKER_XML, xml);
+ }
+
+ /**
+ * Creates a minimal valid 1x1 PNG image for testing.
+ */
+ private byte[] createSimplePng() {
+ String base64Png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAY"
+ + "AAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAA"
+ + "BJRU5ErkJggg==";
+ return Base64.getDecoder().decode(base64Png);
+ }
+}
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/configs/tika-config-encodeocr-full.json
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/configs/tika-config-encodeocr-full.json
new file mode 100644
index 0000000000..5957f15f49
--- /dev/null
+++
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/configs/tika-config-encodeocr-full.json
@@ -0,0 +1,12 @@
+{
+ "parsers": [
+ {
+ "encode-ocr-parser": {
+ "minFileSizeToOcr": 1,
+ "maxFileSizeToOcr": 2000000,
+ "maxImagesToOcr": 25,
+ "skipOcr": false
+ }
+ }
+ ]
+}
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/configs/tika-config-encodeocr-partial.json
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/configs/tika-config-encodeocr-partial.json
new file mode 100644
index 0000000000..ff7a1fe9f0
--- /dev/null
+++
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/configs/tika-config-encodeocr-partial.json
@@ -0,0 +1,10 @@
+{
+ "parsers": [
+ {
+ "encode-ocr-parser": {
+ "minFileSizeToOcr": 1,
+ "maxImagesToOcr": 10
+ }
+ }
+ ]
+}
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/configs/tika-config-encodeocr-skip.json
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/configs/tika-config-encodeocr-skip.json
new file mode 100644
index 0000000000..16bef1387f
--- /dev/null
+++
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/configs/tika-config-encodeocr-skip.json
@@ -0,0 +1,9 @@
+{
+ "parsers": [
+ {
+ "encode-ocr-parser": {
+ "skipOcr": true
+ }
+ }
+ ]
+}
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-configs/tika-config-encodeocr-full.xml
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-configs/tika-config-encodeocr-full.xml
new file mode 100644
index 0000000000..c2174caf4c
--- /dev/null
+++
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-configs/tika-config-encodeocr-full.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<properties>
+ <parsers>
+ <parser class="org.apache.tika.parser.ocrencode.EncodeOCRParser">
+ <params>
+ <param name="minFileSizeToOcr" type="long">1</param>
+ <param name="maxFileSizeToOcr" type="long">2000000</param>
+ <param name="maxImagesToOcr" type="int">25</param>
+ <param name="skipOcr" type="bool">false</param>
+ </params>
+ </parser>
+ </parsers>
+</properties>
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-configs/tika-config-encodeocr-partial.xml
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-configs/tika-config-encodeocr-partial.xml
new file mode 100644
index 0000000000..91597b9373
--- /dev/null
+++
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-configs/tika-config-encodeocr-partial.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<properties>
+ <parsers>
+ <parser class="org.apache.tika.parser.ocrencode.EncodeOCRParser">
+ <params>
+ <param name="minFileSizeToOcr" type="long">1</param>
+ <param name="maxImagesToOcr" type="int">10</param>
+ </params>
+ </parser>
+ </parsers>
+</properties>
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-configs/tika-config-encodeocr-skip.xml
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-configs/tika-config-encodeocr-skip.xml
new file mode 100644
index 0000000000..39e12494bb
--- /dev/null
+++
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-configs/tika-config-encodeocr-skip.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<properties>
+ <parsers>
+ <parser class="org.apache.tika.parser.ocrencode.EncodeOCRParser">
+ <params>
+ <param name="skipOcr" type="bool">true</param>
+ </params>
+ </parser>
+ </parsers>
+</properties>
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-documents/testOCR_encode.jpg
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-documents/testOCR_encode.jpg
new file mode 100644
index 0000000000..eb843bf3b1
Binary files /dev/null and
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-documents/testOCR_encode.jpg
differ
diff --git
a/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-documents/testOCR_encode.png
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-documents/testOCR_encode.png
new file mode 100644
index 0000000000..08cd6f2bfd
Binary files /dev/null and
b/tika-parsers/tika-parsers-extended/tika-parser-ocr-encode-module/src/test/resources/test-documents/testOCR_encode.png
differ