How to convert a Region snapshot to a java byte array.

Asked by Spencer Keller

I'm building a web browser test framework and will be using Allure for reporting test results. In my Java framework a have a Region object that represents the browser (App.focusedWindow()). During a test if a Region.wait(...) fails I want to take “take a snapshot” of the browser for the failure report. Allure wants these snapshots as Java byte arrays. How can I do this? Must I save the browser Region to a file then read it back or is there a better way?

Thanks,
Spence

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Spencer Keller
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

1.1.0+

use
BufferedImage bimg = someRegion.capture().getImage()

in the net (e.g. stack overflow) you will find the code, to get the byte array from a buffered image.

Revision history for this message
Spencer Keller (spencer-keller) said :
#2

Thanks RaiMan. I saw in the ScreenImage javadoc that it is a "CANDIDATE FOR DEPRECATION" so I was trying to stay away from using it but I guess I will for now. Below is my final solution for others:

     Screen scr = new Screen();
     BufferedImage img = scr.capture({someRegion}).getImage();
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     ImageIO.write(img, "png", baos);
     baos.flush();
     byte[] byteImage = baos.toByteArray();

Revision history for this message
RaiMan (raimund-hocke) said :
#3

thanks for the feedback and solution doc

You are right with the deprecated hint: I simply did not remember that.
I will delete that for version 1.1.1+ and bring it back in version 2, when these features will be available in the class Image.