Skip to content

Commit 8c33ae3

Browse files
committed
Add cropped thumbnail
1 parent 506362d commit 8c33ae3

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/main/java/com/assertthat/selenium_shutterbug/core/Snapshot.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,37 @@ public T withThumbnail(String path, String name, double scale) {
7777
FileUtil.writeImage(thumbnailImage, EXTENSION, thumbnailFile);
7878
return self();
7979
}
80-
80+
81+
/**
82+
* Generate cropped thumbnail of the original screenshot.
83+
* Will save different thumbnails depends on when it was called in the chain.
84+
*
85+
* @param path to save thumbnail image to
86+
* @param name of the resulting image
87+
* @param scale to apply
88+
* @return instance of type Snapshot
89+
*/
90+
public T withCroppedThumbnail(String path, String name, double scale, double cropWidth, double cropHeight) {
91+
File thumbnailFile = new File(path.toString(), name);
92+
if(!Files.exists(Paths.get(path))) {
93+
thumbnailFile.mkdirs();
94+
}
95+
thumbnailImage=ImageProcessor.cropAndScale(image,scale, cropWidth, cropHeight);
96+
FileUtil.writeImage(thumbnailImage, EXTENSION, thumbnailFile);
97+
return self();
98+
}
99+
100+
/**
101+
* Generate cropped thumbnail of the original screenshot.
102+
* Will save different thumbnails depends on when it was called in the chain.
103+
*
104+
* @param scale to apply
105+
* @return instance of type Snapshot
106+
*/
107+
public T withCroppedThumbnail(double scale, double cropWidth, double cropHeight) {
108+
return withCroppedThumbnail(Paths.get(location.toString(), "./thumbnails").toString(), "thumb_" + fileName, scale,cropWidth,cropHeight);
109+
}
110+
81111
/**
82112
* Generate a thumbnail of the original screenshot.
83113
* Will save different thumbnails depends on when it was called in the chain.

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,19 @@ public static boolean imagesAreEqualsWithDiff(BufferedImage image1, BufferedImag
191191
}
192192

193193
public static BufferedImage scale(BufferedImage source, double ratio) {
194-
int w = (int) (source.getWidth() * ratio);
195-
int h = (int) (source.getHeight() * ratio);
194+
return cropAndScale(source, ratio, 1.0,1.0);
195+
}
196+
197+
public static BufferedImage cropAndScale(BufferedImage source,double ratio, double cropWidth, double cropHeight) {
198+
int w = source.getWidth();
199+
int h = source.getHeight();
196200
BufferedImage scaledImage = getCompatibleImage(w, h, source);
197201
Graphics2D resultGraphics = scaledImage.createGraphics();
198202
resultGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
199203
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
200204
resultGraphics.drawImage(source, 0, 0, w, h, null);
201205
resultGraphics.dispose();
202-
return scaledImage;
206+
return scaledImage.getSubimage(0, 0, (int)(w*cropWidth),(int)(h*cropHeight));
203207
}
204208

205209
private static BufferedImage getCompatibleImage(int w, int h, BufferedImage source) {

0 commit comments

Comments
 (0)