This commit is contained in:
Anthony Stirling 2023-05-08 12:36:48 +01:00
parent 7353d69f1a
commit 632f29d1d6
2 changed files with 8 additions and 13 deletions

View file

@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import stirling.software.SPDF.utils.ImageFinder; import stirling.software.SPDF.utils.ImageFinder;
import stirling.software.SPDF.utils.PdfUtils;
import stirling.software.SPDF.utils.ProcessExecutor; import stirling.software.SPDF.utils.ProcessExecutor;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@ -45,13 +46,13 @@ public class BlankPageController {
boolean hasText = !pageText.trim().isEmpty(); boolean hasText = !pageText.trim().isEmpty();
if (hasText) { if (hasText) {
pagesToKeepIndex.add(pageIndex); pagesToKeepIndex.add(pageIndex);
System.out.print("page " + pageIndex + " has text"); System.out.println("page " + pageIndex + " has text");
continue; continue;
} }
boolean hasImages = hasImagesOnPage(page); boolean hasImages = hasImagesOnPage(page);
if (hasImages) { if (hasImages) {
pagesToKeepIndex.add(pageIndex); pagesToKeepIndex.add(pageIndex);
System.out.print("page " + pageIndex + " has image"); System.out.println("page " + pageIndex + " has image");
continue; continue;
} }
} }
@ -66,7 +67,7 @@ public class BlankPageController {
BufferedImage image = pdfRenderer.renderImageWithDPI(i - 1, 300); BufferedImage image = pdfRenderer.renderImageWithDPI(i - 1, 300);
ImageIO.write(image, "png", tempFile.toFile()); ImageIO.write(image, "png", tempFile.toFile());
List<String> command = new ArrayList<>(Arrays.asList("python3", "/scripts/detect-blank-pages.py", tempFile.toString())); List<String> command = new ArrayList<>(Arrays.asList("python3", "./scripts/detect-blank-pages.py", tempFile.toString()));
// Run CLI command // Run CLI command
int returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.PYTHON_OPENCV).runCommandWithOutputHandling(command); int returnCode = ProcessExecutor.getInstance(ProcessExecutor.Processes.PYTHON_OPENCV).runCommandWithOutputHandling(command);
@ -79,12 +80,10 @@ public class BlankPageController {
} }
} }
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputDocument.save(outputStream);
outputDocument.close();
document.close(); document.close();
return new ResponseEntity<>(outputStream.toByteArray(), HttpStatus.OK); return PdfUtils.pdfDocToWebResponse(outputDocument, inputFile.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_blanksRemoved.pdf");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
@ -96,8 +95,4 @@ public class BlankPageController {
imageFinder.processPage(page); imageFinder.processPage(page);
return imageFinder.hasImages(); return imageFinder.hasImages();
} }
// ... rest of the code (ImageFinder class and hasImagesOnPage method)
} }

View file

@ -82,7 +82,7 @@ public class ExtractImageScansController {
for (int i = 0; i < images.size(); i++) { for (int i = 0; i < images.size(); i++) {
Path tempDir = Files.createTempDirectory("openCV_output"); Path tempDir = Files.createTempDirectory("openCV_output");
List<String> command = new ArrayList<>(Arrays.asList("python3", "/scripts/split_photos.py", images.get(i), tempDir.toString(), String.valueOf(angleThreshold), List<String> command = new ArrayList<>(Arrays.asList("python3", "./scripts/split_photos.py", images.get(i), tempDir.toString(), String.valueOf(angleThreshold),
String.valueOf(tolerance), String.valueOf(minArea), String.valueOf(minContourArea), String.valueOf(borderSize))); String.valueOf(tolerance), String.valueOf(minArea), String.valueOf(minContourArea), String.valueOf(borderSize)));
// Run CLI command // Run CLI command