Skip to content

Commit

Permalink
feat(process-file): Find 2D-Docs on images
Browse files Browse the repository at this point in the history
  • Loading branch information
juliette-derancourt committed Nov 24, 2023
1 parent 3ad98c2 commit 57f42b3
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.dossierfacile.process.file.barcode.qrcode.QrCode;
import fr.dossierfacile.process.file.barcode.qrcode.QrCodeReader;
import fr.dossierfacile.process.file.barcode.twoddoc.TwoDDocRawContent;
import fr.dossierfacile.process.file.barcode.twoddoc.reader.TwoDDocImageFinder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -26,7 +27,7 @@ public QrCode findQrCode() {

@Override
protected TwoDDocRawContent find2DDoc() {
return null; // TODO
return new TwoDDocImageFinder(image).find2DDoc().orElse(null);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package fr.dossierfacile.process.file.barcode.twoddoc.reader;

import java.awt.*;
import java.awt.image.BufferedImage;

record CroppedFile(BufferedImage bufferedImage) {

CroppedFile rotate(int degrees) {
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();

BufferedImage result = new BufferedImage(width, height, bufferedImage.getType());
Graphics2D graphics2D = result.createGraphics();

graphics2D.rotate(Math.toRadians(degrees), (double) width / 2, (double) height / 2);
graphics2D.drawImage(bufferedImage, null, 0, 0);

return new CroppedFile(result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ static FileCropper fromPdfSource(PdfPage pdfPage) throws IOException {
return new FileCropper(image, scale);
}

static FileCropper fromImageSource(BufferedImage image) {
return new FileCropper(image, 1);
}

CroppedFile cropAt(SquarePosition position) {
SquarePosition scaledPosition = position.scale(scale);
Coordinates coordinates = scaledPosition.coordinates();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AllArgsConstructor;

import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.stream.Stream;

Expand All @@ -25,4 +26,8 @@ Coordinates toCoordinates(PdfPage pdfPage) {
return new Coordinates(xFactor * pdfPage.getWidth(), yFactor * pdfPage.getHeight());
}

Coordinates toCoordinates(BufferedImage image) {
return new Coordinates(xFactor * image.getWidth(), yFactor * image.getHeight());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Optional;

@Slf4j
class TwoDDocImageReader {
class TwoDDocDecoder {

private static final DataMatrixReader READER = new DataMatrixReader();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;
import java.util.Optional;

import static fr.dossierfacile.process.file.barcode.twoddoc.reader.TwoDDocImageReader.readTwoDDocOn;
import static fr.dossierfacile.process.file.barcode.twoddoc.reader.TwoDDocDecoder.readTwoDDocOn;

@Slf4j
@AllArgsConstructor
Expand All @@ -20,9 +20,15 @@ public abstract class TwoDDocFinder {
Optional<TwoDDocRawContent> tryToFindTwoDDocIn(List<SquarePosition> squarePositions) {
for (SquarePosition position : squarePositions) {
CroppedFile croppedFile = fileCropper.cropAt(position);
Optional<TwoDDocRawContent> twoDDoc = readTwoDDocOn(croppedFile);
// Zxing seems to work better on a rotated 2D-Doc
Optional<TwoDDocRawContent> twoDDoc = readTwoDDocOn(croppedFile.rotate(180));
if (twoDDoc.isPresent()) {
log.info("Found 2D-Doc on document");
log.info("Found 2D-Doc (with 180° rotation)");
return twoDDoc;
}
twoDDoc = readTwoDDocOn(croppedFile);
if (twoDDoc.isPresent()) {
log.info("Found 2D-Doc (with no rotation)");
return twoDDoc;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fr.dossierfacile.process.file.barcode.twoddoc.reader;

import fr.dossierfacile.process.file.barcode.twoddoc.TwoDDocRawContent;
import lombok.extern.slf4j.Slf4j;

import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Optional;

@Slf4j
public class TwoDDocImageFinder extends TwoDDocFinder {

private final BufferedImage image;

public TwoDDocImageFinder(BufferedImage image) {
super(FileCropper.fromImageSource(image));
this.image = image;
}

@Override
public Optional<TwoDDocRawContent> find2DDoc() {
TwoDDocSize twoDDocSize = TwoDDocSize.on(image);
List<SquarePosition> list = KnownTwoDDocLocations.stream()
.map(location -> location.toCoordinates(image))
.map(coordinates -> coordinates.toSquare(twoDDocSize.width()))
.toList();
return tryToFindTwoDDocIn(list);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fr.dossierfacile.process.file.barcode.twoddoc.reader;

import fr.dossierfacile.process.file.TestFilesUtil;
import fr.dossierfacile.process.file.barcode.twoddoc.TwoDDocRawContent;
import org.junit.jupiter.api.Test;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

class TwoDDocImageFinderTest {

@Test
void should_read_2DDoc_on_image() throws IOException {
Optional<TwoDDocRawContent> actualContent = findOnImage("2ddoc.jpg");
TwoDDocRawContent expectedContent = new TwoDDocRawContent("DC02FR000001125E125B0126FR247500010MME/SPECIMEN/NATACHA\u001D22145 AVENUE DES SPECIMENS\u001D\u001F54LDD5F7JD4JEFPR6WZYVZVB2JZXPZB73SP7WUTN5N44P3GESXW75JZUZD5FM3G4URAJ6IKDSSUB66Y3OWQIEH22G46QOAGWH7YHJWQ");
assertThat(actualContent).isPresent().contains(expectedContent);
}

@Test
void should_return_empty_if_no_2DDoc_found() throws IOException {
Optional<TwoDDocRawContent> twoDDoc = findOnImage("qr-code.jpg");
assertThat(twoDDoc).isEmpty();
}

private static Optional<TwoDDocRawContent> findOnImage(String fileName) throws IOException {
BufferedImage image = TestFilesUtil.getImage(fileName);
return new TwoDDocImageFinder(image).find2DDoc();
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 57f42b3

Please sign in to comment.