Skip to content

Commit

Permalink
Use ImageTypeDetector's stream overload
Browse files Browse the repository at this point in the history
DEVSIX-5172
  • Loading branch information
ars18wrw committed Apr 6, 2021
1 parent 7b61601 commit e563ebe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
50 changes: 22 additions & 28 deletions pdfocr-api/src/main/java/com/itextpdf/pdfocr/OcrPdfCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,36 +373,30 @@ private void addDataToPdfDocument(
final boolean createPdfA3u) throws OcrException {
for (Map.Entry<File, Map<Integer, List<TextInfo>>> entry
: imagesTextData.entrySet()) {
try {
File inputImage = entry.getKey();
List<ImageData> imageDataList =
PdfCreatorUtil.getImageData(inputImage,
ocrPdfCreatorProperties.getImageRotationHandler());
LOGGER.info(MessageFormatUtil.format(
PdfOcrLogMessageConstant.NUMBER_OF_PAGES_IN_IMAGE,
inputImage.toString(), imageDataList.size()));

Map<Integer, List<TextInfo>> imageTextData = entry.getValue();
if (imageTextData.keySet().size() > 0) {
for (int page = 0; page < imageDataList.size(); ++page) {
ImageData imageData = imageDataList.get(page);
final Rectangle imageSize =
PdfCreatorUtil.calculateImageSize(
imageData,
ocrPdfCreatorProperties.getScaleMode(),
ocrPdfCreatorProperties.getPageSize());

if (imageTextData.containsKey(page + 1)) {
addToCanvas(pdfDocument, imageSize,
imageTextData.get(page + 1),
imageData, createPdfA3u);
}
File inputImage = entry.getKey();
List<ImageData> imageDataList =
PdfCreatorUtil.getImageData(inputImage,
ocrPdfCreatorProperties.getImageRotationHandler());
LOGGER.info(MessageFormatUtil.format(
PdfOcrLogMessageConstant.NUMBER_OF_PAGES_IN_IMAGE,
inputImage.toString(), imageDataList.size()));

Map<Integer, List<TextInfo>> imageTextData = entry.getValue();
if (imageTextData.keySet().size() > 0) {
for (int page = 0; page < imageDataList.size(); ++page) {
ImageData imageData = imageDataList.get(page);
final Rectangle imageSize =
PdfCreatorUtil.calculateImageSize(
imageData,
ocrPdfCreatorProperties.getScaleMode(),
ocrPdfCreatorProperties.getPageSize());

if (imageTextData.containsKey(page + 1)) {
addToCanvas(pdfDocument, imageSize,
imageTextData.get(page + 1),
imageData, createPdfA3u);
}
}
} catch (IOException e) {
LOGGER.error(MessageFormatUtil.format(
PdfOcrLogMessageConstant.CANNOT_ADD_DATA_TO_PDF_DOCUMENT,
e.getMessage()));
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions pdfocr-api/src/main/java/com/itextpdf/pdfocr/PdfCreatorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ This file is part of the iText (R) project.
import com.itextpdf.layout.renderer.ParagraphRenderer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -172,11 +175,11 @@ static com.itextpdf.kernel.geom.Point calculateImageCoordinates(
* @throws IOException if error occurred during reading a file
*/
static List<ImageData> getImageData(final File inputImage, IImageRotationHandler imageRotationHandler)
throws OcrException, IOException {
throws OcrException {
List<ImageData> images = new ArrayList<ImageData>();

try {
ImageType imageType = ImageTypeDetector.detectImageType(UrlUtil.toURL(inputImage.getAbsolutePath()));
try (InputStream imageStream = new FileInputStream(inputImage)) {
ImageType imageType = ImageTypeDetector.detectImageType(imageStream);
if (ImageType.TIFF == imageType) {
int tiffPages = getNumberOfPageTiff(inputImage);

Expand All @@ -198,7 +201,7 @@ static List<ImageData> getImageData(final File inputImage, IImageRotationHandler
}
images.add(imageData);
}
} catch (com.itextpdf.io.IOException e) {
} catch (IOException | com.itextpdf.io.IOException e) {
LOGGER.error(MessageFormatUtil.format(
PdfOcrLogMessageConstant.CANNOT_READ_INPUT_IMAGE,
e.getMessage()));
Expand Down

0 comments on commit e563ebe

Please sign in to comment.