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

Showing posts with label filechooser. Show all posts
Showing posts with label filechooser. Show all posts

Friday, 23 August 2013

Image Viewer Ruby Processing

Here is a little sketch, exploring the possibility of using my file_chooser to create a neat little image viewer. I does not make too much sense on its own, but we can take a look at images in ruby-processing, and then possibly do something with them along the lines of exporting to a "pixelated" sketch to cfdg for example.
load_library :file_chooser
attr_reader :img

###########
# Example usage of file_chooser (in this case image file chooser) 
# not sure this is exactly possible with vanilla processing. Here we
# defer setting the size of the sketch, until we know the size of image.
###########

def setup
  file_chooser do |fc|
    fc.look_feel "Nimbus"
    fc.set_filter "Image Files",  [".png", ".jpg"]
    my_file = fc.display
    @img = load_image(my_file)
    size img.width, img.height
  end
end

def draw
  background img
end

File Chooser Library For ruby-processing

Well here it is in action my ruby-processing file_chooser library, that like control panel takes a block the fc.display returns a a string that you can use in load_image etc to load a file, you can do all this in a block if you like (see below). Is a replacement for vanilla processing selectInput function that relies on reflection (and the chooser cannot be so easily customised). PS the my_file temporary variable is not required. The chooser starts in the users home folder on unix (mac/linux) or the working directory on windoes.
load_library :file_chooser
attr_reader :img

###########
# Example file chooser (in this case image file chooser)
# the image loading and resizing is encapsulated in the block,
# borrows heavily off control_panel. 
###########

def setup
  size 600, 600
  file_chooser do |fc|
    fc.look_feel "Nimbus"
    fc.set_filter "Image Files",  [".png", ".jpg"]
    my_file = fc.display
    @img = load_image(my_file)
    img.resize(width, height)
  end
end

def draw
  image img, 0, 0
end

The Chooser (a customised JFileChooser)
The Sketch

Saturday, 17 August 2013

File Chooser in JRuby

Having convinced myself that the vanilla processing selectInput is a piece of crap (yet more reflection nonsense) I though I had better come up with an alternative for ruby processing, here is such an example (yielded by my good friend google and a bit of imagination, works for me):-
require 'rbconfig'
require 'pathname'

System = Java::JavaLang::System
JFile = Java::JavaIo::File
JXChooser = Java::javax::swing::JFileChooser

class ImageFilter < Java::javax::swing::filechooser::FileFilter
  def accept fobj
    return [".png",".jpg"].include? File.extname(fobj.to_s).downcase
    return fobj.isDirectory
  end

  def getDescription
    "Image Files"
  end
end


# Detect OS
OS = case RbConfig::CONFIG['host_os']
  when /darwin/      then :mac
  when /mswin|mingw/ then :windows
else
  :unix
end

# Asks the user to choose an editor.
# Returns either a Pathname object or nil (if canceled)
def pickImage
  # Create a FileChooser
  fc = JXChooser.new("Image Chooser")
  fc.set_dialog_title("Please select an image")
  fc.setFileFilter ImageFilter.new

  if :mac == OS
    fc.setCurrentDirectory(JFile.new(System.getProperty("user.home") << "/Pictures"))
  elsif :windows == OS
    fc.setCurrentDirectory(JFile.new(System.getProperty("user.dir")))
  else
    fc.setCurrentDirectory(JFile.new(System.getProperty("user.home")))
  end

  success = fc.show_open_dialog(nil)
  if success == JXChooser::APPROVE_OPTION
    return Pathname.new(fc.get_selected_file.get_absolute_path)
  else
    nil
  end
end

puts "The user picked: #{pickImage}"

# EOF
This would be the wrapper for ruby-processing?
def select_input(*args, &block)
  if block_given?
    # code using FileChooser output?
  else
    raise ArgumentError, "select_input must be called with a block" , caller
  end
end

Followers

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