JMStudio中没有网络摄像头检测

时间:2011-11-15 17:56:44

标签: java jmf

当我运行JMF注册表时,我无法使用JMStudio检测/访问我的网络摄像头按下“检测捕获设备”后我得到了以下屏幕

Image

我正在使用ICatch(VI)PC Camera,它是一个USB网络摄像头

这是源代码

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.media.Buffer;
import javax.media.CannotRealizeException;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSourceException;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.AudioFormat;
import javax.media.format.H261Format;
import javax.media.format.RGBFormat;
import javax.media.format.VideoFormat;
import javax.media.format.YUVFormat;
import javax.media.protocol.CaptureDevice;
import javax.media.protocol.DataSource;
import javax.media.util.BufferToImage;

/**
 * A disposable class that uses JMF to serve a still sequence captured from a
 * webcam over a socket connection. It doesn't use TCP, it just blindly
 * captures a still, JPEG compresses it, and pumps it out over any incoming
 * socket connection.
 * 
 * @author Tom Gibara
 *
 */

public class WebcamBroadcaster {

 public static boolean RAW = false;


 private static Player createPlayer(int width, int height) {
  try {

   Vector<CaptureDeviceInfo> devices = CaptureDeviceManager.getDeviceList(null);
//Testing Code
   CaptureDeviceInfo device = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");
   if(device==null) 
   {
   System.out.println("no device");
   }
   else
   System.out.println("device exists");

   System.out.println(devices.size()); // Here i am getting 1
   for (CaptureDeviceInfo info : devices) {
    DataSource source;
    Format[] formats = info.getFormats();

    for (Format format : formats) {

        if (format instanceof AudioFormat) { // only this condition get satisfied
            System.out.println("AudioFormat");
       }

     if ((format instanceof RGBFormat)) {
      RGBFormat rgb = (RGBFormat) format;
      Dimension size = rgb.getSize();
      if (size.width != width || size.height != height) continue;
      if (rgb.getPixelStride() != 3) continue;
      if (rgb.getBitsPerPixel() != 24) continue;
      if ( rgb.getLineStride() != width*3 ) continue;
      MediaLocator locator = info.getLocator();
      source = Manager.createDataSource(locator);
      source.connect();
      System.out.println("RGB Format Found");   
      ((CaptureDevice)source).getFormatControls()[0].setFormat(rgb);
     } else if ((format instanceof YUVFormat)) {
      YUVFormat yuv = (YUVFormat) format;
      Dimension size = yuv.getSize();
      if (size.width != width || size.height != height) continue;
      MediaLocator locator = info.getLocator();
      source = Manager.createDataSource(locator);
      source.connect();
      System.out.println("YUV Format Found");   
      ((CaptureDevice)source).getFormatControls()[0].setFormat(yuv);
     } else {
      continue;
     }

     return Manager.createRealizedPlayer(source);
    }
   }
  } catch (IOException e) {
   System.out.println(e.toString());
   e.printStackTrace();
  } catch (NoPlayerException e) {
   System.out.println(e.toString());
   e.printStackTrace();
  } catch (CannotRealizeException e) {
   System.out.println(e.toString());
   e.printStackTrace();
  } catch (NoDataSourceException e) {
   System.out.println(e.toString());
   e.printStackTrace();
  }
  return null;
 }

 public static void main(String[] args) {
  int[] values = new int[args.length];
  for (int i = 0; i < values.length; i++) {
   values[i] = Integer.parseInt(args[i]);
  }

  WebcamBroadcaster wb;
  if (values.length == 0) {
   wb = new WebcamBroadcaster();
  } else if (values.length == 1) {
   wb = new WebcamBroadcaster(values[0]);
  } else if (values.length == 2) {
   wb = new WebcamBroadcaster(values[0], values[1]);
  } else {
   wb = new WebcamBroadcaster(values[0], values[1], values[2]);
  }

  wb.start();
 }

 public static final int DEFAULT_PORT = 9889;
 public static final int DEFAULT_WIDTH = 320;
 public static final int DEFAULT_HEIGHT = 240;

 private final Object lock = new Object();

 private final int width;
 private final int height;
 private final int port;

 private boolean running;

 private Player player;
 private FrameGrabbingControl control;
 private boolean stopping;
 private Worker worker;

 public WebcamBroadcaster(int width, int height, int port) {
  this.width = width;
  this.height = height;
  this.port = port;
 }

 public WebcamBroadcaster(int width, int height) {
  this(width, height, DEFAULT_PORT);
 }

 public WebcamBroadcaster(int port) {
  this(DEFAULT_WIDTH, DEFAULT_HEIGHT, port);
 }

 public WebcamBroadcaster() {
  this(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_PORT);
 }

 public void start() {
  synchronized (lock) {
   if (running) return;
   player = createPlayer(width, height);
   if (player == null) {
    System.err.println("Unable to find a suitable player");
    return;
   }
   System.out.println("Starting the player");
   player.start();
   control = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
   worker = new Worker();
   worker.start();
   System.out.println("Grabbing frames");
   running = true;
  }
 }

 public void stop() throws InterruptedException {
  synchronized (lock) {
   if (!running) return;
   if (player != null) {
    control = null;
    player.stop();
    player = null;
   }
   stopping = true;
   running = false;
   worker = null;
  }
  try {
   worker.join();
  } finally {
   stopping = false;
  }
 }

 private class Worker extends Thread {

  private final int[] data = new int[width*height];

  @Override
  public void run() {
   ServerSocket ss; 
   try {
    ss = new ServerSocket(port);

   } catch (IOException e) {
    e.printStackTrace();
    return;
   }

   while(true) {
    FrameGrabbingControl c;
    synchronized (lock) {
     if (stopping) break;
     c = control;
    }
    Socket socket = null;
    try {
     socket = ss.accept();

     Buffer buffer = c.grabFrame();
     BufferToImage btoi = new BufferToImage((VideoFormat)buffer.getFormat());
     BufferedImage image = (BufferedImage) btoi.createImage(buffer);

     if (image != null) {
      OutputStream out = socket.getOutputStream();
      if (RAW) {
       image.getWritableTile(0, 0).getDataElements(0, 0, width, height, data);
       image.releaseWritableTile(0, 0);
       DataOutputStream dout = new DataOutputStream(new BufferedOutputStream(out));
       for (int i = 0; i < data.length; i++) {
        dout.writeInt(data[i]);
       }
       dout.close();
      } else {
       ImageIO.write(image, "JPEG", out);
      }
     }

     socket.close();
     socket = null;
    } catch (IOException e) {
     e.printStackTrace();
    } finally {
     if (socket != null)
      try {
       socket.close();
      } catch (IOException e) {
       /* ignore */
      }
    }

   }

   try {
    ss.close();
   } catch (IOException e) {
    /* ignore */
   }
  }

 }

}

我搜索了很多线程,但没有运气

我正在使用Windows XP 以下是我的班级路径值

  

JMFHOME E:\ android \ AR \ jmf-2_1_1e-alljava \ JMF-2.1.1e
  CLASSPATH%JMFHOME%\ lib \ jmf.jar;%JMFHOME%\ lib;

     

JFM lib文件包含jmf.jar,multiplayer.jar,mediaplayer.jar,   customizer.jar

2 个答案:

答案 0 :(得分:0)

重新安装this package再次解决了我的问题

答案 1 :(得分:0)

尝试使用FMJ (Free Media in Java)库,并将以下代码添加到您的主类中。您应该找到网络摄像头,这将使您入门。

static {
    System.setProperty("java.library.path", "D:/fmj-sf/native/win32-x86/");
    try {
        final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
        sysPathsField.setAccessible(true);
        sysPathsField.set(null, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String args[]) throws IOException, NoPlayerException {

    System.setProperty("java.util.logging.config.file", "logging.properties");
    LogManager.getLogManager().readConfiguration();
    // The following is require to register the plugins
    if (!ClasspathChecker.checkAndWarn())
    {
        // JMF is ahead of us in the classpath.  Let's do some things to make this go more smoothly.
        logger.info("Enabling JMF logging");
        if (!JmfUtility.enableLogging())
            logger.warning("Failed to enable JMF logging");

        // Let's register our own prefixes, etc, since they won't generally be if JMF is in charge.
        logger.info("Registering FMJ prefixes and plugins with JMF");
        RegistryDefaults.registerAll(RegistryDefaults.FMJ);
        //RegistryDefaults.unRegisterAll(RegistryDefaults.JMF); // TODO: this can be used to make some things that work in FMJ but not in JMF, work, like streaming mp3/ogg.
        // TODO: what about the removal of some/reordering?
    }

    GlobalCaptureDevicePlugger.addCaptureDevices(); // TODO: this needs to be done globally somewhere.
    final java.util.Vector vectorDevices = CaptureDeviceManager.getDeviceList(null);

    for ( int i = 0;  i < vectorDevices.size();  i++ ) 
    {
        CaptureDeviceInfo infoCaptureDevice = (CaptureDeviceInfo) vectorDevices.get(i);
        System.out.println("CaptureDeviceInfo: ");
        System.out.println(infoCaptureDevice.getName());
        System.out.println("" + infoCaptureDevice.getLocator());
        System.out.println("" + infoCaptureDevice.getFormats()[0]);


    }
    ClasspathChecker.check();
    CaptureDeviceInfo device = (CaptureDeviceInfo) vectorDevices.get(0);
    MediaLocator source = device.getLocator();
    Player player = javax.media.Manager.createPlayer(source);

}