Ai galera já procurei em tudo quanto é lugar e não acho ninguém que tenha resolvido,
muitos perguntam mas a maioria abandona e não posta nenhuma resposta!
Preciso criar uma applet que comunica com a webcam e tira uma foto e salva …
eu consegui fazer com swing mas quando tento fazer com applet da uma exeption,
exeception que mostra :
o codigo que eu faço no Swing é o seguinte:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
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.JButton;
import javax.swing.JComponent;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class SwingCapture extends Panel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
public static Player player = null;
public CaptureDeviceInfo di = null;
public MediaLocator ml = null;
public JButton capture = null;
public Buffer buf = null;
public Image img = null;
public VideoFormat vf = null;
public BufferToImage btoi = null;
public ImagePanel imgpanel = null;
public int cont = 0;
public SwingCapture(){
setLayout(new BorderLayout());
setSize(100,100);
imgpanel = new ImagePanel();
capture = new JButton("Capture");
capture.addActionListener(this);
capture.setLocation(10, 300);
imgpanel.add(capture);
capture.setVisible(true);
String str2 = "vfw//0";
di = CaptureDeviceManager.getDevice(str2);
ml = new MediaLocator("vfw://0");
try{
player = Manager.createRealizedPlayer(ml);
player.start();
Component comp;
if ((comp = player.getVisualComponent()) != null){
add(comp,BorderLayout.NORTH);
}
add(capture,BorderLayout.SOUTH);
add(imgpanel,BorderLayout.NORTH);
}catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
Frame f = new Frame("SwingCapture");
SwingCapture cf = new SwingCapture();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
playerclose();
System.exit(0);
}});
f.add("Center",cf);
f.pack();
f.setSize(new Dimension(500,550));
f.setVisible(true);
}
public static void playerclose(){
player.close();
player.deallocate();
}
public void actionPerformed(ActionEvent e){
JComponent c = (JComponent) e.getSource();
String caminho = "C:\\Users\\milton\\Desktop\\fotos\\"; //caminho para ser salvo
if (c == capture){
// pegando o frame
FrameGrabbingControl fgc = (FrameGrabbingControl)
player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
// Convertendo buffer em imagem
btoi = new BufferToImage((VideoFormat)buf.getFormat());
img = btoi.createImage(buf);
/*/ show the image
imgpanel.setImage(img);
imgpanel.setSize(320, 230);
*/
//Verificando se já existe o arquivo com o mesmo nome na pasta!
File existe = new File(caminho+"test"+cont+".jpg");
while(existe.exists()){ //se existe incrementa até achar um numero vazio.
cont++;
existe = new File(caminho+"test"+cont+".jpg");
}
// chamando o metodo para salvar a imagem
saveJPG(img, caminho+"test"+cont+".jpg");
}
}
class ImagePanel extends Panel {
/**
*
*/
private static final long serialVersionUID = 1L;
public Image myimg = null;
public ImagePanel(){
setLayout(null);
setSize(320,240);
}
public void setImage(Image img){
this.myimg = img;
repaint();
}
public void paint(Graphics g){
if (myimg != null){
g.drawImage(myimg, 0, 0, this);
}
}
}
public static void saveJPG(Image img, String s){
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, null, null);
FileOutputStream out = null;
try {
out = new FileOutputStream(s);
}catch (java.io.FileNotFoundException io){
System.out.println("File Not Found");
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(0.5f,false);
encoder.setJPEGEncodeParam(param);
try{
encoder.encode(bi);
out.close();
}catch (java.io.IOException io){
System.out.println("IOException");
}
}
}
e o codigo que eu to tentando usar na applet é:
[code]package br.ultramax.webcam;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.cdm.CaptureDeviceManager;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
/**
- @author administrator
*/
public class WebcamCaptureApplet extends Applet implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public static Player player = null;
public CaptureDeviceInfo di = null;
public MediaLocator ml = null;
public JButton capture = null;
public Buffer buf = null;
public Image img = null;
public VideoFormat vf = null;
public BufferToImage btoi = null;
public ImagePanel imgpanel = null;
public URL url;
public URLConnection conn;
public String hostname;
public int port;
public void init(){
setLayout(new BorderLayout());
setSize(320,550);
imgpanel = new ImagePanel();
capture = new JButton("Capture");
capture.addActionListener(this);
//This may differ check the jmf registry for
// correct entry
String str2 = "vfw//0";
di = CaptureDeviceManager.getDevice(str2);
hostname = getCodeBase().getHost();
port = getCodeBase().getPort();
ml = new MediaLocator("vfw://0");//di.getLocator();
try {
player = Manager.createRealizedPlayer(ml); //DA O ERRO AQUI! :evil: :evil: :evil:
player.start();
Component comp;
if ((comp = player.getVisualComponent()) != null) {
add(comp,BorderLayout.NORTH);
}
add(capture,BorderLayout.CENTER);
add(imgpanel,BorderLayout.SOUTH);
} catch (Exception e) {
e.printStackTrace();
}
}
public void paint (Graphics g) {
}
public static void playerclose() {
player.close();
player.deallocate();
}
public void actionPerformed(ActionEvent e) {
JComponent c = (JComponent) e.getSource();
if (c == capture) {
// Grab a frame
FrameGrabbingControl fgc = (FrameGrabbingControl)
player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
// Convert it to an image
btoi = new BufferToImage((VideoFormat)buf.getFormat());
img = btoi.createImage(buf);
// show the image
imgpanel.setImage(img);
// save image
UploadImage(img);
}
}
public void start(){
}
class ImagePanel extends Panel {
public Image myimg = null;
public ImagePanel() {
setLayout(null);
setSize(320,240);
}
public void setImage(Image img) {
this.myimg = img;
repaint();
}
public void paint(Graphics g) {
if (myimg != null) {
g.drawImage(myimg, 0, 0, this);
}
}
}
}
[/code]
Estou usando o JMF e já está instalado corretamente pois no Swing roda normal tira a foto e salva bonitinho!
Se alguem souber ajudar por favor!