Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Monday 1 June 2015

More Experiments with Java Reflection in Ruby-Processing

I think I may have cracked it this time!!! Recently I have created a ruby-processing fork of Filters4Processing, whose examples include running movie sketches. Now in vanilla processing there are these convenience methods void movieEvent(Movie movie) and void captureEvent(Capture capture)that are used to read the movie/capture (presumably a frame at a time prior to draw loop). These are/were not available via ruby-processing, however I have found a way to make it work by creating a java interface (and compiling into my rpextras.jar). Then I reopen the Processing::App class to include the interface and it just works!!!

The Interface

package processing.core;

/**
 * This interface makes it easier/possible to use the reflection methods
 * void captureEvent(Capture capture); and
 * void movieEvent(Movie movie); from ruby-processing
 * @author Martin Prout
 */
public interface VideoInterface {
    void movieEvent(processing.video.Movie movie);
    void captureEvent(processing.video.Capture capture);
}

The video_event video library script

require 'rpextras'

class Processing::App
  include Java::ProcessingCore::VideoInterface
end
Now if those processing guys had only created some decent interfaces in the first place this would have been a bit easier.

Method in Use

#
# Speed. 
#
# Use the Movie.speed method to change
# the playback speed. The video_event library is
# required to use "movieEvent" java reflection method
#

load_libraries :video, :video_event
include_package 'processing.video'

attr_reader :mov

def setup
  size(640, 360)
  background(0)
  @mov = Movie.new(self, "transit.mov")
  mov.loop
end

def draw
  image(mov, 0, 0)
  new_speed = map(mouse_x, 0, width, 0.1, 2)
  mov.speed(new_speed)
  fill(255)
  text("%.2f" % new_speed << "X", 10, 30)
end

# The java reflection method  
def movieEvent(mov)
  mov.read
end

A capture example
load_library :video, :video_event
include_package 'processing.video'
attr_reader :cam, :my_shader

def setup
  size(640, 480, P2D)
  cameras = Capture.list
  @my_shader = load_shader('edge_detect.glsl')
  my_shader.set('sketchSize', width.to_f, height.to_f)
  start_capture(width, height)
end

def start_capture(w, h)
  @cam = Capture.new(self, w, h)
  cam.start
end

def draw
  image(cam, 0, 0)
  return if mouse_pressed?
  filter(my_shader)
end

# The java reflection method  
def captureEvent(cam)
  cam.read
end

Main problem is an introduction of a compile time dependency on processing video.jar, and also seems to be safer to keep camel case for reflection method, paradoxically that may improve in latest processing where the video library becomes an external library.

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2