Programa Servidor
Inicializa webcam captura uma imagem e envia via socket para porta 8189, e repere isso atraves de um time, ou seja fica sempre enviando uma imagem, quando o cliente pega a imagem ele busca outra e envia.
[color=red]O programa cliente é simples, em j2me apenas acessa via socketconnection usando um inputstream,
o programa funciona cerca de 40 segundos, capturando e enviando imagem, so que apos 40 segundo da um erro.
ja tentei aumentar a memoria da vm mais nao adianta so retarda o erro.
Alguem tem alguma ideia de como resolver isso???[/color]
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
/*
- JFrame.java
- Created on 14/07/2010, 20:38:22
*/
package javaapplication16;
import javax.media.CannotRealizeException;
import javax.media.NoPlayerException;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
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.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.JOptionPane;
public class JFrame extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
public static Player player = null;
public CaptureDeviceInfo di = null; // @jve:decl-index=0:
public MediaLocator ml = null;
//private Buffer buf = null;
//public Image img = null;
//public Image img1 = null;
//public Image img2 = null;
public VideoFormat vf = null;
//public BufferToImage btoi = null;
// public int sourceWidth ;
// public int sourceHeight ;
//public int thumbWidth = 64;
//public int thumbHeight = -1;
//private int ativa=0;
/** Creates new form JFrame */
public JFrame() {
initComponents();
inicializa();
this.setSize(600,500);
setLocation(170, 05);
}
private void inicializa(){
String str2 = “vfw//0”;
di=null;
ml=null;
di = CaptureDeviceManager.getDevice(str2);
ml = new MediaLocator(“vfw://0”);
try {
player = Manager.createRealizedPlayer(ml);
} catch (IOException ex) {
System.out.println(“Entrou 1”);
Logger.getLogger(JFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoPlayerException ex) {
Logger.getLogger(JFrame.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(“Entrou 2”);
} catch (CannotRealizeException ex) {
Logger.getLogger(JFrame.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Entrou 3");
}
player.start();
Component comp;
if ((comp = player.getVisualComponent()) != null) {
add(comp, BorderLayout.NORTH);
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Conectar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jMenu1.setText("Sobre");
jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jMenu1MouseClicked(evt);
}
});
jMenu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu1ActionPerformed(evt);
}
});
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton2))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(227, Short.MAX_VALUE)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
captura();
}
public void captura(){
//
Buffer buf = null;
BufferToImage btoi = null;
Image img = null;
FrameGrabbingControl fgc = (FrameGrabbingControl) player
.getControl(“javax.media.control.FrameGrabbingControl”);
buf=null;
img=null;
buf = fgc.grabFrame();
try {
btoi = new BufferToImage((VideoFormat) buf.getFormat());
img = createImage(300, 200);
img = btoi.createImage(buf) ; ////aqui
// fgc=null;
gravaImg(img);///aqui
//parece que trava aqui jgi0949gjidjjjgenbnnon
//dfffefesfe
//fesfsxxxxxxxxxxxxxxxx
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "não foi possivel encontrar " + "o dispositivo para a captura da imagem.");
}
}
private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(null, “Programador Jean Ricier - Acesso remoto a webcam J2se e j2me”);
}
private void jMenu1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(null, “Programador Jean Ricier - Acesso remoto a webcam J2se e j2me”);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
player.close();
new JFrame();
}
public void gravaImg (Image imagem) throws IOException{
Image img1=null;
BufferedImage img=null;
try {
// ImageIO.write((RenderedImage) imagem, "jpg", new File(caminho));
ServerSocket s=new ServerSocket(8189);
Socket incoming=s.accept();
OutputStream os=incoming.getOutputStream();
img=(BufferedImage) imagem;
BufferedImage new_img = new BufferedImage(300, 200, BufferedImage.TYPE_INT_RGB);
Graphics2D g = new_img.createGraphics();
g.drawImage(imagem, 0, 0, 300, 200, null);
ImageIO.write(new_img, "png", os);
//sem isso nao roda cai no exeption
os.close(); //xx
s.close(); //xx
///xxxxxxxxxxxxxxxxxxxx
}
catch (IOException e) {
JOptionPane.showMessageDialog(null, "não foi possivel encontrar " + “o dispositivo para a captura da imagem.”);
e.printStackTrace();
}
captura();//////e aqui e aqui o problemafjfjdskkfsjfskk
////f/df/sf/sf/ss/f/f/f/f/f/f/f/
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
// End of variables declaration
}
Exception in thread “AWT-EventQueue-0” java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferInt.(DataBufferInt.java:41)
at java.awt.image.Raster.createPackedRaster(Raster.java:458)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
at java.awt.image.AffineTransformOp.createCompatibleDestImage(AffineTransformOp.java:454)
at com.sun.media.util.BufferToBufferedImage.createImage(BufferToBufferedImage.java:69)
at javax.media.util.BufferToImage.createImage(BufferToImage.java:171)
at javaapplication16.JFrame.captura(JFrame.java:172)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
at javaapplication16.JFrame.captura(JFrame.java:175)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
at javaapplication16.JFrame.captura(JFrame.java:175)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
at javaapplication16.JFrame.captura(JFrame.java:175)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
at javaapplication16.JFrame.captura(JFrame.java:175)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
at javaapplication16.JFrame.captura(JFrame.java:175)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
at javaapplication16.JFrame.captura(JFrame.java:175)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
at javaapplication16.JFrame.captura(JFrame.java:175)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
at javaapplication16.JFrame.captura(JFrame.java:175)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
at javaapplication16.JFrame.captura(JFrame.java:175)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
at javaapplication16.JFrame.captura(JFrame.java:175)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
at javaapplication16.JFrame.captura(JFrame.java:175)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
at javaapplication16.JFrame.captura(JFrame.java:175)
at javaapplication16.JFrame.gravaImg(JFrame.java:235)
CONSTRUÍDO COM SUCESSO (tempo total: 4 minutos 41 segundos)
