Skip to content

Commit b1b13a1

Browse files
committed
Reduce the size of images
1 parent 537a5c7 commit b1b13a1

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/main/java/javafxlibrary/keywords/Keywords/ScreenCapturing.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@
2929
import org.robotframework.javalib.annotation.RobotKeywords;
3030
import javafx.scene.image.Image;
3131
import javax.imageio.ImageIO;
32-
import java.io.File;
33-
import java.io.FileInputStream;
34-
import java.io.IOException;
32+
import java.awt.image.BufferedImage;
33+
import java.io.*;
3534
import java.net.MalformedURLException;
3635
import java.net.URL;
3736
import java.nio.file.Path;
@@ -94,14 +93,10 @@ public Object captureImage(Object locator, boolean logImage){
9493
Path tempPath = Paths.get(getCurrentSessionScreenshotDirectory(), "temp.png");
9594
robotContext.getCaptureSupport().saveImage(resizedImage, tempPath);
9695

97-
File imageFile = tempPath.toFile();
96+
File imageFile = convertToJpeg(tempPath);
9897
byte[] imageBytes = IOUtils.toByteArray(new FileInputStream(imageFile));
9998
String encodedImage = Base64.getEncoder().encodeToString(imageBytes);
100-
101-
if (imageFile.delete())
102-
RobotLog.debug("Deleted temporary image file successfully.");
103-
else
104-
RobotLog.debug("Could not delete the file: " + imageFile.toString());
99+
imageFile.delete();
105100

106101
Double printSize = targetBounds.getWidth() > 800 ? 800 : targetBounds.getWidth();
107102
System.out.println("*HTML* <img src=\"data:image/png;base64," + encodedImage + "\" width=\"" + printSize + "px\">");
@@ -190,9 +185,21 @@ private static Image resizeImage(Image image, Path path) {
190185
double multiplier = width / 800;
191186
try {
192187
String url = path.toUri().toURL().toString();
193-
return new Image(url, width / multiplier, height / multiplier, true, false);
188+
return new Image(url, width / multiplier, height / multiplier, true, true);
194189
} catch (MalformedURLException e) {
195190
throw new JavaFXLibraryNonFatalException("Unable to log the screenshot: image resizing failed!");
196191
}
197192
}
193+
194+
private File convertToJpeg(Path path) throws IOException {
195+
BufferedImage bufferedImage;
196+
bufferedImage = ImageIO.read(path.toFile());
197+
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
198+
bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
199+
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, java.awt.Color.WHITE, null);
200+
path.toFile().delete();
201+
Path tempPathJpeg = Paths.get(getCurrentSessionScreenshotDirectory(), "temp.jpg");
202+
ImageIO.write(newBufferedImage, "jpg", tempPathJpeg.toFile());
203+
return tempPathJpeg.toFile();
204+
}
198205
}

0 commit comments

Comments
 (0)