11package in .omerjerk .processing .video .android ;
22
3+ import java .lang .reflect .Method ;
34import java .nio .IntBuffer ;
45
56import in .omerjerk .processing .video .android .helpers .FullFrameRect ;
@@ -29,6 +30,9 @@ public static void log(String log) {
2930 if (DEBUG )
3031 System .out .println (log );
3132 }
33+
34+ private Method eventMethod ;
35+ private Object eventHandler ;
3236
3337 protected GLSurfaceView glView ;
3438 protected SurfaceTexture mSurfaceTexture ;
@@ -49,6 +53,7 @@ public static void log(String log) {
4953
5054 public abstract void onResume ();
5155 public abstract void onPause ();
56+ public abstract String getEventName ();
5257
5358 public VideoBase (PApplet parent ) {
5459 super ();
@@ -57,6 +62,8 @@ public VideoBase(PApplet parent) {
5762 parent .registerMethod ("pause" , this );
5863 parent .registerMethod ("resume" , this );
5964
65+ setEventHandlerObject (parent );
66+
6067 glView = (GLSurfaceView ) parent .getSurfaceView ();
6168 pg = (PGraphicsOpenGL )parent .g ;
6269// customTexture = new Texture(pg, width, height);
@@ -69,6 +76,10 @@ public VideoBase(PApplet parent) {
6976 public boolean available () {
7077 return isAvailable ;
7178 }
79+
80+ public void read () {
81+ getImage (false );
82+ }
7283
7384 protected void createSurfaceTexture () {
7485 mFullScreen = new FullFrameRect (new Texture2dProgram (
@@ -160,7 +171,7 @@ public void run() {
160171 surfaceTexture .getTransformMatrix (mSTMatrix );
161172 mFullScreen .drawFrame (mTextureId , mSTMatrix );
162173
163- getImage ( false );
174+ fireEvent ( );
164175
165176 /*
166177 * pixelBuffer.position(0); GLES20.glReadPixels(0, 0, width,
@@ -237,4 +248,37 @@ public void loadPixels() {
237248 pixelBuffer .position (0 );
238249 pixelBuffer .get (this .pixels );
239250 }
251+
252+ private void setEventHandlerObject (Object obj ) {
253+ eventHandler = obj ;
254+
255+ try {
256+ eventMethod = obj .getClass ().getMethod (getEventName (), Capture .class );
257+ return ;
258+ } catch (Exception e ) {
259+ // no such method, or an error.. which is fine, just ignore
260+ }
261+
262+ // The captureEvent method may be declared as receiving Object, rather
263+ // than Capture.
264+ try {
265+ eventMethod = obj .getClass ().getMethod (getEventName (), Object .class );
266+ return ;
267+ } catch (Exception e ) {
268+ // no such method, or an error.. which is fine, just ignore
269+ }
270+ }
271+
272+ private void fireEvent () {
273+ if (eventMethod != null ) {
274+ try {
275+ eventMethod .invoke (eventHandler , this );
276+
277+ } catch (Exception e ) {
278+ System .err .println ("error, disabling " + getEventName () + "()" );
279+ e .printStackTrace ();
280+ eventMethod = null ;
281+ }
282+ }
283+ }
240284}
0 commit comments