Fórum Capturador de telas em java #569587
09/04/2009
0
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import javax.imageio.ImageIO;
import java.io.File;
import java.util.StringTokenizer;
/**
* Description of the Class
*
*@author cmsilva (copernico@javafree.com.br)
*@version 19 de Outubro de 2004
*/
public class ScreenCapture extends JFrame {
private JLabel tela = new JLabel();
private JToolBar barra = new JToolBar();
private Image img;
/**
* Constructor for the ScreenCapture object
*/
public ScreenCapture() {
super("Captura de tela");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tela.setBorder(BorderFactory.createLoweredBevelBorder());
this.getContentPane().add(tela);
this.barra.add(new CapturaAction());
this.barra.add(new SalvarAction());
this.getContentPane().add(barra, BorderLayout.NORTH);
}
/**
* Captura a tela corrente
*
*/
private void capturar() throws Exception {
Robot robo = new Robot();
//Pega a image
this.img = robo.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
//Gera um thumbnail
Image scaledImg = img.getScaledInstance((int) tela.getSize().getWidth(), (int) tela.getSize().getHeight(), Image.SCALE_FAST);
//Exibe o thumbnail
tela.setIcon(new ImageIcon(scaledImg));
tela.validate();
}
/**
* Salva a tela atual em PNG
*
*/
private void salvar() throws Exception {
if (this.img == null) {
return;
}
JFileChooser jfc = new JFileChooser();
//Define um filtro para os arquivos exibidos no Chooser
jfc.setFileFilter(
new FileFilter() {
public boolean accept(File f) {
StringTokenizer stk = new StringTokenizer(f.getName(), ".");
String ext = null;
while (stk.hasMoreTokens()) {
ext = stk.nextToken();
}
if (ext == null) {
return false;
}
if (ext.equalsIgnoreCase("png")) {
return true;
} else {
return false;
}
}
public String getDescription() {
return "Imagens PNG";
}
});
int resp = jfc.showSaveDialog(this);
if (resp == JFileChooser.CANCEL_OPTION) {
return;
}
File outputFile = jfc.getSelectedFile();
//Salva a imagem (api imageio)
ImageIO.write((BufferedImage) this.img, "PNG", outputFile);
}
/**
* Action para capturar a tela
*
*@author cmsilva (copernico@javafree.com.br)
*@version 19 de Outubro de 2004
*/
private class CapturaAction extends AbstractAction {
/**
* Constructor for the Captura object
*/
public CapturaAction() {
super("Capturar");
}
public void actionPerformed(ActionEvent ev) {
try {
capturar();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Action para salvar a tela
*
*@author cmsilva (copernico@javafree.com.br)
*@version 19 de Outubro de 2004
*/
private class SalvarAction extends AbstractAction {
/**
* Constructor for the SalvarAction object
*/
public SalvarAction() {
super("Salvar");
}
public void actionPerformed(ActionEvent ev) {
try {
salvar();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* MAIN
*
*@param args Description of the Parameter
*/
public static void main(String args[]) {
ScreenCapture app = new ScreenCapture();
app.setSize(640, 480);
app.show();
}
}
Claudio Silva
Curtir tópico
+ 0Posts
09/04/2009
João Santos
Gostei + 0
09/04/2009
Grupog3
Gostei + 0
09/04/2009
Daniel Carrazzoni
Gostei + 0
09/04/2009
Daniel Carrazzoni
Gostei + 0
09/04/2009
Daniel Carrazzoni
Gostei + 0
09/04/2009
Arian Pasquali
SELECT * FROM Win32_Process
Gostei + 0
09/04/2009
Arian Pasquali
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ScreenShot {
public static void main(String [] a){
new ScreenShot().atualiza("c:/teste.jpg");
}
public void atualiza(String arquivo){
//tempo de espera
String tempo = "0";
try {
long time = Long.parseLong(tempo) * 1000L;
System.out.println("Waiting " + (time / 1000L) +
" second(s)...");
Thread.sleep(time);
} catch(NumberFormatException nfe) {
System.err.println(tempo + " does not seem to be a " +
"valid number of seconds.");
System.exit(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
Rectangle screenRect = new Rectangle(screenSize);
// criar screenshot
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedImage image = robot.createScreenCapture(screenRect);
try {
ImageIO.write(image, "jpg", new File(arquivo));
System.out.println("Arquivo gerado : "+arquivo);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Gostei + 0
09/04/2009
Daniel Carrazzoni
Gostei + 0
09/04/2009
Arian Pasquali
Gostei + 0
09/04/2009
Arian Pasquali
Gostei + 0
09/04/2009
Wallace Souza
Gostei + 0
09/04/2009
Leonardo Oliveira
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)