/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Acceder2Frames.java
*
* Created on 30/06/2010, 10:15:01 PM
*/
package com.blogspot.rolandopalermo.sockets;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.imageio.ImageIO;
import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.Processor;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.Timer;
/**
*
* @author Rolando
*/
public class Acceder2Frames
extends javax.
swing.
JFrame {
//Atributos necesarios para acceder a una cámara web
private static Player player = null;
private MediaLocator localizador = null;
private Processor p;
private CaptureDeviceInfo dispositivo = null;
private static String source
= "vfw:Microsoft WDM Image Capture (Win32):0"; private Buffer buffer;
private BufferToImage buffer_image = null;
/** Creates new form Acceder2Frames */
public Acceder2Frames
(String host
) { initComponents();
servidorChat = host;
dispositivo = CaptureDeviceManager.getDevice(source);
localizador = dispositivo.getLocator();
timer
= new Timer (1,
new ActionListener () { //Cada 1 milisegundo capturará el frame de video FrameGrabbingControl fgc = (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
buffer = fgc.grabFrame();
// Convert it to an image
buffer_image = new BufferToImage((VideoFormat)buffer.getFormat());
try {
ImageIO.write(bufferedImage, "jpg", salidaImagen);
byte[] bytesImagen = salidaImagen.toByteArray();
salida.writeObject( bytesImagen );
salida.flush();
// panelCaptura.setImage(img);
System.
err.
println( "El cliente termino la conexión" ); }
}
});
setLocationRelativeTo(null);
setVisible(true);
}
private void iniciarCaptura() {
try {
player = Manager.createRealizedPlayer(localizador);
player.start();
if (player.getVisualComponent() != null) {
panelVideo.
add(player.
getVisualComponent(),
BorderLayout.
CENTER); panelVideo.updateUI();
}
System.
err.
println(e.
toString()); }
}
public void acceso2Frames() {
timer.start();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
panelVideo
= new javax.
swing.
JPanel(); panelOpciones
= new javax.
swing.
JPanel(); botonSalir
= new javax.
swing.
JButton();
setTitle("Captura de Video - Transmisor de video");
panelOpciones.
setBackground(new java.
awt.
Color(204,
204,
204));
botonSalir.setText("Cerrar");
botonSalir.setOpaque(false);
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { botonSalirActionPerformed(evt);
}
});
panelOpciones.add(botonSalir);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.
addComponent(panelVideo, javax.
swing.
GroupLayout.
Alignment.
LEADING, javax.
swing.
GroupLayout.
DEFAULT_SIZE,
351,
Short.
MAX_VALUE) .
addComponent(panelOpciones, javax.
swing.
GroupLayout.
Alignment.
LEADING, javax.
swing.
GroupLayout.
DEFAULT_SIZE,
351,
Short.
MAX_VALUE)) .addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.
addComponent(panelVideo, javax.
swing.
GroupLayout.
DEFAULT_SIZE,
278,
Short.
MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(panelOpciones, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void botonSalirActionPerformed
(java.
awt.
event.
ActionEvent evt
) {//GEN-FIRST:event_botonSalirActionPerformed // TODO add your handling code here:
if(player != null) {
player.close();
player.deallocate();
}
timer.stop();
cerrarConexion();
}//GEN-LAST:event_botonSalirActionPerformed
private void ejecutarCliente() {
try {
conectarAServidor(); // Paso 1: crear un socket para realizar la conexión
salida.flush(); // vacÃar búfer de salida para enviar información de encabezado
acceso2Frames();
System.
err.
println( "El cliente termino la conexión" ); excepcionES.printStackTrace();
}
} // fin del método ejecutarCliente
// conectarse al servidor
}
private void cerrarConexion() {
System.
out.
println( "\nCerrando conexión" ); try {
salida.close();
cliente.close();
excepcionES.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main
(String args
[]) { setDefaultLookAndFeelDecorated(true);
Acceder2Frames ventana = new Acceder2Frames("127.0.0.1");
ventana.iniciarCaptura();
ventana.ejecutarCliente();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.
swing.
JButton botonSalir
; private javax.
swing.
JPanel panelOpciones
; private javax.
swing.
JPanel panelVideo
; // End of variables declaration//GEN-END:variables
}