Screen Capture in java Tutorial
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ScreenDemo {
TrayIcon tray;
void addIcon(){
tray=new TrayIcon(Toolkit.getDefaultToolkit().getImage("src/aa.png"));
try{
SystemTray.getSystemTray().add(tray);
}catch (Exception e) {
e.printStackTrace();
}
}
void removeIcon(){
SystemTray.getSystemTray().remove(tray);
}
void screenCapture(){
try{
Robot robo=new Robot();
File folder=new File("D://captures");
if(!folder.exists())folder.mkdir();
String path=folder.getAbsolutePath()+File.separator+System.currentTimeMillis()+".jpg";
Rectangle rec=new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage bi=robo.createScreenCapture(rec);
ImageIO.write(bi,"jpeg", new File(path));
System.out.println("success");
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
final ScreenDemo sd=new ScreenDemo();
sd.addIcon();
sd.tray.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.isMetaDown()){
sd.removeIcon();
}else{
sd.screenCapture();
}
}
});
}
}
No comments:
Post a Comment