Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

screenshot - How to capture selected screen of other application using java?

We are trying to develop a screen capture utility.

How do we capture selected screen of another application using Java? And how do we add a callout to the captured screen?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based on Prajakta's description of the project, I believe some explanation of manipulating a screen shot is in order (I think John did an excellent job of explaining how to capture the screen shot using the java.awt.Robot class). Remember, as Steve McLeod said, Java may not be able to automatically locate the location of the window you want to capture on the screen. This is important, because the Robot class needs to know this location, either automatically or manually from you.

Callouts, text, images, etc can be added to the screen shot via the Graphics2D object you receive when you call the createGraphics() method of the screen shot's BufferedImage. I highly recommend you check out the Graphics2D's API to better understand what it is capable of. I also recommend finding some tutorials, perhaps starting with the the 2D Graphics Tutorial from Sun. The book entitled "Filthy Rich Clients" may also come in useful.

When you finally want to save this modified screen shot, you can use the one of the "write" methods of the ImageIO class.

Here is a very simple, start-to-finish example. It is up to you to fill in whatever details necessary.

I hope this help a little!

Robot robot = new Robot();

// The hard part is knowing WHERE to capture the screen shot from
BufferedImage screenShot = robot.createScreenCapture(x, y, width, height);
Graphics2D graphics = screenShot.createGraphics();

// Add a label to the screen shot
Color textColor = Color.RED;
graphics.setColor(textColor);
graphics.drawString("Some text", textX, textY);

// Save your screen shot with its label
ImageIO.save(screenShot, "png", new File("myScreenShot.png"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...