package com.haxademic.core.hardware.webcam;

import processing.core.PApplet;
import processing.core.PImage;
import processing.video.Capture;

import com.haxademic.core.app.P;

public class WebCamWrapper {

	public static Capture webCam;
	public static PImage lastFrame;

	public static boolean initWebCam( PApplet p, int width, int height ) {
		if( webCam == null ) {
			String[] cameras = Capture.list();
			if (cameras.length == 0) {
				P.println("There are no cameras available for capture.");
				return false;
			} else {
				P.println("Available cameras:");
				for (int i = 0; i < cameras.length; i++) {
					P.println(cameras[i]);
				}
				webCam = new Capture( p, width, height );
				return true;
			}      
		}
		return true;
	}

	public static PImage getImage() {		
		if( webCam.available() == true ) {
			webCam.read();
			lastFrame = webCam;
			return webCam;
		} else {
			P.println("no webcam!!");
			return lastFrame;
		}
	}

	public static void drawImage( PApplet p, int x, int y ){
		p.image( webCam, x, y );
	}

	public static void dispose(){
		if( webCam != null ) webCam.stop();
	}

}

@@ -368,40 +368,108 @@
 %0A%09%09%09
-String%5B%5D cameras = Capture.list(
+P.println(%22getting cameras%22);%0A%09%09%09String%5B%5D cameras = Capture.list();%0A%09%09%09P.println(%22cameras: %22+cameras
 );%0A%09
@@ -753,16 +753,36 @@
 ight );%0A
+%09%09%09%09webCam.start();%0A
 %09%09%09%09retu

Improve webcam initialization