Skip to content

Commit

Permalink
Minor old fixes, bumped to 2.0.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
RubbaBoy committed Jul 9, 2019
1 parent 24bb951 commit dd0e819
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -23,7 +23,7 @@ apply plugin: 'io.codearte.nexus-staging'

group 'com.uddernetworks.newocr'
archivesBaseName = "NewOCR"
version '2.0.0-SNAPSHOT'
version '2.0.0'

sourceCompatibility = 12

Expand Down
Expand Up @@ -20,12 +20,14 @@ public class DefaultScannedImage implements ScannedImage {

private transient File originalFile;

private BufferedImage binarizedImage;
private transient BufferedImage originalImage;

private final Int2ObjectMap<List<ImageLetter>> grid = new Int2ObjectLinkedOpenHashMap<>();

public DefaultScannedImage(File originalFile, BufferedImage originalImage) {
public DefaultScannedImage(File originalFile, BufferedImage binarizedImage, BufferedImage originalImage) {
this.originalFile = originalFile;
this.binarizedImage = binarizedImage;
this.originalImage = originalImage;
}

Expand Down Expand Up @@ -102,6 +104,11 @@ public Int2ObjectMap.Entry<List<ImageLetter>> getLineEntry(int y) {
return grid.int2ObjectEntrySet().stream().skip(y).findFirst().orElse(null);
}

@Override
public BufferedImage getBinarizedImage() {
return binarizedImage;
}

@Override
public BufferedImage getOriginalImage() {
return originalImage;
Expand Down
Expand Up @@ -17,6 +17,7 @@
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.*;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -103,6 +104,7 @@ public ScannedImage scanImage(File file) {
var inputOptional = this.options.getImageReadMethod().apply(file);
if (inputOptional.isEmpty()) throw new RuntimeException("Input file not found!");
var input = inputOptional.get();
var originalInput = copyBufferedImage(input);
var values = OCRUtils.createGrid(input);
var searchCharacters = new ArrayList<SearchCharacter>();

Expand Down Expand Up @@ -172,7 +174,7 @@ public ScannedImage scanImage(File file) {

// Sorts the lines again based on X values, to move spaces from the back to their proper locations in the line.

ScannedImage scannedImage = new DefaultScannedImage(file, input);
ScannedImage scannedImage = new DefaultScannedImage(file, input, originalInput);

sortedLines.keySet().stream().sorted().forEach(y -> {
List<ImageLetter> line = sortedLines.get(y.intValue());
Expand Down Expand Up @@ -234,4 +236,9 @@ public int spaceRound(double input) {
known += OCRUtils.diff(extra, 1) < 0.2D ? 1 : 0;
return known;
}

private BufferedImage copyBufferedImage(BufferedImage image) {
var colorModel = image.getColorModel();
return new BufferedImage(colorModel, image.copyData(null), colorModel.isAlphaPremultiplied(), null);
}
}
Expand Up @@ -88,6 +88,13 @@ public interface ScannedImage {
*/
Int2ObjectMap.Entry<List<ImageLetter>> getLineEntry(int y);

/**
* Gets the binarized image before any preprocessing
*
* @return The binarized image, which may be null if pulled from caches
*/
BufferedImage getBinarizedImage();

/**
* Gets the original image scanned by the OCR.
*
Expand Down
Expand Up @@ -38,7 +38,7 @@ public Optional<BufferedImage> apply(File file) {
@Override
public Optional<BufferedImage> apply(File file) {
try {
return Optional.ofNullable(OCRUtils.readImage(file));
return Optional.of(OCRUtils.readImage(file));
} catch (IOException e) {
LOGGER.error("Error reading file with ImageReadMethod.IMAGE_ICON", e);
return Optional.empty();
Expand Down

0 comments on commit dd0e819

Please sign in to comment.