I use java Runtime class to execute system commands. You can use it like :
Runtime.getRuntime().exec("idevicescreenshot -u "+udid+" temp.tiff");
Another way of use with two commands. no need to use temp file.
idevicescreenshot -u $1 screenshot.png
sips -s format png screenshot.png --out screenshot.png
Hi Suaylpozmen,
Thx for the support. I tried the code which you have shared with me. But when I am trying to run this code in Eclipse, I am getting a runtime error as "Cannot run program "idevicescreenshot": error=2, No such file or directory".
I am using the below code:
Runtime.getRuntime().exec("idevicescreenshot -u "+ "b46d35609a2a9e445c8d1faa7c184d93ecc576c7" + "/Users/bhaskar/Downloads/First.tiff");
Can you pls help me in solving the issue.
Thanking you,
Bhaskar.M
@bhaskar123 can you run idevicescreenshot from command line? Eclipse could not find the binary in PATH.
@bhaskar123 no you should only run idevicescreenshot command in terminal not the java code.
if it is okay you have to add path variable to eclipse configuration.
Hi Pr4bh4sh,
I have used the below code, and I am able to get screenshot in Tiff format.
"Runtime.getRuntime().exec("/bin/bash -l -c idevicescreenshot");"
But I want screen shot in jpg. So that I can attach this screenshot to "Extent Reports"
So how can I take screenshot in jpg format.
Thanking you,
Bhaskar.M
with Java:
public void saveScreenToJPGFile(BufferedImage image, String fileUrl) {
try {
// create a blank, RGB, same width and height, and a white background
BufferedImage newBufferedImage = new BufferedImage(image.getWidth(),
image.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);
ImageOutputStream ios = ImageIO.createImageOutputStream(new File(fileUrl));
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(0.90F);
writer.setOutput(ios);
writer.write(null, new IIOImage(newBufferedImage, null, null), iwp);
writer.dispose();
System.out.println(" screenshot saved: " + fileUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
Hi pr4bh4sh,
I am able to take screenshot using this code
"sips -s format png original_screenshot.tiff --out converted_screenshot.png"
But I am unable to open the converted screenshot in web application. I am getting the below error message
"This image cannot be displayed because it contains errors".
can you help me....
Not sure what would be the cause, can you try taking the image and then converting it to png, using the example @Aleksei has provided.
To identify the root cause try opening the tiff image without converting it. If you see the image means there's nothing wrong with file creation and something going wrong with sips
conversion(which is unlikely).