Skip to content

Commit 7a039f3

Browse files
authored
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.
1 parent c12494c commit 7a039f3

15 files changed

Lines changed: 1229 additions & 0 deletions

File tree

tika-bom/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@
223223
<artifactId>tika-parser-ocr-module</artifactId>
224224
<version>${revision}</version>
225225
</dependency>
226+
<dependency>
227+
<groupId>org.apache.tika</groupId>
228+
<artifactId>tika-parser-ocr-encode-module</artifactId>
229+
<version>${revision}</version>
230+
</dependency>
226231
<dependency>
227232
<groupId>org.apache.tika</groupId>
228233
<artifactId>tika-parser-pdf-module</artifactId>

tika-parsers/tika-parsers-extended/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<modules>
3434
<module>tika-parser-sqlite3-module</module>
3535
<module>tika-parser-scientific-module</module>
36+
<module>tika-parser-ocr-encode-module</module>
3637
<module>tika-parser-sqlite3-package</module>
3738
<module>tika-parser-scientific-package</module>
3839
<module>tika-parsers-extended-integration-tests</module>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<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">
21+
<parent>
22+
<artifactId>tika-parsers-extended</artifactId>
23+
<groupId>org.apache.tika</groupId>
24+
<version>${revision}</version>
25+
<relativePath>../pom.xml</relativePath>
26+
</parent>
27+
<modelVersion>4.0.0</modelVersion>
28+
29+
<artifactId>tika-parser-ocr-encode-module</artifactId>
30+
<name>Apache Tika OCR encode parser module</name>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>${project.groupId}</groupId>
35+
<artifactId>tika-serialization</artifactId>
36+
<version>${project.version}</version>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.apache.logging.log4j</groupId>
41+
<artifactId>log4j-core</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.apache.logging.log4j</groupId>
46+
<artifactId>log4j-slf4j2-impl</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-jar-plugin</artifactId>
56+
<configuration>
57+
<archive>
58+
<manifestEntries>
59+
<Automatic-Module-Name>org.apache.tika.parser.ocrencode</Automatic-Module-Name>
60+
</manifestEntries>
61+
</archive>
62+
</configuration>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
67+
<scm>
68+
<tag>3.0.0-rc1</tag>
69+
</scm>
70+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.tika.parser.ocrencode;
18+
19+
import java.io.Serializable;
20+
21+
/**
22+
* Configuration for {@link EncodeOCRParser}. This parser base64-encodes image
23+
* bytes into the XHTML output instead of running OCR text extraction locally,
24+
* so the size/count limits below govern which images are accepted for
25+
* encoding, not text recognition.
26+
* <p>
27+
* The {@code *Ocr} field and setter names are retained to keep the
28+
* tika-config/JSON parameter names stable; treat them as "OCR-encode".
29+
* <p>
30+
* This class is not thread safe and must be synchronized externally.
31+
*/
32+
public class EncodeOCRConfig implements Serializable {
33+
34+
private static final long serialVersionUID = -1761942486845717891L;
35+
36+
// Default maximum size of a single image accepted for base64 encoding.
37+
// Base64 output is ~4/3 the input size, and the encoded characters are
38+
// buffered in the XHTML content handler, so keep this conservative by
39+
// default. Override via config to raise for larger workloads.
40+
public static final long DEFAULT_MAX_FILE_SIZE_TO_OCR = 100L * 1024 * 1024;
41+
42+
private long maxFileSizeToOcr = DEFAULT_MAX_FILE_SIZE_TO_OCR;
43+
private long minFileSizeToOcr = 0;
44+
private boolean skipOcr = false;
45+
private int maxImagesToOcr = 50;
46+
private boolean inlineContent = false;
47+
48+
public void setInlineContent(boolean inlineContent) {
49+
this.inlineContent = inlineContent;
50+
}
51+
52+
public boolean isInlineContent() {
53+
return inlineContent;
54+
}
55+
56+
/**
57+
* @see #setMinFileSizeToOcr(long minFileSizeToOcr)
58+
*/
59+
public long getMinFileSizeToOcr() {
60+
return minFileSizeToOcr;
61+
}
62+
63+
/**
64+
* Set the minimum image size (in bytes) accepted for base64 encoding.
65+
* Images smaller than this are skipped. Default is 0 (no lower bound).
66+
*/
67+
public void setMinFileSizeToOcr(long minFileSizeToOcr) {
68+
this.minFileSizeToOcr = minFileSizeToOcr;
69+
}
70+
71+
/**
72+
* @see #setMaxFileSizeToOcr(long maxFileSizeToOcr)
73+
*/
74+
public long getMaxFileSizeToOcr() {
75+
return maxFileSizeToOcr;
76+
}
77+
78+
/**
79+
* Set the maximum image size (in bytes) accepted for base64 encoding.
80+
* Images larger than this are skipped. Default is
81+
* {@value #DEFAULT_MAX_FILE_SIZE_TO_OCR} bytes (100 MB).
82+
*/
83+
public void setMaxFileSizeToOcr(long maxFileSizeToOcr) {
84+
this.maxFileSizeToOcr = maxFileSizeToOcr;
85+
}
86+
87+
public boolean isSkipOcr() {
88+
return skipOcr;
89+
}
90+
91+
/**
92+
* If set to <code>true</code>, disables base64 encoding at runtime: the
93+
* parser reports no supported types and parse() is a no-op. Use this to
94+
* turn the parser off for a specific file without rewiring tika-config.
95+
*
96+
* @param skipOcr
97+
*/
98+
public void setSkipOcr(boolean skipOcr) {
99+
this.skipOcr = skipOcr;
100+
}
101+
102+
public int getMaxImagesToOcr() {
103+
return maxImagesToOcr;
104+
}
105+
106+
/**
107+
* Sets the maximum number of images to base64-encode per parse (across
108+
* the whole document, tracked via ParseContext). Further images beyond
109+
* this count are skipped. Default is 50.
110+
*
111+
* @param maxImagesToOcr maximum number of images to encode; must be &gt;= 0
112+
*/
113+
public void setMaxImagesToOcr(int maxImagesToOcr) {
114+
if (maxImagesToOcr < 0) {
115+
throw new IllegalArgumentException(
116+
"maxImagesToOcr must be >= 0"
117+
);
118+
}
119+
this.maxImagesToOcr = maxImagesToOcr;
120+
}
121+
}

0 commit comments

Comments
 (0)