Skip to content

Commit b3a0e60

Browse files
authored
Adjust image splitting to account for page dpi (#1296)
Adjust image splitting to account for page dpi Closes #1295
1 parent cdab545 commit b3a0e60

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

src/qz/printer/action/PrintImage.java

+31-13
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.apache.logging.log4j.LogManager;
1717
import org.apache.logging.log4j.Logger;
1818
import qz.common.Constants;
19-
import qz.common.TrayManager;
2019
import qz.printer.PrintOptions;
2120
import qz.printer.PrintOutput;
2221
import qz.utils.ConnectionUtilities;
@@ -26,7 +25,9 @@
2625
import javax.imageio.IIOException;
2726
import javax.imageio.ImageIO;
2827
import javax.print.attribute.PrintRequestAttributeSet;
28+
import javax.print.attribute.ResolutionSyntax;
2929
import javax.print.attribute.standard.OrientationRequested;
30+
import javax.print.attribute.standard.PrinterResolution;
3031
import java.awt.*;
3132
import java.awt.geom.Rectangle2D;
3233
import java.awt.image.BufferedImage;
@@ -39,7 +40,6 @@
3940
import java.io.IOException;
4041
import java.util.ArrayList;
4142
import java.util.List;
42-
import java.util.Locale;
4343

4444

4545
/**
@@ -105,22 +105,40 @@ public void parseData(JSONArray printData, PrintOptions options) throws JSONExce
105105
log.debug("Parsed {} images for printing", images.size());
106106
}
107107

108-
private List<BufferedImage> breakupOverPages(BufferedImage img, PageFormat page) {
108+
private List<BufferedImage> breakupOverPages(BufferedImage img, PageFormat page, PrintRequestAttributeSet attributes) {
109109
List<BufferedImage> splits = new ArrayList<>();
110110

111111
Rectangle printBounds = new Rectangle(0, 0, (int)page.getImageableWidth(), (int)page.getImageableHeight());
112+
PrinterResolution res = (PrinterResolution)attributes.get(PrinterResolution.class);
113+
float dpi = res.getFeedResolution(1) / (float)ResolutionSyntax.DPI;
114+
float cdpi = res.getCrossFeedResolution(1) / (float)ResolutionSyntax.DPI;
112115

113-
int columnsNeed = (int)Math.ceil(img.getWidth() / page.getImageableWidth());
114-
int rowsNeed = (int)Math.ceil(img.getHeight() / page.getImageableHeight());
115-
log.trace("Image to be printed across {} pages", columnsNeed * rowsNeed);
116+
//printing uses 72dpi, convert so we can check split size correctly
117+
int useWidth = (int)((img.getWidth() / cdpi) * 72);
118+
int useHeight = (int)((img.getHeight() / dpi) * 72);
116119

117-
for(int row = 0; row < rowsNeed; row++) {
118-
for(int col = 0; col < columnsNeed; col++) {
119-
Rectangle clip = new Rectangle((col * printBounds.width), (row * printBounds.height), printBounds.width, printBounds.height);
120-
if (clip.x + clip.width > img.getWidth()) { clip.width = img.getWidth() - clip.x; }
121-
if (clip.y + clip.height > img.getHeight()) { clip.height = img.getHeight() - clip.y; }
120+
int columnsNeed = (int)Math.ceil(useWidth / page.getImageableWidth());
121+
int rowsNeed = (int)Math.ceil(useHeight / page.getImageableHeight());
122122

123-
splits.add(img.getSubimage(clip.x, clip.y, clip.width, clip.height));
123+
if (columnsNeed == 1 && rowsNeed == 1) {
124+
log.trace("Unscaled image does not need spit");
125+
splits.add(img);
126+
} else {
127+
log.trace("Image to be printed across {} pages", columnsNeed * rowsNeed);
128+
//allows us to split the image at the actual dpi instead of 72
129+
float upscale = dpi / 72f;
130+
float c_upscale = cdpi / 72f;
131+
132+
for(int row = 0; row < rowsNeed; row++) {
133+
for(int col = 0; col < columnsNeed; col++) {
134+
Rectangle clip = new Rectangle((col * (int)(printBounds.width * c_upscale)), (row * (int)(printBounds.height * upscale)),
135+
(int)(printBounds.width * c_upscale), (int)(printBounds.height * upscale));
136+
137+
if (clip.x + clip.width > img.getWidth()) { clip.width = img.getWidth() - clip.x; }
138+
if (clip.y + clip.height > img.getHeight()) { clip.height = img.getHeight() - clip.y; }
139+
140+
splits.add(img.getSubimage(clip.x, clip.y, clip.width, clip.height));
141+
}
124142
}
125143
}
126144

@@ -157,7 +175,7 @@ public void print(PrintOutput output, PrintOptions options) throws PrinterExcept
157175
//breakup large images to print across pages as needed
158176
List<BufferedImage> split = new ArrayList<>();
159177
for(BufferedImage bi : images) {
160-
split.addAll(breakupOverPages(bi, page));
178+
split.addAll(breakupOverPages(bi, page, attributes));
161179
}
162180
images = split;
163181
}

0 commit comments

Comments
 (0)