<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lasse Bunk&#039;s weblog &#187; Webcam</title>
	<atom:link href="http://lassebunk.dk/tag/webcam/feed/" rel="self" type="application/rss+xml" />
	<link>http://lassebunk.dk</link>
	<description>Ruby on Rails, programming, and SEO</description>
	<lastBuildDate>Sat, 19 May 2012 10:18:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Updated for Rails 3: Using Paperclip and jpegcam to get pictures from your webcam into Rails</title>
		<link>http://lassebunk.dk/2011/02/19/paperclip-jpegcam-webcam-rails3/</link>
		<comments>http://lassebunk.dk/2011/02/19/paperclip-jpegcam-webcam-rails3/#comments</comments>
		<pubDate>Fri, 18 Feb 2011 23:29:12 +0000</pubDate>
		<dc:creator>lassebunk</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[jpegcam]]></category>
		<category><![CDATA[Paperclip]]></category>
		<category><![CDATA[Webcam]]></category>

		<guid isPermaLink="false">http://lassebunk.dk/?p=1368</guid>
		<description><![CDATA[Example application to make your webcam work with Ruby on Rails.]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> <a href="https://github.com/lassebunk/webcam_app">View the complete source code</a> or <a href="http://test.lassebunk.dk/webcam/">see the app in action</a>.</p>
<p><strong>Update:</strong> Having trouble getting this to work using Ruby 1.9? Check out the solution <a href="https://github.com/lassebunk/webcam_app/issues/1">here</a>.</p>
<p><strong>Uodate:</strong> The <a href="https://github.com/lassebunk/webcam_app/">app at GitHub</a> is now rewritten for Rails 3.1.</p>
<p>Here&#8217;s how to get pictures from your webcam into Rails using the <a href="http://github.com/thoughtbot/paperclip">Paperclip plugin</a> and <a href="http://code.google.com/p/jpegcam/">jpegcam</a>. It is an update of the <a href="http://lassebunk.dk/2010/02/18/paperclip-jpegcam-webcam-rails/">Rails 2.3.x version of this post</a>.</p>
<p>What we will do is create a <code>Photo</code> model and a <code>Photos</code> controller with actions <code>new</code>, <code>upload</code> and <code>create</code> to take care of the image creation.</p>
<p>Start by creating a new Rails application:</p>
<pre class="brush: bash; title: ; notranslate">
$ rails new webcam_app
$ cd webcam_app
</pre>
<p>Create the <code>Photo</code> model:</p>
<pre class="brush: bash; title: ; notranslate">
$ rails g model Photo description:string
$ rake db:migrate
</pre>
<p>Create the <code>Photos</code> controller:</p>
<pre class="brush: bash; title: ; notranslate">
$ rails g controller Photos
</pre>
<p>Edit <code>config/routes.rb</code> to contain a photos resource:</p>
<pre class="brush: ruby; title: ; notranslate">
resources :photos, <img src='http://lassebunk.dk/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly =&gt; [:index, :show, :new, :create] do
  post 'upload', <img src='http://lassebunk.dk/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> n =&gt; :collection
end
</pre>
<p><a href="http://code.google.com/p/jpegcam/downloads/list">Download jpegcam</a> and put <code>webcam.js</code> in your <code>public/javascripts</code> folder and <code>webcam.swf</code> and <code>shutter.mp3</code> in your <code>public</code> folder.</p>
<p>Edit the layout file <code>app/views/layouts/application.html.erb</code> and insert the following HTML inside the <code>&lt;head&gt;</code>-tag just below the other <code>javascript_include_tag</code>:</p>
<pre class="brush: ruby; title: ; notranslate">
&lt;%= javascript_include_tag 'webcam' %&gt;
</pre>
<p>Next, create <code>app/views/photos/new.html.erb</code> and make a div for the webcam contents:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;webcam&quot;&gt;&lt;/div&gt;
</pre>
<p>And the actual webcam javascript:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;&gt;
  webcam.set_swf_url('/webcam.swf');
  webcam.set_api_url('&lt;%= upload_photos_path %&gt;');
  webcam.set_quality(90);
  webcam.set_shutter_sound(true, '/shutter.mp3');
  $('webcam').innerHTML = webcam.get_html(640, 480);
&lt;/script&gt;
</pre>
<p>That will run the actual webcam so you can see yourself <img src='http://lassebunk.dk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  But for taking a picture, we&#8217;ll need to add the following button:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=&quot;button&quot; value=&quot;Take picture&quot; onclick=&quot;webcam.snap();&quot; /&gt;
</pre>
<p>Now when you click the button, the webcam image will be posted to <code>/photos/upload</code>. Try it out:</p>
<pre class="brush: bash; title: ; notranslate">
$ rails s
</pre>
<p>and go to <a href="http://localhost:3000/photos/new">http://localhost:3000/photos/new</a>.</p>
<p>Let&#8217;s add some code for handling the upload.</p>
<p>The <code>webcam.swf</code> just uploads a bunch of jpeg data so we&#8217;ll need to get a hold of the raw post data. That&#8217;s done with Rails&#8217; <code>request.raw_post</code>. In your <code>PhotosController</code>:</p>
<pre class="brush: ruby; title: ; notranslate">
def upload
  File.open(upload_path, 'w') do |f|
    f.write request.raw_post
  end
  render :text =&gt; &quot;ok&quot;
end

private

def upload_path # is used in upload and create
  file_name = session[:session_id].to_s + '.jpg'
  File.join(RAILS_ROOT, 'public', 'uploads', file_name)
end
</pre>
<p>Remember to create the <code>public/uploads</code> folder.</p>
<p>Now, back to <code>app/views/photos/new.html.erb</code>. Create the following at the bottom of the view:</p>
<pre class="brush: ruby; title: ; notranslate">
&lt;% form_for Photo.new, :html =&gt; { :style =&gt; &quot;display: none;&quot; } do |f| %&gt;
  &lt;p&gt;
    &lt;%= f.label :description %&gt;&lt;br /&gt;
    &lt;%= f.text_field :description %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.submit &quot;Save the photo&quot; %&gt;
    or &lt;%= link_to &quot;Take another&quot;, new_photo_path %&gt;
  &lt;/p&gt;
&lt;% end %&gt;
</pre>
<p>In your javascript just below the webcam div, insert the following function:</p>
<pre class="brush: jscript; title: ; notranslate">
function upload_complete(msg) {
  if (msg == 'ok') {
    $('new_photo').show();
    $('photo_description').focus();
  } else {
    alert('An error occured');
    webcam.reset();
  }
}
</pre>
<p>And add the following webcam code:</p>
<pre class="brush: jscript; title: ; notranslate">
webcam.set_hook('onComplete', 'upload_complete');
</pre>
<p>Now when you take a picture you will get a new photo form for entering a description. If something goes wrong, the webcam will be reset so you can try again. This might also be a good time to check your logs.</p>
<p>Now we have the upload (see for yourself in your public/uploads folder) but we still need to add Paperclip to the model.<br />
Start by adding the plugin gem to your <code>Gemfile</code>:</p>
<pre class="brush: ruby; title: ; notranslate">
gem 'paperclip', '~&gt; 2.3'
</pre>
<p>Then run:</p>
<pre class="brush: bash; title: ; notranslate">
$ bundle install
</pre>
<p>to install the gem.</p>
<p>In the <code>Photo</code> model:</p>
<pre class="brush: ruby; title: ; notranslate">
class Photo &lt; ActiveRecord::Base
  has_attached_file :image, :styles =&gt; { :medium =&gt; &quot;300x300&gt;&quot;, :thumb =&gt; &quot;100x100&gt;&quot; }
end
</pre>
<p>For this to work, we need to add some necessary Paperclip columns to our <code>Photo</code> model:</p>
<pre class="brush: bash; title: ; notranslate">
$ rails g paperclip photo image
$ rake db:migrate
</pre>
<p>Now we can show photos like this:</p>
<pre class="brush: ruby; title: ; notranslate">
&lt;%= image_tag photo.image.url(:thumb) %&gt;
</pre>
<p>We just need to save the photos first. Add the following to your <code>PhotosController</code> somewhere above the <code>private</code> keyword:</p>
<pre class="brush: ruby; title: ; notranslate">
def create
  @photo = Photo.new(params[:photo])
  @photo.image = File.new(upload_path)
  @photo.save

  redirect_to @photo
end

def show
  @photo = Photo.find(params[:id])
end

def index
  @photos = Photo.all
end
</pre>
<p>In <code>app/views/photos/show.html.erb</code>:</p>
<pre class="brush: ruby; title: ; notranslate">
&lt;h1&gt;Photo&lt;/h1&gt;
&lt;p&gt;&lt;%= image_tag @photo.image.url(:medium) %&gt;&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;
  &lt;%=h @photo.description %&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;%= link_to &quot;Take a new picture&quot;, new_photo_path %&gt;
  | &lt;%= link_to &quot;See all pictures&quot;, photos_path %&gt;
&lt;/p&gt;
</pre>
<p>And last but not least in <code>app/views/photos/index.html.erb</code>:</p>
<pre class="brush: ruby; title: ; notranslate">
&lt;h1&gt;All photos&lt;/h1&gt;
&lt;p&gt;
  &lt;% @photos.each do |photo| %&gt;
    &lt;%= link_to image_tag(photo.image.url(:thumb)), photo %&gt;
  &lt;% end %&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;%= link_to &quot;Take a new picture&quot;, new_photo_path %&gt;
&lt;/p&gt;
</pre>
<p>Now start your <code>rails s</code> if it isn&#8217;t already, go to <a href="http://localhost:3000/photos">http://localhost:3000/photos</a> – aaannnd.. it works!</p>
<p>
<a href="http://twitter.com/lassebunk" onclick="abtest.trackGoal(21, this);" class="icon twitter">Follow me on Twitter</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://lassebunk.dk/2011/02/19/paperclip-jpegcam-webcam-rails3/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Using Paperclip and jpegcam to get pictures from your webcam into Rails</title>
		<link>http://lassebunk.dk/2010/02/18/paperclip-jpegcam-webcam-rails/</link>
		<comments>http://lassebunk.dk/2010/02/18/paperclip-jpegcam-webcam-rails/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 14:45:49 +0000</pubDate>
		<dc:creator>lassebunk</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[jpegcam]]></category>
		<category><![CDATA[Paperclip]]></category>
		<category><![CDATA[Webcam]]></category>

		<guid isPermaLink="false">http://lassebunk.dk/?p=1096</guid>
		<description><![CDATA[Example application to make your webcam work with Ruby on Rails.]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> This post is written for Rails 2.3.x. <a href="http://lassebunk.dk/2011/02/19/paperclip-jpegcam-webcam-rails3/">See Rails 3 version of this post</a>.</p>
<p>Here&#8217;s how to get pictures from your webcam into Rails using the <a href="http://github.com/thoughtbot/paperclip">Paperclip plugin</a> and <a href="http://code.google.com/p/jpegcam/">jpegcam</a>.</p>
<p>What we will do is create a <code>Photo</code> model and a <code>Photos</code> controller with actions <code>new</code>, <code>upload</code> and <code>create</code> to take care of the image creation.</p>
<p>Start by creating a new Rails application:</p>
<pre class="brush: bash; title: ; notranslate">
$ rails webcam_app
$ cd webcam_app
</pre>
<p>Create the <code>Photo</code> model:</p>
<pre class="brush: bash; title: ; notranslate">
$ script/generate model Photo description:string
$ rake db:migrate
</pre>
<p>Create the <code>Photos</code> controller:</p>
<pre class="brush: bash; title: ; notranslate">
$ script/generate controller Photos
</pre>
<p>Edit <code>config/routes.rb</code> to contain a photos resource:</p>
<pre class="brush: ruby; title: ; notranslate">
map.resources :photos, <img src='http://lassebunk.dk/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly =&gt; [:index, :show, :new, :create], :new =&gt; { :upload =&gt; :post }
</pre>
<p><a href="http://code.google.com/p/jpegcam/downloads/list">Download jpegcam</a> and put <code>webcam.js</code> in your <code>public/javascripts</code> folder and <code>webcam.swf</code> and <code>shutter.mp3</code> in your <code>public</code> folder.</p>
<p>Create the layout file <code>app/views/layouts/application.html.erb</code> and insert the following HTML:</p>
<pre class="brush: ruby; title: ; notranslate">
&lt;html&gt;
  &lt;head&gt;
    &lt;%= javascript_include_tag :defaults %&gt;
    &lt;%= javascript_include_tag 'webcam' %&gt;
  &lt;/head&gt;
  &lt;body&gt;
	  &lt;%= yield %&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Next, create <code>app/views/photos/new.html.erb</code> and make a div for the webcam contents:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;webcam&quot;&gt;&lt;/div&gt;
</pre>
<p>And the actual webcam javascript:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;&gt;
  webcam.set_swf_url('/webcam.swf');
  webcam.set_api_url('&lt;%= upload_new_photo_path %&gt;');
  webcam.set_quality(90);
  webcam.set_shutter_sound(true, '/shutter.mp3');
  $('webcam').innerHTML = webcam.get_html(640, 480);
&lt;/script&gt;
</pre>
<p>That will run the actual webcam so you can see yourself <img src='http://lassebunk.dk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  But for taking a picture, we&#8217;ll need to add the following button:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=&quot;button&quot; value=&quot;Take picture&quot; onclick=&quot;webcam.snap();&quot; /&gt;
</pre>
<p>Now when you click the button, the webcam image will be posted to <code>/photos/new/upload</code>. Try it out:</p>
<pre class="brush: bash; title: ; notranslate">
$ script/server
</pre>
<p>and go to <a href="http://localhost:3000/photos/new">http://localhost:3000/photos/new</a>.</p>
<p>Let&#8217;s add some code for handling the upload.</p>
<p>The <code>webcam.swf</code> just uploads a bunch of jpeg data so we&#8217;ll need to get a hold of the raw post data. That&#8217;s done with Rails&#8217; <code>request.raw_post</code>. In your <code>PhotosController</code>:</p>
<pre class="brush: ruby; title: ; notranslate">
def upload
  File.open(upload_path, 'w') do |f|
    f.write request.raw_post
  end
  render :text =&gt; &quot;ok&quot;
end

private

def upload_path # is used in upload and create
  file_name = session[:session_id].to_s + '.jpg'
  File.join(RAILS_ROOT, 'public', 'uploads', file_name)
end
</pre>
<p>Remember to create the <code>public/uploads</code> folder.</p>
<p>Now, back to <code>app/views/photos/new.html.erb</code>. Create the following at the bottom of the view:</p>
<pre class="brush: ruby; title: ; notranslate">
&lt;% form_for Photo.new, :html =&gt; { :style =&gt; &quot;display: none;&quot; } do |f| %&gt;
  &lt;p&gt;
    &lt;%= f.label :description %&gt;&lt;br /&gt;
    &lt;%= f.text_field :description %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.submit &quot;Save the photo&quot; %&gt;
    or &lt;%= link_to &quot;Take another&quot;, new_photo_path %&gt;
  &lt;/p&gt;
&lt;% end %&gt;
</pre>
<p>In your javascript just below the webcam div, insert the following function:</p>
<pre class="brush: jscript; title: ; notranslate">
function upload_complete(msg) {
  if (msg == 'ok') {
    $('new_photo').show();
    $('photo_description').focus();
  } else {
    alert('An error occured');
    webcam.reset();
  }
}
</pre>
<p>And add the following webcam code:</p>
<pre class="brush: jscript; title: ; notranslate">
webcam.set_hook('onComplete', 'upload_complete');
</pre>
<p>Now when you take a picture you will get a new photo form for entering a description. If something goes wrong, the webcam will be reset so you can try again. This might also be a good time to check your logs.</p>
<p>Now we have the upload (see for yourself in your public/uploads folder) but we still need to add Paperclip to the model.<br />
Start by installing the plugin:</p>
<pre class="brush: bash; title: ; notranslate">
$ script/plugin install git://github.com/thoughtbot/paperclip.git
</pre>
<p>In the <code>Photo</code> model:</p>
<pre class="brush: ruby; title: ; notranslate">
class Photo &lt; ActiveRecord::Base
  has_attached_file :image, :styles =&gt; { :medium =&gt; &quot;300x300&gt;&quot;, :thumb =&gt; &quot;100x100&gt;&quot; }
end
</pre>
<p>For this to work, we need to add some necessary Paperclip columns to our <code>Photo</code> model:</p>
<pre class="brush: bash; title: ; notranslate">
$ script/generate paperclip photo image
$ rake db:migrate
</pre>
<p>Now we can show photos like this:</p>
<pre class="brush: ruby; title: ; notranslate">
&lt;%= image_tag photo.image.url(:thumb) %&gt;
</pre>
<p>We just need to save the photos first. Add the following to your <code>PhotosController</code> somewhere above the <code>private</code> keyword:</p>
<pre class="brush: ruby; title: ; notranslate">
def create
  @photo = Photo.new(params[:photo])
  @photo.image = File.new(upload_path)
  @photo.save

  redirect_to @photo
end

def show
  @photo = Photo.find(params[:id])
end

def index
  @photos = Photo.all
end
</pre>
<p>In <code>app/views/photos/show.html.erb</code>:</p>
<pre class="brush: ruby; title: ; notranslate">
&lt;h1&gt;Photo&lt;/h1&gt;
&lt;p&gt;&lt;%= image_tag @photo.image.url(:medium) %&gt;&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;Description:&lt;/strong&gt;&lt;br /&gt;
  &lt;%=h @photo.description %&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;%= link_to &quot;Take a new picture&quot;, new_photo_path %&gt;
  | &lt;%= link_to &quot;See all pictures&quot;, photos_path %&gt;
&lt;/p&gt;
</pre>
<p>And last but not least in <code>app/views/photos/index.html.erb</code>:</p>
<pre class="brush: ruby; title: ; notranslate">
&lt;h1&gt;All photos&lt;/h1&gt;
&lt;p&gt;
  &lt;% @photos.each do |photo| %&gt;
    &lt;%= link_to image_tag(photo.image.url(:thumb)), photo %&gt;
  &lt;% end %&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;%= link_to &quot;Take a new picture&quot;, new_photo_path %&gt;
&lt;/p&gt;
</pre>
<p>Now start your <code>script/server</code> if it isn&#8217;t already, go to <a href="http://localhost:3000/photos">http://localhost:3000/photos</a> – aaannnd.. it works!</p>
<p>
<a href="http://twitter.com/lassebunk" onclick="abtest.trackGoal(21, this);" class="icon twitter">Follow me on Twitter</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://lassebunk.dk/2010/02/18/paperclip-jpegcam-webcam-rails/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

