Class CameraHelper

  • All Implemented Interfaces:
    com.github.sarxos.webcam.WebcamListener

    public class CameraHelper
    extends HelperBase
    implements com.github.sarxos.webcam.WebcamListener
    Helper for the cameras JavaScript module. This is based on the webcam-capture package by Bartosz Firyn (SarXos), available from: https://github.com/sarxos/webcam-capture.

    Note that you can use the Ptolemy II ImageDisplay actor to display image sequences captured using this module, but this does not work well when executed within Eclipse. In Eclipse, the Swing event thread blocks "waiting for: OGLRenderQueue$QueueFluher", and spends most of its time blocked rather than rendering. Hence, we do not get smooth video. Perhaps this is an Eclipse bug?

    See the documentation for the JavaScript cameras module to see how to use this.

    If invoking the constructor generates a message like:

      Caused by: java.lang.RuntimeException: Library 'OpenIMAJGrabber' was not loaded successfully from file '/tmp/BridJExtractedLibraries5717506824090765864/OpenIMAJGrabber.so'
      

    Then under Red Hat Linux, install lib4l for 64-bit JVMs, run:

      sudo yum install libstdc++.x86_64
      

    The way to diagnose these problems under Linux is to unjar the webcam-capture jar and run ldd:

      cd /tmp
      jar -xf $PTII/lib/webcam-capture-0.3.12.jar
      ldd ./com/github/sarxos/webcam/ds/buildin/lib/linux_x64/OpenIMAJGrabber.so
      

    Note that the webcam-capture jar contains both 32-bit and 64-bit shared libraries, be sure to run the ldd command on the version appropriate for your JVM.

    Then look for libraries that are listed as "not found", then as root use yum search xxx to find them and then install them.

    For example, if libv4l2.so.0 is not found, then run yum search v4l2 and if v4l2 is not installed, then yum install libv4l.x86_64.

    Since:
    Ptolemy II 11.0
    Version:
    $Id$
    Author:
    Edward A. Lee, Contributor: Christopher Brooks
    Pt.AcceptedRating:
    Red (cxh)
    Pt.ProposedRating:
    Yellow (eal)
    • Constructor Summary

      Constructors 
      Constructor Description
      CameraHelper​(java.lang.Object actor, jdk.nashorn.api.scripting.ScriptObjectMirror currentObj)
      Create the system default camera.
      CameraHelper​(java.lang.Object actor, jdk.nashorn.api.scripting.ScriptObjectMirror currentObj, java.lang.String name)
      Create a camera with the specified name.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String[] cameras()
      Return an array of camera names that are available on this host.
      void close()
      Close the camera.
      static java.lang.String defaultCamera()
      Return the system default camera name.
      java.lang.String getViewSize()
      Return the current view size for this camera, given as a JSON formatted string with fields "width" and "height", for example '{"width":176, "height":144}'.
      void open()
      Open the camera.
      void setViewSize​(java.util.Map spec)
      Set the current view size for this camera, representing the desired size as a Map with integer valued fields "width" and "height".
      AWTImageToken snapshot()
      Return the most recent image obtained by the camera, or null if no image has been obtained.
      java.lang.String[] viewSizes()
      Return an array of view sizes supported by this camera, each given as a string of the form "width=176,height=144", for example.
      void webcamClosed​(com.github.sarxos.webcam.WebcamEvent event)
      Notify this object that its camera has been closed.
      void webcamDisposed​(com.github.sarxos.webcam.WebcamEvent event)
      Notify this object that its camera has been disposed.
      void webcamImageObtained​(com.github.sarxos.webcam.WebcamEvent event)
      Notify this object that a new image has been obtained.
      void webcamOpen​(com.github.sarxos.webcam.WebcamEvent event)
      Notify this object that its camera has been opened.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CameraHelper

        public CameraHelper​(java.lang.Object actor,
                            jdk.nashorn.api.scripting.ScriptObjectMirror currentObj)
                     throws java.io.IOException
        Create the system default camera. If the system does not have a physical camera, then the dummy is used.
        Parameters:
        actor - The actor associated with this camera.
        currentObj - The JavaScript object that this is helping.
        Throws:
        java.io.IOException - If there is no such camera.
      • CameraHelper

        public CameraHelper​(java.lang.Object actor,
                            jdk.nashorn.api.scripting.ScriptObjectMirror currentObj,
                            java.lang.String name)
                     throws java.io.IOException
        Create a camera with the specified name. The name is required to be one of those returned by cameras(), or else a exception will be thrown. If cameras() returns no cameras, then the dummy camera is used.
        Parameters:
        name - The name of the camera.
        actor - The actor associated with this camera.
        currentObj - The JavaScript object that this is helping.
        Throws:
        java.io.IOException - If the camera does not exist.
    • Method Detail

      • cameras

        public static java.lang.String[] cameras()
        Return an array of camera names that are available on this host. This method refreshes the list. The dummy camera is for testing machine configurations that have no camera. Note that selecting the dummy camera typically disables any other cameras that may be present. This is because the WebcamDummyDriver is loaded and set as the default camera.
        Returns:
        A list of camera names, or null if there none.
      • close

        public void close()
        Close the camera.
      • defaultCamera

        public static java.lang.String defaultCamera()
        Return the system default camera name.
        Returns:
        The system default camera, or null if there aren't any.
      • getViewSize

        public java.lang.String getViewSize()
        Return the current view size for this camera, given as a JSON formatted string with fields "width" and "height", for example '{"width":176, "height":144}'.
        Returns:
        A string representing the current view size.
        See Also:
        setViewSize(Map)
      • open

        public void open()
        Open the camera. This starts a sequence of notifications that call webcamImageObtained() and trigger emission of the "image" event for the corresponding JavaScript object.
      • setViewSize

        public void setViewSize​(java.util.Map spec)
                         throws IllegalActionException
        Set the current view size for this camera, representing the desired size as a Map with integer valued fields "width" and "height".
        Parameters:
        spec - A map with integer valued fields "width" and "height".
        Throws:
        IllegalActionException - If either field is absent.
        See Also:
        getViewSize()
      • snapshot

        public AWTImageToken snapshot()
        Return the most recent image obtained by the camera, or null if no image has been obtained.
        Returns:
        A Ptolemy II token containing the image, or null if there is no image to return.
      • viewSizes

        public java.lang.String[] viewSizes()
        Return an array of view sizes supported by this camera, each given as a string of the form "width=176,height=144", for example.
        Returns:
        An array of strings representing available view sizes.
      • webcamClosed

        public void webcamClosed​(com.github.sarxos.webcam.WebcamEvent event)
        Notify this object that its camera has been closed.
        Specified by:
        webcamClosed in interface com.github.sarxos.webcam.WebcamListener
        Parameters:
        event - The event that closed the camera.
      • webcamDisposed

        public void webcamDisposed​(com.github.sarxos.webcam.WebcamEvent event)
        Notify this object that its camera has been disposed.
        Specified by:
        webcamDisposed in interface com.github.sarxos.webcam.WebcamListener
        Parameters:
        event - The event that disposed the camera.
      • webcamImageObtained

        public void webcamImageObtained​(com.github.sarxos.webcam.WebcamEvent event)
        Notify this object that a new image has been obtained. This causes the associated JavaScript Camera object to emit an "image" event with the image as an argument.
        Specified by:
        webcamImageObtained in interface com.github.sarxos.webcam.WebcamListener
        Parameters:
        event - The event of obtaining the image.
      • webcamOpen

        public void webcamOpen​(com.github.sarxos.webcam.WebcamEvent event)
        Notify this object that its camera has been opened.
        Specified by:
        webcamOpen in interface com.github.sarxos.webcam.WebcamListener
        Parameters:
        event - The event that opened the camera.