What is the method for Java to capture the current screen image?
This article mainly explains "what is the method of capturing the current screen image by Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the method of capturing the current screen image by Java".
Import java.awt.*
Import java.awt.image.*
Import com.sun.image.codec.jpeg.*
Class ImageCanvas extends Canvas {
Private Image image
Private Dimension prefSize
Public ImageCanvas (Image image) {
This.image = image
CalculatePreferredSize ()
}
Public void setImage (Image image) {
This.image = image
CalculatePreferredSize ()
Repaint ()
}
Private void calculatePreferredSize () {
PrefSize = new Dimension (image.getWidth (this), image.getHeight (this))
System.out.println (image.getWidth (this))
SetSize (prefSize)
}
Public Dimension getPreferredSize () {
Return prefSize
}
Public Dimension getMinimumSize () {
Return prefSize
}
Public void update (Graphics g) {
Paint (g)
}
Public void paint (Graphics g) {
G.drawImage (image,0,0,null)
}
}
Class ImageUtils {
Public static Image getScreenImage () {
Robot robot
Try {
Robot = new Robot ()
} catch (Exception e) {
Throw new RuntimeException ("unable to construct Robot")
}
Dimension screenDims = Toolkit.getDefaultToolkit () .getScreenSize ()
Image screen = robot.createScreenCapture (new Rectangle (450 and 350)). GetScaledInstance (400 and 300)
MediaTracker tracker = new MediaTracker (new Label ())
Tracker.addImage (screen,1)
Try {
Tracker.waitForID (0)
} catch (InterruptedException e) {/ * *... * /}
Return screen
}
}
Public class ScreenCapture extends Frame {
Private ImageCanvas canvas = new ImageCanvas (ImageUtils.getScreenImage ())
Public ScreenCapture () {
Add (canvas)
SetSize (400300)
SetVisible (true)
Thread imageThread = new UpdateThread ()
ImageThread.setDaemon (true)
ImageThread.start ()
}
Class UpdateThread extends Thread {
Public void run () {
While (true) {
Try {
Thread.currentThread (). Sleep (1000)
}
Catch (Exception ex) {
}
Canvas.setImage (ImageUtils.getScreenImage ())
Validate ()
}
}
}
Public static void main (String [] args) {
New ScreenCapture ()
}
}
Thank you for your reading, the above is the content of "what is the method of capturing the current screen image by Java". After the study of this article, I believe you have a deeper understanding of what the method of capturing the current screen image by Java is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!