Skip to content

Commit 725c774

Browse files
committed
✨ add extracting Split and Crop to file
1 parent dd81035 commit 725c774

14 files changed

Lines changed: 218 additions & 28 deletions

File tree

src/main/java/com/mindee/image/ExtractedImage.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.awt.image.BufferedImage;
55
import java.io.ByteArrayOutputStream;
66
import java.io.IOException;
7+
import java.nio.file.Files;
78
import java.nio.file.Path;
89
import java.nio.file.Paths;
910
import javax.imageio.ImageIO;
@@ -37,7 +38,7 @@ public ExtractedImage(BufferedImage image, String filename, String saveFormat, i
3738
* Write the image to a file.
3839
* Uses the default image format and filename.
3940
*
40-
* @param outputPath the output directory (must exist).
41+
* @param outputPath the output path, it may be a file or a directory.
4142
* @throws IOException Throws if the file can't be accessed.
4243
*/
4344
public void writeToFile(String outputPath) throws IOException {
@@ -48,12 +49,14 @@ public void writeToFile(String outputPath) throws IOException {
4849
* Write the image to a file.
4950
* Uses the default image format and filename.
5051
*
51-
* @param outputPath the output directory (must exist).
52+
* @param outputPath the output path, it may be a file or a directory.
5253
* @throws IOException Throws if the file can't be accessed.
5354
*/
5455
public void writeToFile(Path outputPath) throws IOException {
55-
var imagePath = outputPath.resolve(this.filename);
56-
var outputfile = imagePath.toFile();
56+
if (Files.isDirectory(outputPath)) {
57+
outputPath = outputPath.resolve(this.filename);
58+
}
59+
var outputfile = outputPath.toFile();
5760
ImageIO.write(this.image, this.saveFormat, outputfile);
5861
}
5962

src/main/java/com/mindee/image/ExtractedImages.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public void saveAllToDisk(String outputPath) throws IOException {
1010
}
1111

1212
public void saveAllToDisk(Path outputPath) throws IOException {
13-
for (ExtractedImage image : this) {
14-
image.writeToFile(outputPath);
13+
for (ExtractedImage extractedImage : this) {
14+
extractedImage.writeToFile(outputPath);
1515
}
1616
}
1717
}

src/main/java/com/mindee/pdf/ExtractedPDF.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mindee.input.LocalInputSource;
44
import java.io.IOException;
55
import java.nio.file.Files;
6+
import java.nio.file.Path;
67
import java.nio.file.Paths;
78
import lombok.Getter;
89

@@ -26,14 +27,26 @@ public ExtractedPDF(byte[] fileBytes, String filename) {
2627
}
2728

2829
/**
29-
* Write the PDF to a file.
30+
* Write the extracted PDF to a file.
3031
*
31-
* @param outputPath the output directory (must exist).
32+
* @param outputPath the output path, it may be a file or a directory.
33+
* @throws IOException Throws if the file can't be accessed.
34+
*/
35+
public void writeToFile(Path outputPath) throws IOException {
36+
if (Files.isDirectory(outputPath)) {
37+
outputPath = outputPath.resolve(this.filename);
38+
}
39+
Files.write(outputPath, this.fileBytes);
40+
}
41+
42+
/**
43+
* Write the extracted PDF to a file.
44+
*
45+
* @param outputPath the output path, it may be a file or a directory.
3246
* @throws IOException Throws if the file can't be accessed.
3347
*/
3448
public void writeToFile(String outputPath) throws IOException {
35-
var pdfPath = Paths.get(outputPath, this.filename);
36-
Files.write(pdfPath, this.fileBytes);
49+
writeToFile(Paths.get(outputPath));
3750
}
3851

3952
/**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
package com.mindee.pdf;
22

3+
import java.io.IOException;
4+
import java.nio.file.Path;
35
import java.util.ArrayList;
46

57
public class ExtractedPDFs extends ArrayList<ExtractedPDF> {
8+
public void saveAllToDisk(String outputPath) throws IOException {
9+
saveAllToDisk(Path.of(outputPath));
10+
}
11+
12+
public void saveAllToDisk(Path outputPath) throws IOException {
13+
for (ExtractedPDF extractedPDF : this) {
14+
extractedPDF.writeToFile(outputPath);
15+
}
16+
}
617
}

src/main/java/com/mindee/v2/fileoperations/Crop.java renamed to src/main/java/com/mindee/v2/fileoperations/Cropper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
import java.io.IOException;
99
import java.util.List;
1010

11-
public class Crop {
11+
public class Cropper {
1212
private final ImageExtractor imageExtractor;
1313

14-
public Crop(LocalInputSource inputSource) throws IOException {
14+
public Cropper(LocalInputSource inputSource) throws IOException {
1515
this.imageExtractor = new ImageExtractor(inputSource);
1616
}
1717

18-
public ExtractedImage extractSingle(CropItem cropItem) throws IOException {
18+
public ExtractedImage extractSingleCrop(CropItem cropItem) throws IOException {
1919
return this.imageExtractor
2020
.extractImage(cropItem.getLocation(), cropItem.getLocation().getPage(), 0);
2121
}
2222

23-
public ExtractedImages extractMultiple(List<CropItem> cropItems) {
23+
public ExtractedImages extractMultipleCrops(List<CropItem> cropItems) {
2424
var extractedImages = new ExtractedImages();
2525
for (int i = 0; i < cropItems.size(); i++) {
2626
var cropItem = cropItems.get(i);

src/main/java/com/mindee/v2/fileoperations/Split.java renamed to src/main/java/com/mindee/v2/fileoperations/Splitter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
import java.util.ArrayList;
1010
import java.util.stream.Collectors;
1111

12-
public class Split {
12+
public class Splitter {
1313
BasePDFExtractor pdfSplitter;
1414

15-
public Split(LocalInputSource inputSource) throws IOException {
15+
public Splitter(LocalInputSource inputSource) throws IOException {
1616
this.pdfSplitter = new BasePDFExtractor(inputSource);
1717
}
1818

19-
public ExtractedPDF extractSingle(SplitRange splitRange) throws IOException {
19+
public ExtractedPDF extractSingleSplit(SplitRange splitRange) throws IOException {
2020
return this.pdfSplitter.extractSinglePage(splitRange.getPageRangeDistinct(), true);
2121
}
2222

23-
public ExtractedPDFs extractMultiple(ArrayList<SplitRange> splitRanges) throws IOException {
23+
public ExtractedPDFs extractMultipleSplits(ArrayList<SplitRange> splitRanges) throws IOException {
2424
return this.pdfSplitter
2525
.extractSubDocuments(
2626
splitRanges.stream().map(SplitRange::getPageRangeDistinct).collect(Collectors.toList())

src/main/java/com/mindee/v2/product/crop/CropItem.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.image.ExtractedImage;
6+
import com.mindee.input.LocalInputSource;
7+
import com.mindee.v2.fileoperations.Cropper;
58
import com.mindee.v2.parsing.inference.field.FieldLocation;
69
import com.mindee.v2.product.extraction.ExtractionResponse;
10+
import java.io.IOException;
711
import lombok.AllArgsConstructor;
812
import lombok.EqualsAndHashCode;
913
import lombok.Getter;
@@ -40,4 +44,9 @@ public class CropItem {
4044
public String toString() {
4145
return "* :Location: " + location + "\n :Object Type: " + objectType;
4246
}
47+
48+
public ExtractedImage extractFromInputSource(LocalInputSource inputSource) throws IOException {
49+
var cropper = new Cropper(inputSource);
50+
return cropper.extractSingleCrop(this);
51+
}
4352
}

src/main/java/com/mindee/v2/product/crop/CropResult.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.image.ExtractedImages;
6+
import com.mindee.input.LocalInputSource;
7+
import com.mindee.v2.fileoperations.Cropper;
8+
import java.io.IOException;
59
import java.util.ArrayList;
610
import java.util.StringJoiner;
711
import lombok.AllArgsConstructor;
@@ -24,6 +28,15 @@ public final class CropResult {
2428
@JsonProperty("crops")
2529
private ArrayList<CropItem> crops;
2630

31+
/**
32+
* Based on the crop results, extract the documents into individual files as an
33+
* {@link ExtractedImages} instance.
34+
*/
35+
public ExtractedImages extractFromInputSource(LocalInputSource inputSource) throws IOException {
36+
var cropper = new Cropper(inputSource);
37+
return cropper.extractMultipleCrops(this.crops);
38+
}
39+
2740
@Override
2841
public String toString() {
2942
var joiner = new StringJoiner("\n");

src/main/java/com/mindee/v2/product/split/SplitRange.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.image.ExtractedImages;
6+
import com.mindee.input.LocalInputSource;
7+
import com.mindee.pdf.ExtractedPDF;
8+
import com.mindee.v2.fileoperations.Splitter;
59
import com.mindee.v2.product.extraction.ExtractionResponse;
10+
import java.io.IOException;
611
import java.util.ArrayList;
712
import java.util.LinkedHashSet;
813
import java.util.List;
@@ -45,4 +50,13 @@ public class SplitRange {
4550
public List<Integer> getPageRangeDistinct() {
4651
return new ArrayList<>(new LinkedHashSet<>(this.pageRange));
4752
}
53+
54+
/**
55+
* Based on the crop results, extract the documents into individual files as an
56+
* {@link ExtractedImages} instance.
57+
*/
58+
public ExtractedPDF extractFromInputSource(LocalInputSource inputSource) throws IOException {
59+
var splitter = new Splitter(inputSource);
60+
return splitter.extractSingleSplit(this);
61+
}
4862
}

src/main/java/com/mindee/v2/product/split/SplitResult.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.input.LocalInputSource;
6+
import com.mindee.pdf.ExtractedPDFs;
7+
import com.mindee.v2.fileoperations.Splitter;
8+
import java.io.IOException;
59
import java.util.ArrayList;
610
import java.util.StringJoiner;
711
import lombok.AllArgsConstructor;
@@ -24,6 +28,15 @@ public final class SplitResult {
2428
@JsonProperty("splits")
2529
private ArrayList<SplitRange> splits;
2630

31+
/**
32+
* Based on the split results, extract the documents into individual files as an
33+
* {@link ExtractedPDFs} instance.
34+
*/
35+
public ExtractedPDFs extractFromInputSource(LocalInputSource inputSource) throws IOException {
36+
var splitter = new Splitter(inputSource);
37+
return splitter.extractMultipleSplits(this.splits);
38+
}
39+
2740
@Override
2841
public String toString() {
2942
var joiner = new StringJoiner("\n");

0 commit comments

Comments
 (0)