Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

Cap auto-split QR detection retry at 300 DPI (#7821)

Anthony Stirling6ebbbc2089bf
diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/AutoSplitPdfController.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/AutoSplitPdfController.java
index f513391d7a..ea8b79916a 100644
--- a/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/AutoSplitPdfController.java
+++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/misc/AutoSplitPdfController.java
@@ -67,6 +67,10 @@ public class AutoSplitPdfController {
// 150 DPI is sufficient for QR code detection — higher wastes memory and CPU
private static final int QR_DETECTION_DPI = 150;
+ // Ceiling for the retry pass. 300 DPI decodes every divider size 500 does, including a 10mm
+ // code under blur, noise and skew, at a quarter of the pixels.
+ private static final int QR_RETRY_DPI = 300;
+
// Max total pixels before we downscale to avoid OOM on getRGB() allocation
private static final long MAX_IMAGE_PIXELS = 100_000_000L; // ~10000x10000
@@ -233,8 +237,8 @@ public class AutoSplitPdfController {
/**
* Render the full page to an image and scan it for a QR code. Tries a low DPI first (fast, low
- * memory) and only retries at the system's maxDPI if detection fails. The first rendered image
- * is released before the retry to allow GC to reclaim it.
+ * memory) and only retries at {@link #QR_RETRY_DPI} if detection fails. The first rendered
+ * image is released before the retry to allow GC to reclaim it.
*/
private String checkPageByRendering(PDFRenderer pdfRenderer, int pageNum) throws IOException {
log.debug("Rendering page {} at {} DPI for QR detection", pageNum + 1, QR_DETECTION_DPI);
@@ -248,17 +252,17 @@ public class AutoSplitPdfController {
bim = null; // allow GC before potential high-DPI retry
if (result == null) {
- int maxDpi = getSystemMaxDpi();
- if (maxDpi > QR_DETECTION_DPI) {
+ int retryDpi = Math.min(getSystemMaxDpi(), QR_RETRY_DPI);
+ if (retryDpi > QR_DETECTION_DPI) {
log.debug(
"Retrying page {} at {} DPI (low-DPI detection failed)",
pageNum + 1,
- maxDpi);
+ retryDpi);
BufferedImage highRes =
ExceptionUtils.handleOomRendering(
pageNum + 1,
- maxDpi,
- () -> pdfRenderer.renderImageWithDPI(pageNum, maxDpi));
+ retryDpi,
+ () -> pdfRenderer.renderImageWithDPI(pageNum, retryDpi));
result = decodeQRCode(highRes);
}
}
diff --git a/app/core/src/test/java/stirling/software/SPDF/controller/api/misc/AutoSplitPdfControllerTest.java b/app/core/src/test/java/stirling/software/SPDF/controller/api/misc/AutoSplitPdfControllerTest.java
index 09ff35463a..50741da68d 100644
--- a/app/core/src/test/java/stirling/software/SPDF/controller/api/misc/AutoSplitPdfControllerTest.java
+++ b/app/core/src/test/java/stirling/software/SPDF/controller/api/misc/AutoSplitPdfControllerTest.java
@@ -28,6 +28,7 @@ import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
+import org.apache.pdfbox.rendering.PDFRenderer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
@@ -487,6 +488,49 @@ class AutoSplitPdfControllerTest {
}
}
+ @Nested
+ @DisplayName("checkPageByRendering retry DPI")
+ class RetryDpi {
+
+ /** DPIs the renderer is asked for when no QR decodes, for a given configured maxDPI. */
+ private List<Float> renderedDpis(int configuredMaxDpi) throws Exception {
+ applicationProperties.getSystem().setMaxDPI(configuredMaxDpi);
+ List<Float> dpis = new ArrayList<>();
+ PDFRenderer renderer = mock(PDFRenderer.class);
+ when(renderer.renderImageWithDPI(anyInt(), anyFloat()))
+ .thenAnswer(
+ inv -> {
+ dpis.add(inv.getArgument(1));
+ return new BufferedImage(40, 40, BufferedImage.TYPE_INT_RGB);
+ });
+
+ invokeInstance(
+ "checkPageByRendering",
+ new Class<?>[] {PDFRenderer.class, int.class},
+ renderer,
+ 0);
+ return dpis;
+ }
+
+ @Test
+ @DisplayName("retry is capped at 300 even when maxDPI is 500")
+ void capsRetryAt300() throws Exception {
+ assertEquals(List.of(150f, 300f), renderedDpis(500));
+ }
+
+ @Test
+ @DisplayName("a maxDPI below the cap still limits the retry")
+ void respectsLowerConfiguredMax() throws Exception {
+ assertEquals(List.of(150f, 200f), renderedDpis(200));
+ }
+
+ @Test
+ @DisplayName("no retry when maxDPI is already the detection DPI")
+ void skipsRetryAtDetectionDpi() throws Exception {
+ assertEquals(List.of(150f), renderedDpis(150));
+ }
+ }
+
// ---------------------------------------------------------------------
// autoSplitPdf(...) full handler
// ---------------------------------------------------------------------