Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Java captures the current screen image

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how Java captures the current screen image". In daily operation, I believe many people have doubts about how Java captures the current screen image. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "how Java captures the current screen image". Next, please follow the editor to study!

Package com.borland.samples.welcome

/ * *

* Title:

* Description:

* Copyright: Copyright (c) 2001

* Company:

* @ author

* @ version 1.0

, /

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 ()

}

}

At this point, the study on "how Java captures the current screen image" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report