Viewing A Web Camera with Java

I used this to test Video For Windows and the Java Media Framework (JMF) for my robotics code. If you install the JMF this should work fine for displaying the image from your webcam.

public class WebCamViewer extends SimpleSwing {

    private Timer timer = new Timer();
	private GetFrame getFrame;
    private ImagePanel imagePanel;
    private int fps = 15;
    public static final String DEFAULT_CAMERA = "vfw://0";
    public static final long serialVersionUID = 1;

    public WebCamViewer() throws Exception {
        init(DEFAULT_CAMERA, GetFrame.RGB_ENCODING,320,240);

    public WebCamViewer(String camera) throws Exception{
        init(camera, GetFrame.RGB_ENCODING,320,240);
    public WebCamViewer(String camera, String encoding, int width, int height) throws Exception{
        init(camera,encoding, width,height);
    private void init(String camera, String encoding, int w, int h) throws Exception{
    	// creates frame grabber
        getFrame = new GetFrame(camera,encoding,w,h);
        imagePanel =  new ImagePanel(w,h);
        // set size of the window
        setSize(w + 8, h+35);

        // add imagePanel
        // make visible

    // start the camera frame capture
    public void start() {

        timer.schedule(new TimerTask() {
            public void run() {
        }, 200, (int)(1000 / fps));

    // stop the camera frame capture
    public void stop() throws Exception{

    // get frame from GetFrame
    public void getPic() {
        try {
            // set to image panel and repaint called from ImagePanel
        } catch (Exception e) {
    // get the framegrabber
    public GetFrame getGetFrame() {
        return getFrame;
    // get frames per second
    public int getFps() {
        return fps;
    //sets frames per second
    public void setFps(int fps) {
        this.fps = fps;

    public static void main(String[] args) {
        try {

            WebCamViewer webcam = new WebCamViewer(WebCamViewer.DEFAULT_CAMERA,GetFrame.RGB_ENCODING,320,240);
        } catch (Exception e) {

Also included is a simple swing class. This will just display a title text and a white background.

public class SimpleSwing extends JFrame {

	public static final long serialVersionUID = 1;
	// constructor
	public SimpleSwing()  {
		// calls JFrame with title
		super("Java Robots Are Cool!");
		// set look & feel
	    // closes
	    addWindowListener(new ExitListener());
	    // sets size
	    setSize(320, 240);
	    // sets pane of content
	    Container content = getContentPane();
	    // sets color to white
	    // shows frame

	  public static void main(String[] args) throws Exception{
	  	SimpleSwing test = new SimpleSwing();