PageRenderTime 85ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/ptolemy/actor/lib/opencv/javacv/ImageReader.java

https://github.com/ptII/ptII
Java | 119 lines | 39 code | 13 blank | 67 comment | 4 complexity | 23222abb70257acf53787a719217b081 MD5 | raw file
  1. /* An actor that capture image from camera by using OpenCV
  2. Copyright (c) 2010-2011 The Regents of the University of California.
  3. All rights reserved.
  4. Permission is hereby granted, without written agreement and without
  5. license or royalty fees, to use, copy, modify, and distribute this
  6. software and its documentation for any purpose, provided that the above
  7. copyright notice and the following two paragraphs appear in all copies
  8. of this software.
  9. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  10. FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  11. ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  12. THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  13. SUCH DAMAGE.
  14. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
  17. PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
  18. CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
  19. ENHANCEMENTS, OR MODIFICATIONS.
  20. PT_COPYRIGHT_VERSION_2
  21. COPYRIGHTENDKEY
  22. */
  23. package ptolemy.actor.lib.opencv.javacv;
  24. import static name.audet.samuel.javacv.jna.highgui.cvLoadImage;
  25. import name.audet.samuel.javacv.jna.cxcore.IplImage;
  26. import ptolemy.actor.lib.Source;
  27. import ptolemy.data.ObjectToken;
  28. import ptolemy.data.type.BaseType;
  29. import ptolemy.kernel.CompositeEntity;
  30. import ptolemy.kernel.util.IllegalActionException;
  31. import ptolemy.kernel.util.NameDuplicationException;
  32. import ptolemy.kernel.util.StringAttribute;
  33. ///////////////////////////////////////////////////////////////////
  34. ////CvCameraCapture
  35. /**
  36. * A simple actor starts a video capture process using
  37. * the Open Computer Vision (OpenCV) Library.
  38. * @author Tatsuaki Iwata, Edward A. Lee, Christopher Brooks
  39. * @version
  40. * @since
  41. * @Pt.ProposedRating
  42. * @Pt.AcceptedRating
  43. */
  44. public class ImageReader extends Source {
  45. /** Construct an actor with the given container and name.
  46. * In addition to invoking the base class constructors, construct
  47. * the <i>init</i> and <i>step</i> parameter and the <i>step</i>
  48. * port. Initialize <i>init</i>
  49. * to IntToken with value 0, and <i>step</i> to IntToken with value 1.
  50. * @param container The container.
  51. * @param name The name of this actor.
  52. * @exception IllegalActionException If the actor cannot be contained
  53. * by the proposed container.
  54. * @exception NameDuplicationException If the container already has an
  55. * actor with this name.
  56. */
  57. public ImageReader(CompositeEntity container, String name)
  58. throws IllegalActionException, NameDuplicationException {
  59. super(container, name);
  60. pathName = new StringAttribute(this, "pathName");
  61. pathName.setExpression("test.png");
  62. output.setTypeEquals(BaseType.OBJECT);
  63. }
  64. ///////////////////////////////////////////////////////////////////
  65. //// ports and parameters ////
  66. /** The name of the file to write to. The default
  67. * value of this parameter is "test.png"
  68. */
  69. public StringAttribute pathName;
  70. ///////////////////////////////////////////////////////////////////
  71. //// public methods ////
  72. /** Output a frame.
  73. * @exception IllegalActionException If thrown while writing to the port.
  74. */
  75. public void fire() throws IllegalActionException {
  76. output.send(0, new ObjectToken(_image));
  77. }
  78. /** Load image from file
  79. * @exception IllegalActionException If thrown by the super class.
  80. */
  81. public void initialize() throws IllegalActionException {
  82. super.initialize();
  83. String pathNameString = pathName.getExpression();
  84. if (_image == null) {
  85. _image = cvLoadImage(pathNameString, 1);
  86. if (_image == null) {
  87. throw new IllegalActionException(this, "Fail to load image "
  88. + _image.getClass());
  89. }
  90. }
  91. }
  92. /** Stop the capture.
  93. * @exception IllegalActionException If thrown by the super class.
  94. */
  95. public void wrapup() throws IllegalActionException {
  96. super.wrapup();
  97. _image.release();
  98. }
  99. ///////////////////////////////////////////////////////////////////
  100. //// private variables ////
  101. private IplImage _image;
  102. }