All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.sarxos.webcam.WebcamMotionEvent Maven / Gradle / Ivy

Go to download

This library allows you to use your PC webcam, IP or network cameras directly from Java. It's compatible with most operating systems (Windows, Linux, MacOS).

There is a newer version: 0.3.9
Show newest version
package com.github.sarxos.webcam;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.EventObject;


/**
 * Webcam detected motion event.
 *
 * @author Bartosz Firyn (SarXos)
 */
public class WebcamMotionEvent extends EventObject {

	private static final long serialVersionUID = -7245768099221999443L;

	private final double strength;
	private final Point cog;
	private final BufferedImage previousImage;
	private final BufferedImage currentImage;

	/**
	 * Create detected motion event.
	 *
	 * @param detector
	 * @param strength
	 * @param cog center of motion gravity
	 */
	public WebcamMotionEvent(WebcamMotionDetector detector, double strength, Point cog) {
		this(detector, null, null, strength, cog);
	}

    /**
     * Create detected motion event.
     *
     * @param detector
     * @param strength
     * @param cog center of motion gravity
     * @param points list of all detected points
     */
    public WebcamMotionEvent(WebcamMotionDetector detector, double strength, Point cog, ArrayList points) {
        this(detector, null, null, strength, cog, points);
    }

	/**
	 * Create detected motion event.
	 *
	 * @param detector
	 * @param previousImage
	 * @param currentImage
	 * @param strength
	 * @param cog center of motion gravity
	 */
	public WebcamMotionEvent(WebcamMotionDetector detector, BufferedImage previousImage, BufferedImage currentImage, double strength, Point cog) {
		super(detector);
		this.previousImage = previousImage;
		this.currentImage = currentImage;
		this.strength = strength;
		this.cog = cog;
	}

    /**
     * Create detected motion event.
     *
     * @param detector
     * @param previousImage
     * @param currentImage
     * @param strength
     * @param cog center of motion gravity
     * @param points list of all detected points
     */
    public WebcamMotionEvent(WebcamMotionDetector detector, BufferedImage previousImage, BufferedImage currentImage, double strength, Point cog, ArrayList points) {
        this(detector, previousImage, currentImage, strength, cog);
        this.points = points;
    }

    private ArrayList points;
    public ArrayList getPoints(){
        return points;
    }
	
	/**
	 * Get percentage fraction of image covered by motion. 0 is no motion on
	 * image, and 100 is full image covered by motion.
	 *
	 * @return Motion area
	 */
	public double getArea() {
		return strength;
	}

	public Point getCog() {
		return cog;
	}

	public Webcam getWebcam() {
		return ((WebcamMotionDetector) getSource()).getWebcam();
	}

	/**
	 * Returns last image before the motion.
	 * Instance is shared among the listeners, so if you need to change the image, create a copy. 
	 */
	public BufferedImage getPreviousImage() {
		return previousImage;
	}

	/**
	 * Returns image with the motion detected.
	 * Instance is shared among the listeners, so if you need to change the image, create a copy. 
	 */
	public BufferedImage getCurrentImage() {
		return currentImage;
	}

	@Override
	public WebcamMotionDetector getSource() {
		return (WebcamMotionDetector) super.getSource();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy