WebCam 简单粗暴

public class Test {

	static Webcam webcam;
	static WebcamMotionDetector detector;

	public static void init() {
		webcam = Webcam.getDefault();
		webcam.setViewSize(WebcamResolution.VGA.getSize());

		detector = new WebcamMotionDetector(webcam);
		detector.setInterval(200); // one check per 200 ms
		detector.setInertia(2000); // keep "motion" state for 2 seconds
		detector.start();

		Thread t = new Thread("motion-printer") {

			@Override
			public void run() {

				boolean motion = false;
				long now = 0;

				while (true) {
					now = System.currentTimeMillis();
					if (detector.isMotion()) {
						if (!motion) {
							motion = true;
							System.out.println(now + " MOTION STARTED");
						}
					} else {
						if (motion) {
							motion = false;
							System.out.println(now + " MOTION STOPPED");
						}
					}
					try {
						Thread.sleep(50); // must be smaller than interval
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};

		t.setDaemon(true);
		t.start();
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		init();
		System.in.read();
	}

猜你喜欢

转载自sants.iteye.com/blog/2201029