Skip to content

Commit 1a1994e

Browse files
committed
Check if new height/width is less than original before cropping
1 parent 56b9576 commit 1a1994e

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/main/java/com/assertthat/selenium_shutterbug/utils/image/ImageProcessor.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,26 @@ public static BufferedImage cropAndScale(BufferedImage source,double ratio, doub
205205
resultGraphics.dispose();
206206
return scaledImage.getSubimage(0, 0, (int)(w*cropWidth),(int)(h*cropHeight));
207207
}
208-
208+
209+
public static BufferedImage cropAndScale(BufferedImage source,double ratio, int maxWidth, int maxHeight) {
210+
int w = source.getWidth();
211+
int h = source.getHeight();
212+
BufferedImage scaledImage = getCompatibleImage(w, h, source);
213+
Graphics2D resultGraphics = scaledImage.createGraphics();
214+
resultGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
215+
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
216+
resultGraphics.drawImage(source, 0, 0, w, h, null);
217+
resultGraphics.dispose();
218+
if(maxWidth != -1 && w > maxWidth){
219+
w = maxWidth;
220+
}
221+
if(maxHeight != -1 && h > maxHeight){
222+
h = maxHeight;
223+
}
224+
return scaledImage.getSubimage(0, 0, w,h);
225+
}
226+
227+
209228
private static BufferedImage getCompatibleImage(int w, int h, BufferedImage source) {
210229
BufferedImage bimage = null;
211230
try{

0 commit comments

Comments
 (0)