Take a screenshot of a specific application on Windows (.exe) in java - java

Is it possible to take a screen capture of a specific application/exe launched on Windows in Java?
I tried this code but it does not take a capture of the application itself and return a black picture:
try {
...
Robot robot = new Robot();
Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage bufferedImage = robot.createScreenCapture(captureSize);
ImageIO.write(bufferedImage,"png",myFile);
...
} catch(AWTException e) {
System.err.println("call a doc!");
}
This is why I think that forcing a screenshot linked with the process launched may solve the problem.
Or maybe they are other ways to take a capture of the displayed screen? (if yes could you please show me some working code?)

Related

testautomation - Can´t test game on android screen

That i want to do is to "click"/"touch" on a android screen through images because i want to test a game and this game don't have any xpath.
If i cant use image recognition then I at least want to "click" on the screen using coordinates.
But none of these options have worked for me so far.
This far has i come. Now i using Appium Driver.
USING IMAGE RECOGNITION ON DESKTOP:
public void playGame() throws InterruptedException, FindFailed {
Screen screen = new Screen();
String imageLibrary = "src/main/resources/automated-tests/images/";
String skipIntro = imageLibrary + "JackAndTheBeanstalk/skip.PNG";
String skipInfo = imageLibrary + "JackAndTheBeanstalk/continue.PNG";
String bet = imageLibrary + "JackAndTheBeanstalk/increaseStakes.PNG";
String play = imageLibrary + "JackAndTheBeanstalk/play.PNG";
Thread.sleep(10000);
if (screen.exists(new Pattern(skipIntro).similar((float) 0.8)) != null) {
System.out.println("Start playing Jack and the beanstalk");
Thread.sleep(10000);
screen.click(new Pattern(skipIntro).similar((float) 0.8));
screen.click(new Pattern(skipIntro).similar((float) 0.8));
screen.click(new Pattern(skipInfo).similar((float) 0.8));
screen.click(new Pattern(bet).similar((float) 0.8));
screen.click(new Pattern(play).similar((float)0.8));
}
}
This image recognition is working fine on desktop. My goal is to do this but on android! But the click method don´t work on android, only desktop. I have heard that you can use tap, performTouchAction and press instead but i don´t know how to use them.
USING PRESS ON ANDROID:
TouchAction action = new TouchAction(driver);
action.press(10, 0).moveTo(22,33).release().perform();
The problem here is that i don´t know how to get the RIGHT coordinates for android.
Have anyone tried to test automate mobile games/or something similar on android or used image recognition on android or used coordinates on android? Or have you used a different tactic to test automate on android or an other device?
Thanks in advance!
Its unclear what library you are using for image recognition (what do you import to use Screen class?).
But its obvious, that only Appium is not enough to test it.
You can research for Appium + Sikuli API or Appium+OpenCV prototypes.
Several links so far:
Appium+Sikuli
Appium+OpenCV+Akaze

JavaFX 2 Save crisp snapshot of scene to disk [duplicate]

This question already has an answer here:
How to save a high DPI snapshot of a JavaFX Canvas
(1 answer)
Closed 7 years ago.
I'm trying to save a snapshot of my application to disk using JavaFX 2, which should be easy considering FX comes with a built-in snapshot function. It does work, however, the image is fuzzy. The application I'm building relies greatly on getting these images to save clear and crisp. Anyone know why this happens? Or how to fix it so it saves what I'm actually seeing when the app runs?
The top image is what saved to disk, the bottom is my app running:
My code for saving the scene is below:
try
{
WritableImage wi = new WritableImage((int) scene.getWidth(), (int) scene.getHeight());
WritableImage snapshot = scene.snapshot(wi);
File output = new File("Full.png");
ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "png", output);
} catch (IOException ex) {
ex.printStackTrace();
}
It looks as if you are running your code on a Mac with Retina display. If that is the case, the answer is here:
How to save a high DPI snapshot of a JavaFX Canvas

OpenCVFrameGrabber javacv in MATLAB doesnt work

I am using this code in MATLAB R2015a:
javaaddpath('javacv.jar')
import org.bytedeco.javacv.*
grabber = OpenCVFrameGrabber(0)
grabber.start()
img = grabber.grab()
The first time I use this code, it works, opens the camera and grub some image.
After I use grabber.stop(), the code doesn't work any more.
Even if restart MATLAB, and make sure I can open the camera in other programs and made sure the camera is available.
When I run this line: grabber.start(), a new window 'Video Source' is opened.
It does not open when I use the code for the first time.
Then I press ok, and there is exception:
Java exception occurred:
org.bytedeco.javacv.FrameGrabber$Exception:
cvCreateCameraCapture() Error: Could not create
camera capture.
at
org.bytedeco.javacv.OpenCVFrameGrabber.start(OpenCVFrameGrabber.java:179)
How I can solve it?
as #Samuel Audet as Mention,
I switch to VideoInputFrameGrabber( for windows )
for linux we need to use FrameGrabber.createDefault(0) , 0 is device index as webcam is at 0 by default
Now the code is look like that, and it work. (Maybe It what i was using in the first time??)
javaaddpath('javacv.jar')
import org.bytedeco.javacv.*
grabber = VideoInputFrameGrabber(0)
grabber.start()
img = grabber.grab()
EDIT:
as #Samuel Audet as Mention,
For cross platform code, I switch to FrameGrabber.createDefault()
javaaddpath('javacv.jar');
import org.bytedeco.javacv.*
grabber = FrameGrabber.createDefault(0);
grabber.start();
img = grabber.grab();

How to save as image an on-screen Canvas3D?

I'm doing a program to create a die (cube) with different image textures based on the input of the user (user choose images on a SWT GUI).
Once the user choose the images, it can visualize the dice on a separate dialog, and perform some rotate operations over it (see, after perform a small rotation to see three faces in the screenshots).
See screenshots: http://pastebin.com/XqJfXL6i
And my problem starts here: I want to save the content of the canvas (the dice with the background in its current form, after being rotated). I've been searching for several codes and I think that my problem is because my current canvas is an "on-screen" canvas and I need an off-screen canvas, which will allow to save the content.
My current code is the following:
http://pastebin.com/ZAv0ATJN
And.. here starts the problem. It throws this exception:
java.lang.IllegalStateException: Canvas3D: Not in off-screen mode
Concretely it fails in this line:
ImageComponent2D ic2d = canvas.getOffScreenBuffer();
As you can see there are several lines commented that I tried before, but they didn't work neither.
Any clue about how to do it?
Thanks!
Based on the comment provided by gouessej (thanks!) finally I use this code, which works fine for my doubt:
private void saveImage(String img) throws Exception {
FileOutputStream fileOut = new FileOutputStream(img);
Robot r = new Robot();
BufferedImage bi = r.createScreenCapture(new java.awt.Rectangle(
(int) frame.getLocationOnScreen().getX(), (int) frame
.getLocationOnScreen().getY(), frame.getBounds().width,
frame.getBounds().height));
ImageIO.write(bi, "jpeg", fileOut);
fileOut.flush();
fileOut.close();
}

Programatically getting a screenshot of a Java applet

I have tried to devise a way to get a screenshot of a Java applet running in a browser, but I can't seem to get it working. I managed successfully to use cutycapt to get screenshots fine from "normal" websites, but I soon found out that qtwebkit which it seems to rely on for the rendering does not support java. I also tried IEcapt thinking that it would somehow inherit the Java rendering capabilities of IE on the system, but it does not work. Flash also does not seem to be working in IEcapt, and it has no flags for enabling plugins, so I am assuming the functionality is not there either.
Does anyone have any thoughts on how you could render something like an /index.jsp to an image from a Windows or Linux command line?
Selenium webdriver might be useful here:
http://docs.seleniumhq.org/projects/webdriver/
It is used primarily for test automation but it might be helpful.
It could be used, for example, like this:
import org.openqa.selenium.*;
WebDriver driver = new FirefoxDriver(); // create a Firefox webdriver instance
driver.get("http://www.google.com/"); // navigate to page
File screenshotFile = ((Screenshot)driver).getScreenshotAs(file); // capture screenshot
// and save to a file
// here you can trigger any necessary actions on the website:
Webelement element = driver.findElement(By.name("q")).sendKeys("xxxxx");
element.click();
new WebDriverWait(driver, 10)).until(ExpectedConditions.titleContains("xxxxx"));
// and capture a new screenshot once the content has changed
File xxxScreenshotFile = ((Screenshot)driver).getScreenshotAs(file);
Have you tried using java.awt.Robot?
Rectangle rect = yourGragphicsConfiguration.getBounds();
BufferedImage image = new Robot().createScreenCapture(rect);
If you know the position of your applet you might be able to get it with
BufferedImage dest = image.getSubimage(appletX, appletY, appletHeight, appletWidth);
You can take a screenshot of Swing/AWT component.
This can be done in 2 ways. In both cases the component must be visible.
Without the robot use:
BufferedImage image = new BufferedImage(component.getWidth(),
component.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
component.paint(g);
With the robot use:
In this case the screenshot of area in which this component is situated will be made. That is, if the component overlaps another application window then the screenshot will contain an area of this another window.
Point point = new Point(0, 0);
SwingUtilities.convertPointToScreen(point, component);
Rectangle region = component.getBounds();
region.x = point.x;
region.y = point.y;
BufferedImage image= new Robot().createScreenCapture(region);
This information is taken from the article: Frequently Asked Questions during Java applet development

Categories