active_scaffold_camera 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8d9e65eb25c68c2a40b390368d9c194e011bde9e
4
+ data.tar.gz: c6948ba2e3ea4ba8d0e5376538a595aeebd580b5
5
+ SHA512:
6
+ metadata.gz: 56376ee5af7e578c3955e4c450f594a95e66534df5672789098e0bfb0ebc46ceb6e07aa167d318e21013241ac809fff2cc5791661512069d6a195248459e94a2
7
+ data.tar.gz: a65384dbbb0144c55a924ec8c83a6b1d18d6b237dd19b1bfbe1177126ac244b8dcc2e5e66d6929771e873c09294568b2f92cddb765ecb819139585da63234746
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 activescaffold
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,60 @@
1
+ h1. Camera for ActiveScaffold
2
+
3
+ Currently, this plugin is compatible with ActiveScaffold >= 3.4 gem.
4
+
5
+ h2. Overview
6
+
7
+ This plugin adds a camera viewer to take a snapshot from camera on ActiveScaffold forms. It uses "SayCheese.js":https://github.com/leemachin/say-cheese
8
+
9
+ h2. Installation
10
+
11
+ You'll need at least ActiveScaffold 3.4 to use this
12
+
13
+ <pre>
14
+ gem install active_scaffold_camera
15
+ </pre>
16
+
17
+ h2. Usage
18
+
19
+ h4. Step 1
20
+
21
+ Set column to use :snapshot form_ui
22
+
23
+ h4. Step 2
24
+
25
+ Setup snapshot using options method on column
26
+
27
+ <pre>
28
+ # app/controllers/visitors_controller.rb
29
+
30
+ class VisitorsController < ApplicationController
31
+ active_scaffold :visitor do |config|
32
+ config.columns[:signature].form_ui = :signaturepad
33
+ config.columns[:signature].options = {:source => -1}
34
+ end
35
+ end
36
+ </pre>
37
+
38
+ Available options: video_not_supported, audio_not_supported, media_forbidden, source
39
+
40
+ Source can be set to use a specific source instead of displaying source selector. It must be the source index, and it can be negative index to start from end. If no device on that index, first device will be used.
41
+
42
+ If video_not_supported, audio_not_supported or media_forbidden are symbols they will be translated (using as_ method, so it must be on active_scaffold namespace). They have a default translation.
43
+
44
+ h4. Save image
45
+
46
+ Snapshot form_ui sends a data url, encoded on base64, of the image, so it should be saved on column of blob type.
47
+
48
+ h2. Support
49
+
50
+ If you have issues installing the plugin, search / post to the "Active Scaffold":http://groups.google.com/group/activescaffold forum or "Create an issue":http://github.com/activescaffold/active_scaffold_camera/issues
51
+
52
+ h2. Contributing
53
+
54
+ Fork, hack, push, and request a pull:
55
+
56
+ http://github.com/activescaffold/active_scaffold_camera/
57
+
58
+ h2. License
59
+
60
+ Released under the MIT license (included).
@@ -0,0 +1,102 @@
1
+ //= require say-cheese
2
+ //= require_self
3
+
4
+ ActiveScaffold.snapshot = function(selector_or_elements, parent) {
5
+ function videoConstraint(video) {
6
+ return typeof(video) == 'string' ? {optional: [{sourceId: video}]} : video;
7
+ }
8
+
9
+ function audioConstraint(audio) {
10
+ return typeof(audio) == 'string' ? {optional: [{sourceId: audio}]} : audio;
11
+ }
12
+
13
+ function startVideo(element, video, audio, video_select) {
14
+ var sayCheese = new SayCheese(element, {snapshots: true, video: videoConstraint(video), audio: audioConstraint(audio)});
15
+
16
+ if (video_select) {
17
+ video_select.on('change', function() {
18
+ sayCheese.stop();
19
+ sayCheese.video.remove();
20
+ $('.snapshots', sayCheese.element).remove();
21
+ $('input', sayCheese.element).val('');
22
+ sayCheese.options.video = videoConstraint($(this).val());
23
+ sayCheese.start();
24
+ });
25
+ }
26
+
27
+ sayCheese.on('start', function() {
28
+ $(this.video).data('saycheese', this);
29
+ $(this.video).after($('<div>').addClass('snapshots'));
30
+ this.video.play();
31
+ });
32
+ sayCheese.on('error', function(error) {
33
+ var $alert = $('<div>');
34
+ $alert.addClass('message error-message').css('margin-top', '20px');
35
+
36
+ if (error === 'NOT_SUPPORTED') {
37
+ $alert.html($(this.element).data('video-not-supported'));
38
+ } else if (error === 'AUDIO_NOT_SUPPORTED') {
39
+ $alert.html($(this.element).data('audio-not-supported'));
40
+ } else {
41
+ $alert.html($(this.element).data('media-forbidden'));
42
+ }
43
+
44
+ $(this.element).prepend($alert);
45
+ });
46
+ sayCheese.on('snapshot', function(snapshot) {
47
+ var img = document.createElement('img'), container = $(this.video).parent(), input = $('input', this.element);
48
+ $(img).on('load', function() { $('.snapshots', container).html(img); });
49
+ img.src = snapshot.toDataURL('image/png');
50
+ input.val(img.src);
51
+ });
52
+
53
+ sayCheese.start();
54
+ }
55
+
56
+ var elements;
57
+ if (typeof(selector_or_elements) == 'string') elements = jQuery(selector_or_elements, parent);
58
+ else elements = jQuery(selector_or_elements);
59
+
60
+ elements.each(function() {
61
+ var $element = $(this), selector = '#' + $element.attr('id');
62
+ MediaStreamTrack.getSources(function(sourceInfos) {
63
+ var sources = [];
64
+ for(i in sourceInfos) {
65
+ var source = sourceInfos[i];
66
+ if (source.kind == 'video') {
67
+ sources.push(source);
68
+ }
69
+ }
70
+ var video, source, select, index = $element.data('source');
71
+ if (index != null) {
72
+ if (index < 0) source = sources[sources.length + index];
73
+ else source = sources[index];
74
+ if (source) video = source.id;
75
+ } else if (sources.length > 1) {
76
+ var select = $('<select>');
77
+ for (i in sources) {
78
+ source = sources[i];
79
+ select.append($('<option>').val(source.id).html(source.label || 'camera ' + (i + 1)));
80
+ }
81
+ $element.prepend(select);
82
+ }
83
+ startVideo(selector, video || true, false, select);
84
+ });
85
+ });
86
+ };
87
+
88
+ jQuery(document).ready(function($) {
89
+ $(document).on('click', '.active-scaffold .snapshot video', function() {
90
+ $(this).data('saycheese').takeSnapshot();
91
+ });
92
+ $(document).on('as:action_success', 'a.as_action', function(e, action_link) {
93
+ var elements = $('.snapshot-input', action_link.adapter);
94
+ if (elements.length) ActiveScaffold.snapshot(elements);
95
+ });
96
+ $(document).on('as:element_updated', function(e) {
97
+ var elements = $('.snapshot-input', e.target);
98
+ if (elements.length) ActiveScaffold.snapshot(elements);
99
+ });
100
+ var elements = $('.snapshot-input');
101
+ if (elements.length) ActiveScaffold.snapshot(elements);
102
+ });
@@ -0,0 +1,187 @@
1
+ /*
2
+ * Say Cheese!
3
+ * Lee Machin, 2012
4
+ * http://leemach.in, http://new-bamboo.co.uk
5
+ *
6
+ * Minimal javascript library for integrating a webcam and snapshots into your app.
7
+ *
8
+ * Handles starting up the webcam and rendering the element, and also capturing shots
9
+ * in a separate canvas element.
10
+ *
11
+ * Depends on video and canvas, and of course, getUserMedia. It's unlikely to work
12
+ * on anything but the newest browsers.
13
+ */
14
+
15
+ var SayCheese = (function() {
16
+
17
+ var SayCheese;
18
+
19
+ navigator.getUserMedia = (navigator.getUserMedia ||
20
+ navigator.webkitGetUserMedia ||
21
+ navigator.mozGetUserMedia ||
22
+ navigator.msGetUserMedia ||
23
+ false);
24
+
25
+ window.AudioContext = (window.AudioContext ||
26
+ window.webkitAudioContext);
27
+
28
+ window.URL = (window.URL ||
29
+ window.webkitURL);
30
+
31
+ SayCheese = function SayCheese(element, options) {
32
+ this.snapshots = [],
33
+ this.video = null,
34
+ this.events = {},
35
+ this.stream = null,
36
+ this.options = {
37
+ video: true,
38
+ snapshots: true,
39
+ audio: false
40
+ };
41
+
42
+ this.setOptions(options);
43
+ this.element = document.querySelector(element);
44
+ return this;
45
+ };
46
+
47
+ SayCheese.prototype.on = function on(evt, handler) {
48
+ if (this.events.hasOwnProperty(evt) === false) {
49
+ this.events[evt] = [];
50
+ }
51
+
52
+ this.events[evt].push(handler)
53
+ };
54
+
55
+ SayCheese.prototype.off = function off(evt, handler) {
56
+ this.events[evt] = this.events[evt].filter(function(h) {
57
+ return h !== handler;
58
+ });
59
+ };
60
+
61
+ SayCheese.prototype.trigger = function trigger(evt, data) {
62
+ if (this.events.hasOwnProperty(evt) === false) {
63
+ return false;
64
+ }
65
+
66
+ this.events[evt].forEach(function(handler) {
67
+ handler.call(this, data);
68
+ }.bind(this));
69
+ };
70
+
71
+ SayCheese.prototype.setOptions = function setOptions(options) {
72
+ // just use naïve, shallow cloning
73
+ for (var opt in options) {
74
+ this.options[opt] = options[opt];
75
+ }
76
+ }
77
+
78
+ SayCheese.prototype.getStreamUrl = function getStreamUrl() {
79
+ if (window.URL && window.URL.createObjectURL) {
80
+ return window.URL.createObjectURL(this.stream);
81
+ } else {
82
+ return this.stream;
83
+ }
84
+ };
85
+
86
+ SayCheese.prototype.createVideo = function createVideo() {
87
+ var width = 320,
88
+ height = 0,
89
+ streaming = false;
90
+
91
+ this.video = document.createElement('video');
92
+
93
+ this.video.addEventListener('canplay', function() {
94
+ if (!streaming) {
95
+ height = this.video.videoHeight / (this.video.videoWidth / width);
96
+ this.video.style.width = width;
97
+ this.video.style.height = height;
98
+ streaming = true;
99
+ return this.trigger('start');
100
+ }
101
+ }.bind(this), false);
102
+ };
103
+
104
+ SayCheese.prototype.linkAudio = function linkAudio() {
105
+ this.audioCtx = new window.AudioContext();
106
+ this.audioStream = this.audioCtx.createMediaStreamSource(this.stream);
107
+
108
+ var biquadFilter = this.audioCtx.createBiquadFilter();
109
+
110
+ this.audioStream.connect(biquadFilter);
111
+ biquadFilter.connect(this.audioCtx.destination);
112
+ };
113
+
114
+ SayCheese.prototype.takeSnapshot = function takeSnapshot(width, height) {
115
+ if (this.options.snapshots === false) {
116
+ return false;
117
+ }
118
+
119
+ width = width || this.video.videoWidth;
120
+ height = height || this.video.videoHeight;
121
+
122
+ var snapshot = document.createElement('canvas'),
123
+ ctx = snapshot.getContext('2d');
124
+
125
+ snapshot.width = width;
126
+ snapshot.height = height;
127
+
128
+ ctx.drawImage(this.video, 0, 0, width, height);
129
+
130
+ this.snapshots.push(snapshot);
131
+ this.trigger('snapshot', snapshot);
132
+
133
+ ctx = null;
134
+ };
135
+
136
+ /* Start up the stream, if possible */
137
+ SayCheese.prototype.start = function start() {
138
+
139
+ // fail fast and softly if browser not supported
140
+ if (navigator.getUserMedia === false) {
141
+ this.trigger('error', 'NOT_SUPPORTED');
142
+ return false;
143
+ }
144
+
145
+ var success = function success(stream) {
146
+ this.stream = stream;
147
+ this.createVideo();
148
+
149
+ if (navigator.mozGetUserMedia) {
150
+ this.video.mozSrcObject = stream;
151
+ } else {
152
+ this.video.src = this.getStreamUrl();
153
+ }
154
+
155
+ if (this.options.audio === true) {
156
+ try {
157
+ this.linkAudio();
158
+ } catch(e) {
159
+ this.trigger('error', 'AUDIO_NOT_SUPPORTED');
160
+ }
161
+ }
162
+
163
+ this.element.appendChild(this.video);
164
+ this.video.play();
165
+ }.bind(this);
166
+
167
+ /* error is also called when someone denies access */
168
+ var error = function error(error) {
169
+ this.trigger('error', error);
170
+ }.bind(this);
171
+
172
+ return navigator.getUserMedia({ video: this.options.video, audio: this.options.audio }, success, error);
173
+ };
174
+
175
+ SayCheese.prototype.stop = function stop() {
176
+ this.stream.stop();
177
+
178
+ if (window.URL && window.URL.revokeObjectURL) {
179
+ window.URL.revokeObjectURL(this.video.src);
180
+ }
181
+
182
+ return this.trigger('stop');
183
+ };
184
+
185
+ return SayCheese;
186
+
187
+ })();
@@ -0,0 +1,9 @@
1
+ .active-scaffold .snapshot-input {
2
+ video, img { max-width: 300px; }
3
+ video, .snapshots { float: left; }
4
+ .snapshots { margin-left: 10px; }
5
+ select {
6
+ display: block;
7
+ margin-bottom: 10px;
8
+ }
9
+ }
@@ -0,0 +1,5 @@
1
+ en:
2
+ active_scaffold:
3
+ video_not_supported: "<strong>:(</strong> your browser doesn't support video yet!"
4
+ audio_not_supported: "<strong>:(</strong> your browser doesn't support audio yet!"
5
+ media_forbidden: "<strong>:(</strong> you have to click 'allow' to use media"
@@ -0,0 +1,5 @@
1
+ es:
2
+ active_scaffold:
3
+ video_not_supported: "<strong>:(</strong> ¡tu navegador no soporta video!"
4
+ audio_not_supported: "<strong>:(</strong> ¡tu navegador no soporta audio!"
5
+ media_forbidden: "<strong>:(</strong> tienes que pulsar 'permitir' para usar la cámara"
@@ -0,0 +1,12 @@
1
+ require "active_scaffold_camera/engine.rb"
2
+
3
+ module ActiveScaffoldCamera
4
+ def self.root
5
+ File.dirname(__FILE__) + "/.."
6
+ end
7
+ autoload 'ViewHelpers', 'active_scaffold_camera/view_helpers.rb'
8
+ end
9
+
10
+ ActionView::Base.send :include, ActiveScaffoldCamera::ViewHelpers
11
+ ActiveScaffold.stylesheets << 'active_scaffold_camera'
12
+ ActiveScaffold.javascripts << 'active_scaffold_camera'
@@ -0,0 +1,4 @@
1
+ module ActiveScaffoldCamera
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveScaffoldCamera
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 1
6
+
7
+ STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ module ActiveScaffoldCamera
2
+ module ViewHelpers
3
+ I18N_ATTRIBUTES = [:video_not_supported, :audio_not_supported, :media_forbidden]
4
+ def snapshot_attributes(column)
5
+ attributes = Hash[I18N_ATTRIBUTES.map{ |attr| [attr, column.options[attr] || attr] }]
6
+ attributes[:source] = column.options[:source] if column.options[:source]
7
+ I18N_ATTRIBUTES.each { |attr| attributes[attr] = as_(attributes[attr]) if attributes[attr].is_a? Symbol }
8
+ attributes
9
+ end
10
+
11
+ def active_scaffold_input_snapshot(column, html_options)
12
+ content_tag :div, :class => "snapshot-input #{html_options[:class]}", :id => html_options[:id], :data => snapshot_attributes(column) do
13
+ hidden_field :record, column.name, :name => html_options[:name]
14
+ end
15
+ end
16
+
17
+ def active_scaffold_column_snapshot(record, column)
18
+ value = record.send(column.name)
19
+ tag :img, :src => value if value
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_scaffold_camera
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergio Cambra
8
+ - Volker Hochstein
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-12-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoulda
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '1.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '1.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: active_scaffold
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: 3.4.0
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: 3.4.0
56
+ description: Helper to take a snapshot from camera on ActiveScaffold forms, using
57
+ saycheese.js
58
+ email: activescaffold@googlegroups.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.textile
64
+ files:
65
+ - LICENSE
66
+ - README.textile
67
+ - app/assets/javascripts/active_scaffold_camera.js
68
+ - app/assets/javascripts/say-cheese.js
69
+ - app/assets/stylesheets/active_scaffold_camera.scss
70
+ - config/locales/en.yml
71
+ - config/locales/es.yml
72
+ - lib/active_scaffold_camera.rb
73
+ - lib/active_scaffold_camera/engine.rb
74
+ - lib/active_scaffold_camera/version.rb
75
+ - lib/active_scaffold_camera/view_helpers.rb
76
+ homepage: http://github.com/activescaffold/active_scaffold_camera
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.5
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Snapshot form_ui using saycheese.js
100
+ test_files: []