← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #164161]: [How to] Capture screenshot directly using java Robot features

 

Question #164161 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/164161

Description changed to:
*** note from the developer (comment #5)

Actually Screen.capture returns a ScreenImage
(http://sikuli.org/doc/java-x/org/sikuli/script/ScreenImage.html), which
has a getImage method returns a java.awt.image.BufferedImage. One can
use ImageIO.write to write this BufferedImage to disk with any format
that ImageIO supports.

----------------------------------------------------------------------------

In Submitting this in order to maybe help others with the task. I came
to a point where I needed to capture screenshots at certain checkpoints
(To check how the screen was after performing certain tasks). And since
I could not find any information about capturing screenshots on the
launchpad page of sikuli i decided to post my solution.

In python there are many libs for this but none of them is contained in
jython (at least what I know). So I figured and of course there Is
possible to do this in some ways using java.

Most of the solutions i found online did contain very much code so I
decided to make them contain a little less code.

In order to perform this task we need to import three modules from three
different libs.

Here Is the code for capturing a screenshot using java.

#needed imports
from java.awt import *
from java.io import File
from javax.imageio import ImageIO

#set Robot to robot
robot = Robot()

#makes myScreenshot contain the screenshot which shall be saved to an imagefile, this version is 
#- capturing the whole screen. But in order to decide which area you want to capture 
#- replace "Toolkit.getDefaultToolkit().getScreenSize()))" with (x, y) where x and y are coordinates

myScreenshot =
robot.createScreenCapture(Rectangle(Toolkit.getDefaultToolkit().getScreenSize()))

#saves myScreenshot to a .jpg called myScreenshot, if you want to save in other formats than .jpg
#- just replace "jpg" to e.g .png and replace .jpg with e.g .png

ImageIO.write(myScreenshot, "jpg", File(r"C:\myScreenshot.jpg"))

In order to capture screenshots "on the way" inside a test or something
it makes sense to put the code inside a function.

Hope this has some value. :-)

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.