Saludos a todos, esta vez tengo un pequeño problema a ver si me pueden ayudar
Resulta que quiero hacer algo con el
JMF y lo que se me ocurrio pues es capturar una foto de la WebCam, buscando en google di con este ejemplo:
import javax.swing.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
{
public static Player player = null;
public CaptureDeviceInfo di = null;
public MediaLocator ml = null;
public Buffer buf = null;
public VideoFormat vf = null;
public BufferToImage btoi = null;
public ImagePanel imgpanel = null;
public SwingCapture()
{
setSize(320,550);
imgpanel = new ImagePanel();
capture.addActionListener(this);
String str1
= "vfw:Logitech USB Video Camera:0"; String str2
= "vfw:Microsoft WDM Image Capture (Win32):0"; di = CaptureDeviceManager.getDevice(str1);
ml = di.getLocator();
try
{
player = Manager.createRealizedPlayer(ml);
player.start();
if ((comp = player.getVisualComponent()) != null)
{
}
}
{
e.printStackTrace();
}
}
public static void main
(String[] args
) {
SwingCapture cf = new SwingCapture();
playerclose();
f.add("Center",cf);
f.pack();
f.setVisible(true);
}
public static void playerclose()
{
player.close();
player.deallocate();
}
{
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
saveJPG(img,"c:\\test.jpg");
}
}
class ImagePanel
extends Panel {
public Image myimg
= null;
public ImagePanel()
{
setLayout(null);
setSize(320,240);
}
public void setImage
(Image img
) {
this.myimg = img;
repaint();
}
{
if (myimg != null)
{
g.drawImage(myimg, 0, 0, this);
}
}
}
{
g2.drawImage(img, null, null);
try
{
}
{
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();
}
{
System.
out.
println("IOException"); }
}
}
Pero el problema es que al compilarlo y ejecutarlo me lanza una excepcion:
NullPointerException, pues al parecer no detecto la cam y devolvio null, en la siguiente linea:
ml = di.getLocator();
El problema no es con la cam porque el programa que trae el
JMF por defecto para hacer las pruebas si la detecta correctamente y me muestra imagen, pero en este ejmplo no. A ver si me ayudan con esto, de antemano gracias