[Java] save captured screen or image on hard disk

Asked by vishal bhat

Hi

Region region = new Region( 50,100,1300,200);
     region.highlight();
     Screen s2 = region.getScreen();

     BufferedImage bf = s2.capture().getImage();
     echo("data of image"+ bf.getData());

Now after getting bufferedimage - how can i reuse it in sikuli script or better save it to my imagestore on local hard drive.

Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Anshu (anshu-ca) said :
#1

Hi Vishal,
I am using something like this - when i take a screen capture - incase my test fails.
YOU can modify it as per your needs.

In my sikuli script , i have

m=capture(1,1,1020,900)
sName = sWorkingDir + sFileName+".png" # you can give values to these variables
shutil.copy(m,sName) # you need to import shutil in the beginning of your script

So this will copy the image file in your directory with whatever name you pass to it..

Hope that helps.
Anshu

Revision history for this message
vishal bhat (crab19) said :
#2

Hi Anshu

Thanks for the pointers. One query thoug - whats your m object?

Revision history for this message
vishal bhat (crab19) said :
#3

Also i think shutil is python utility...i am using java to write tests

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

I think you are using the Sikuli Java API on a very low level (bufferedImage = in memory bitstream).

You should stay on the file level, if you do not have the need to do something valuable with the image content.

String bf = s2.capture();

now bf contains the filename of the captured image in the temp folder. You have to simply move it to your image folder.

That was exactly, what Anshu proposed, saying, you have to adapt it to your situation.

BTW: I am not sure about your intention: your approach captures the screen and not the specified region.

if you want to capture the region: (transcribing Anshu's suggestion)

Region reg = new Region( 50,100,1300,200);
String imgFile = Screen.capture(reg)

Revision history for this message
vishal bhat (crab19) said :
#5

BufferedImage bf = s2.capture().getImage();
File file1 = new File(imagePath+"/destFile.png");
ImageIO.write(bf, "png", file1);

import java.io.File;

This solved my problem

Thanks for all the pointers

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

solved by questioner himself

Revision history for this message
Viral Bhatt (virubhatt7) said :
#7

I tried using the shutil.copy(src, dest) but it gave me following error, any comments??

File "C:\Program Files\Sikuli X\sikuli-script.jar\Lib\shutil.py", line 85, in copy
File "C:\Program Files\Sikuli X\sikuli-script.jar\Lib\shutil.py", line 52, in copyfile
IOError: (13, 'EACCES', Imagefilename)