Webcam image capture



I'm attempting to capture an image from a webcam using the below codes.

code snippet:

import javax.media.*;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import java.awt.*;
import javax.media.control.FrameGrabbingControl;
import java.io.*;
import java.language.*;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class FrameCapture {
static MediaLocator ml;
static Player player;
static Component videoScreen;
static Buffer buf = null;
static String str = null;
static BufferToImage btoi = null;
static BufferedImage bi = null;
static Image frameImage = null;
//static FileOutputStream out = null;
static String filename = null;
public static void main(String args) {
try {
ml = new MediaLocator("vfw://0");
player = Manager.createRealizedPlayer(ml);
player.start();
videoScreen = player.getVisualComponent();
Frame frame = new Frame();
frame.setBounds(10 10 300 300);
frame.add(videoScreen);
frame.setVisible(true);
FrameGrabbingControl fgc = (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
if (fgc == null) {
System.out.println("fgc == null");
//System.exit(0);
}
buf = fgc.grabFrame();
btoi = new BufferToImage((VideoFormat) buf.getFormat());
if (btoi == null) {
System.out.println("btoi == null");
}
/*
VideoFormat format = (VideoFormat) buf.getFormat();
Dimension size = format.getSize();
if (format == null) {
System.out.println("format == null");
}
frameImage = btoi.createImage(buf);
if (frameImage == null) {
System.out.println("frameImage == null");
}
bi = new BufferedImage(frameImage.getHeight(null) frameImage.getWidth(null) BufferedImage.TYPE_INT_RGB);
if (bi == null) {
System.out.println("bi == null");
}
*/
java.awt.image.BufferedImage img = (java.awt.image.BufferedImage)btoi.createImage(buf);
try
{
filename = "myImage.png";
File file = new File(filename);
FileOutputStream out = new FileOutputStream(file);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(img);
param.setQuality(1.0f false); // 100% high quality setting no compression
encoder.setJPEGEncodeParam(param);
//encoder.encode ( bi );
encoder.encode(img);
out.close();
}
catch(Exception e) {
System.out.println ("Error saving JPEG: " + e.getMessage() );
}
player.close();
player.deallocate();
}catch(Exception e) {
System.err.println("U r Stupid..: " + e.getMessage());
}
}
}


Here i get a java.language.NullPointerException in the saving JPEG try catch.
I hv altered a little the code snippet in the below approach but i get again an error.

Code:

import javax.media.*;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import java.awt.*;
import javax.media.control.FrameGrabbingControl;
import java.io.*;
import java.language.*;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.media.control.FormatControl;
public class FrameCapture {
static MediaLocator ml;
static Player player;
static Component videoScreen;
static Buffer buf = null;
static String str = null;
static BufferToImage btoi = null;
static BufferedImage bi = null;
static Image frameImage = null;
static String filename = null;
static FormatControl formatControl = null;
static VideoFormat currentFormat = null;

public static void main(String args) {
try {
ml = new MediaLocator("vfw://0");
player = Manager.createRealizedPlayer(ml);
player.start();
videoScreen = player.getVisualComponent();
Frame frame = new Frame();
frame.setBounds(10 10 300 300);
frame.add(videoScreen);
frame.setVisible(true);
formatControl = (FormatControl) player.getControl(
"javax.media.control.FormatControl");
Format currFormat = formatControl.getFormat();
currentFormat = (VideoFormat) currFormat;
FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl(
"javax.media.control.FrameGrabbingControl");
Buffer buffer = fgc.grabFrame();
BufferToImage btoi = new BufferToImage((VideoFormat) buffer.
getFormat());
Image image = btoi.createImage(buffer);
String filename = "betaImage.jpeg";
BufferedImage bi = new BufferedImage(image.getWidth(null)
image.getHeight(null)
BufferedImage.TYPE_INT_RGB);
FileOutputStream out = null;
try {
out = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1.0f false); // 100% high quality setting no compression
encoder.setJPEGEncodeParam(param);
encoder.encode(bi);
out.close();
} catch (Exception e) {
System.out.println("Error saving JPEG: " + e.getMessage());
}
player.close();
player.deallocate();
} catch (Exception e) {
System.err.println("U r Stupid..: " + e.getMessage());
}
}
}


Any solution on what's wrong?


Get Source Code

  Views 48   Posted on : Friday, March 29, 2013 8:40:16 AM
Related Tags:




Replies on "Webcam image capture" :

Nobody has replied on this post , be first to post answer.


Post Your Answer/Comment:

Please Login first to post Reply !


Note: Please Put Code into <code></code> block.




Also See: