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[i]);
}
} 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[i]);
}
} 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[i]);
}
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[i] instanceof VideoFormat ? (VideoFormat) formats[i] :
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[i]);
}
} 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.
JScoobyCed - 24 Dec 2005 02:01 GMT
> Hello everyone,
>
[quoted text clipped - 3 lines]
> object, but how do I configure this buffer object or how can I get an
> already configured buffer?
[...]
> Buffer buffer = new Buffer();
>
[quoted text clipped - 20 lines]
>
> BufferToImage bti = new BufferToImage(format);
Why are you trying to create a format out of the box. The Buffer object
can give it to you. I am not sure it will solve your issue, but I think
it worths trying:
<code>
VideoFormat format = (VideoFormat) buffer.getFormat();
BufferToImage bti = new BufferToImage(format);
Image image = bti.createImage(buffer);
</code>

Signature
JSC