/ocr/ocrservice/src/com/googlecode/eyesfree/opticflow/FrameProcessor.java

http://eyes-free.googlecode.com/ · Java · 209 lines · 70 code · 31 blank · 108 comment · 1 complexity · 94bc933a142e5bf8fc5aa328895fd6f3 MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.googlecode.eyesfree.opticflow;
  17. import android.graphics.Canvas;
  18. import com.googlecode.eyesfree.env.Size;
  19. import com.googlecode.eyesfree.env.Stopwatch;
  20. import java.util.Vector;
  21. /**
  22. * Abstract class which defines base functionality of a preview frame processor,
  23. * to be called by PreviewLooper. Frame processors are chained together and may
  24. * use the output of preceeding FrameProcessors. Uses the Template Method design
  25. * pattern to delegate functionality to implementing classes. Modified by Alan
  26. * Viverette from Andrew Harp's original source.
  27. *
  28. * @author Andrew Harp
  29. * @author alanv@google.com (Alan Viverette)
  30. */
  31. public abstract class FrameProcessor {
  32. private Stopwatch timer;
  33. private boolean initialized;
  34. private boolean firstFrame;
  35. /**
  36. * @return The amount of time this processor wishes to wait until it sees
  37. * the next frame.
  38. */
  39. protected final long timeTillNextFrameMillis() {
  40. // No processor should pass up its first frame.
  41. if (firstFrame) {
  42. return 0;
  43. }
  44. // Frame processors have the ability to adjust the result of this method
  45. // by
  46. // modifying what is returned from getTimeBetweenFramesMillis().
  47. return getTimeBetweenFramesMillis() - timer.getElapsedMilliseconds();
  48. }
  49. /**
  50. * Overridable method controlling the rate at which this processor can
  51. * consume frames.
  52. *
  53. * @return The amount of time the frame processor wants to wait between
  54. * successive calls to processFrame.
  55. */
  56. protected long getTimeBetweenFramesMillis() {
  57. // Greedy by default; always process available frames.
  58. return 0;
  59. }
  60. /**
  61. * Handles per-processor initialization. Guaranteed to be called at least
  62. * once after the camera size is known and before looping starts. May be
  63. * called multiple times. Calls onInit() on child-classes for customized
  64. * implementation.
  65. *
  66. * @param size
  67. */
  68. protected final synchronized void init(final Size size) {
  69. timer = new Stopwatch();
  70. timer.start();
  71. firstFrame = true;
  72. initialized = true;
  73. onInit(size);
  74. }
  75. /**
  76. * @return whether or not this processor has received at least one call to
  77. * init().
  78. */
  79. protected final boolean isInitialized() {
  80. return initialized;
  81. }
  82. /**
  83. * Frees up any resources consumed by this processor. Calls onShutdown() to
  84. * handle customized cleanup.
  85. */
  86. protected final synchronized void shutdown() {
  87. timer = null;
  88. initialized = false;
  89. onShutdown();
  90. }
  91. /**
  92. * Every time processing thread is started, this method is guaranteed to be
  93. * called.
  94. */
  95. protected final synchronized void start() {
  96. onStart();
  97. }
  98. /**
  99. * Every time processing thread is stopped, this method is guaranteed to be
  100. * called.
  101. */
  102. protected final synchronized void stop() {
  103. onStop();
  104. }
  105. /**
  106. * Processes an incoming frame. Delegates actual processing to child-class.
  107. *
  108. * @param frame the frame to process.
  109. */
  110. protected final synchronized void processFrame(final TimestampedFrame frame) {
  111. timer.reset();
  112. firstFrame = false;
  113. onProcessFrame(frame);
  114. }
  115. /**
  116. * Overridable method for processors to define their own custom
  117. * initialization logic.
  118. *
  119. * @param size
  120. */
  121. protected void onInit(final Size size) {
  122. }
  123. /**
  124. * Overridable method for processors to define their own custom shutdown
  125. * logic.
  126. */
  127. protected void onShutdown() {
  128. }
  129. /**
  130. * Overridable method for processors to define their own custom logic when
  131. * they are started. This method is called every time the processing thread
  132. * is started and a sequence of frames will arrive at the processor.
  133. * onInit() is guaranteed to be called before this method.
  134. */
  135. protected void onStart() {
  136. }
  137. /**
  138. * Overridable method for processors to define their own custom logic when
  139. * they are stopped. This method is called every time the processing thread
  140. * is stopped and frame processor will not receive frames continuously.
  141. * onStart() may be called later when the frame processing thread is
  142. * restarted. Different from onShutdown() which is called when a processor
  143. * is destroyed, this method may be called several times.
  144. */
  145. protected void onStop() {
  146. }
  147. /**
  148. * The one method that FrameProcessors must implement themselves to do their
  149. * customized frame processing.
  150. *
  151. * @param frame the frame to process.
  152. */
  153. protected abstract void onProcessFrame(final TimestampedFrame frame);
  154. /**
  155. * Do all debug drawing here.
  156. *
  157. * @param canvas
  158. */
  159. protected final void drawDebug(Canvas canvas) {
  160. onDrawDebug(canvas);
  161. }
  162. protected void onDrawDebug(Canvas canvas) {
  163. }
  164. protected String getName() {
  165. return this.getClass().getSimpleName();
  166. }
  167. /**
  168. * @return A vector of strings to show, one per line, for debug purposes.
  169. */
  170. protected Vector<String> getDebugText() {
  171. return new Vector<String>();
  172. }
  173. protected final void preprocessFrame(TimestampedFrame frame) {
  174. onPreprocess(frame);
  175. }
  176. protected void onPreprocess(TimestampedFrame frame) {
  177. }
  178. }