Permalink
Browse files

Forgot to add demo to github (but in 0.6 release)

  • Loading branch information...
1 parent 519b964 commit 20d8dbd28945910d304ff71e38f8f3ca1f51e1c9 @rscottm rscottm committed Sep 22, 2011
Showing with 60 additions and 0 deletions.
  1. +60 −0 assets/demo-scripts/demo-camera.rb
@@ -0,0 +1,60 @@
+#######################################################
+#
+# demo-camera.rb (by Scott Moyer)
+#
+# Fires up a camera. Take a picture by touching
+# the screen. Pictures are stored in the scripts
+# directory.
+#
+#######################################################
+
+require 'ruboto/activity'
+require 'ruboto/util/toast'
+
+java_import "android.view.SurfaceView"
+java_import "android.hardware.Camera"
+
+ruboto_import "org.ruboto.callbacks.RubotoSurfaceHolderCallback"
+
+class Camera
+ def picture_id
+ @picture_id ||= 0
+ @picture_id += 1
+ end
+end
+
+$activity.start_ruboto_activity "$camera_demo", RubotoActivity, R.style::Theme_NoTitleBar_Fullscreen do
+ def on_create(bundle)
+ @surface_view = SurfaceView.new(self)
+ @surface_view.set_on_click_listener(proc{|v| take_picture})
+ @surface_view.holder.add_callback @holder_callback
+ # Deprecated, but still required for older API version
+ @surface_view.holder.set_type android.view.SurfaceHolder::SURFACE_TYPE_PUSH_BUFFERS
+ self.content_view = @surface_view
+ end
+
+ @holder_callback = RubotoSurfaceHolderCallback.new_with_callbacks do
+ def on_surface_created(holder)
+ $camera = Camera.open # Add (1) for front camera
+ $camera.preview_display = holder
+ $camera.start_preview
+ end
+
+ def on_surface_destroyed(holder)
+ $camera.stop_preview
+ $camera.release
+ $camera = nil
+ end
+ end
+
+ def take_picture
+ picture_file = "#{Dir.pwd}/picture#{$camera.picture_id}.jpg"
+ $camera.take_picture(proc{toast "Piture taken"}, nil) do |data, camera|
+ fos = java.io.FileOutputStream.new(picture_file)
+ fos.write(data)
+ fos.close
+ end
+ picture_file
+ end
+end
+

0 comments on commit 20d8dbd

Please sign in to comment.