R
René Beltman
Hello everyone,
With the JMF I am trying to locally save an image provided by a webcam.
For this I need the BufferToImage class. This class requires a Buffer
object, but how do I configure this buffer object or how can I get an
already configured buffer?
The following code is not creating an image:
import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;
import javax.media.util.BufferToImage;
public class Webcam {
public static void main(String [] args) throws Throwable {
// get the webcam
CaptureDeviceInfo info = CaptureDeviceManager.getDevice("vfw:Microsoft WDM
Image Capture (Win32):0");
DataSource src = Manager.createDataSource(info.getLocator());
src.connect();
src.start();
if (src instanceof PullBufferDataSource) {
PullBufferDataSource dataSource = (PullBufferDataSource) src;
PullBufferStream [] dataStreams = dataSource.getStreams();
System.out.println("PullBufferStream:");
for (int i = 0; i < dataStreams.length; i++) {
System.out.println(i + ": " + dataStreams);
}
} else if (src instanceof PullDataSource) {
PullDataSource dataSource = (PullDataSource) src;
PullSourceStream [] dataStreams = dataSource.getStreams();
System.out.println("PullDataSource:");
for (int i = 0; i < dataStreams.length; i++) {
System.out.println(i + ": " + dataStreams);
}
} else if (src instanceof PushBufferDataSource) {
PushBufferDataSource dataSource = (PushBufferDataSource) src;
PushBufferStream [] dataStreams = dataSource.getStreams();
System.out.println("PushBufferDataSource:");
for (int i = 0; i < dataStreams.length; i++) {
System.out.println(i + ": " + dataStreams);
}
System.out.println("preparing a capture...");
PushBufferStream dataStream = dataStreams[0];
System.out.println("created a dataStream, now creating a buffer...");
Buffer buffer = new Buffer();
System.out.println("created a buffer, now configuring...");
buffer.setDuration(1000000);
System.out.println("buffer configured. Now starting to read...");
dataStream.read(buffer);
System.out.println("reading finished, now detecting a format...");
Format [] formats = info.getFormats();
VideoFormat format = null;
for (int i = 0; i < formats.length && format == null; i++) {
format = formats instanceof VideoFormat ? (VideoFormat) formats :
null;
}
System.out.println("format " + format + " detected, now starting to
create an BufferToImage instance");
BufferToImage bti = new BufferToImage(format);
System.out.println("Instance ready, now creating an image");
java.awt.Image image = bti.createImage(buffer);
if (image != null) {
System.out.println("Image created");
} else {
System.out.println("The image could not been created");
}
} else if (src instanceof PushDataSource) {
PushDataSource dataSource = (PushDataSource) src;
PushSourceStream [] dataStreams = dataSource.getStreams();
System.out.println("PushDataSource:");
for (int i = 0; i < dataStreams.length; i++) {
System.out.println(i + ": " + dataStreams);
}
} else {
System.out.println(src.getClass().getName());
}
}
}
Does someone know wat I am doing wrong and how can I correct it? Thanks in
advantage.
Greetings,
René Beltman.
With the JMF I am trying to locally save an image provided by a webcam.
For this I need the BufferToImage class. This class requires a Buffer
object, but how do I configure this buffer object or how can I get an
already configured buffer?
The following code is not creating an image:
import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;
import javax.media.util.BufferToImage;
public class Webcam {
public static void main(String [] args) throws Throwable {
// get the webcam
CaptureDeviceInfo info = CaptureDeviceManager.getDevice("vfw:Microsoft WDM
Image Capture (Win32):0");
DataSource src = Manager.createDataSource(info.getLocator());
src.connect();
src.start();
if (src instanceof PullBufferDataSource) {
PullBufferDataSource dataSource = (PullBufferDataSource) src;
PullBufferStream [] dataStreams = dataSource.getStreams();
System.out.println("PullBufferStream:");
for (int i = 0; i < dataStreams.length; i++) {
System.out.println(i + ": " + dataStreams);
}
} else if (src instanceof PullDataSource) {
PullDataSource dataSource = (PullDataSource) src;
PullSourceStream [] dataStreams = dataSource.getStreams();
System.out.println("PullDataSource:");
for (int i = 0; i < dataStreams.length; i++) {
System.out.println(i + ": " + dataStreams);
}
} else if (src instanceof PushBufferDataSource) {
PushBufferDataSource dataSource = (PushBufferDataSource) src;
PushBufferStream [] dataStreams = dataSource.getStreams();
System.out.println("PushBufferDataSource:");
for (int i = 0; i < dataStreams.length; i++) {
System.out.println(i + ": " + dataStreams);
}
System.out.println("preparing a capture...");
PushBufferStream dataStream = dataStreams[0];
System.out.println("created a dataStream, now creating a buffer...");
Buffer buffer = new Buffer();
System.out.println("created a buffer, now configuring...");
buffer.setDuration(1000000);
System.out.println("buffer configured. Now starting to read...");
dataStream.read(buffer);
System.out.println("reading finished, now detecting a format...");
Format [] formats = info.getFormats();
VideoFormat format = null;
for (int i = 0; i < formats.length && format == null; i++) {
format = formats instanceof VideoFormat ? (VideoFormat) formats :
null;
}
System.out.println("format " + format + " detected, now starting to
create an BufferToImage instance");
BufferToImage bti = new BufferToImage(format);
System.out.println("Instance ready, now creating an image");
java.awt.Image image = bti.createImage(buffer);
if (image != null) {
System.out.println("Image created");
} else {
System.out.println("The image could not been created");
}
} else if (src instanceof PushDataSource) {
PushDataSource dataSource = (PushDataSource) src;
PushSourceStream [] dataStreams = dataSource.getStreams();
System.out.println("PushDataSource:");
for (int i = 0; i < dataStreams.length; i++) {
System.out.println(i + ": " + dataStreams);
}
} else {
System.out.println(src.getClass().getName());
}
}
}
Does someone know wat I am doing wrong and how can I correct it? Thanks in
advantage.
Greetings,
René Beltman.