Motion Detector in Java
This link can be used to download the library used to make this project:
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamMotionDetector;
import com.github.sarxos.webcam.WebcamMotionEvent;
import com.github.sarxos.webcam.WebcamMotionListener;
public class DetectMotion implements WebcamMotionListener {
public DetectMotion() {
WebcamMotionDetector detector = new WebcamMotionDetector(Webcam.getDefault());
detector.setInterval(200); // one check per 500 ms
detector.addMotionListener(this);
detector.start();
}
@Override
public void motionDetected(WebcamMotionEvent wme) {
System.out.print("beep ");
}
public static void main(String[] args) {
//Detector
new DetectMotion();
System.in.read(); // keep program open
}
}
Every time a motion is detected by the webcam of your PC 'beep' will be printed.
Use the famous book 'Head First Java' to learn Java in the best way possible, Download the free e-book now.
You can also follow this tutorial by Genuine Coder Youtube channel to build basic programs using this library.