29
29
import org .robotframework .javalib .annotation .RobotKeywords ;
30
30
import javafx .scene .image .Image ;
31
31
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 .*;
35
34
import java .net .MalformedURLException ;
36
35
import java .net .URL ;
37
36
import java .nio .file .Path ;
@@ -94,14 +93,10 @@ public Object captureImage(Object locator, boolean logImage){
94
93
Path tempPath = Paths .get (getCurrentSessionScreenshotDirectory (), "temp.png" );
95
94
robotContext .getCaptureSupport ().saveImage (resizedImage , tempPath );
96
95
97
- File imageFile = tempPath . toFile ( );
96
+ File imageFile = convertToJpeg ( tempPath );
98
97
byte [] imageBytes = IOUtils .toByteArray (new FileInputStream (imageFile ));
99
98
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 ();
105
100
106
101
Double printSize = targetBounds .getWidth () > 800 ? 800 : targetBounds .getWidth ();
107
102
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) {
190
185
double multiplier = width / 800 ;
191
186
try {
192
187
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 );
194
189
} catch (MalformedURLException e ) {
195
190
throw new JavaFXLibraryNonFatalException ("Unable to log the screenshot: image resizing failed!" );
196
191
}
197
192
}
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
+ }
198
205
}
0 commit comments