Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

record and snapshot

  • Loading branch information...
commit 8c9b02704fa5ca59ff7f2c30dae1815d9be3143d 1 parent f4ae710
@contra contra authored
Showing with 52 additions and 7 deletions.
  1. +14 −3 README.md
  2. +16 −3 lib/main.coffee
  3. +1 −1  package.json
  4. +21 −0 test/main.coffee
View
17 README.md
@@ -22,11 +22,13 @@ This library requires that you have OpenCV installed - go to the OpenCV site for
## Usage
-The object returned from createStream is a full readable Stream - you can pause, resume, destroy, etc.
+#### createStream([camera])
+
+The object returned from createStream is a full readable Stream - you can pause, resume, destroy, pipe, etc.
createStream optionally takes a camera number and defaults to 0 for the main camera
-Each data event is a full image buffer from the camera - the framerate is variable on your CPU (should be an option in the future). To convert the buffer to a base64 data uri (for the browser) just do ```"data:image/png;base64," + buffer.toString('base64')```
+Each data event is a full image buffer from the camera - the framerate is variable on your CPU (should be an option in the future). To convert the buffer to a base64 data uri (for the browser) just do ```"data:image/png;base64," + buffer.toString('base64')```
```coffee-script
camera = require 'camera'
@@ -37,6 +39,15 @@ webcam.on 'data', (buffer) ->
# do something with image buffer
```
+#### snapshot(stream, callback)
+
+Sugar for ```s.once 'data', cb``` - callback receives one image buffer
+
+#### record(stream, milliseconds, cb)
+
+Returns an array of video frames for the time-span specified in milliseconds
+
+
## Examples
@@ -49,7 +60,7 @@ camera = require 'camera'
webcam = camera.createStream()
webcam.on 'data', (buffer) ->
- fs.writeFileSync 'cam.png', buffer
+ fs.writeFileSync 'cam.jpg', buffer
webcam.destroy()
```
View
19 lib/main.coffee
@@ -2,7 +2,18 @@ Stream = require 'stream'
cv = require 'opencv'
module.exports =
- snapshot: (s, cb) -> s.on 'data', cb
+ snapshot: (s, cb) -> s.once 'data', cb
+
+ record: (s, ms, cb) ->
+ vid = []
+ push = (buf) -> vid.push buf
+ clear = ->
+ s.removeListener 'data', push
+ cb vid
+
+ setTimeout clear, ms
+ s.on 'data', push
+
createStream: (idx=0) ->
cam = new cv.VideoCapture idx
@@ -13,18 +24,20 @@ module.exports =
getImage = ->
cam.read (i) ->
return if s.paused or s.destroyed
+ return unless s.readable
s.emit 'data', i.toBuffer()
process.nextTick getImage
s.pause = ->
s.paused = true
- return @
+ return s
s.resume = ->
s.paused = false
getImage()
- return @
+ return s
s.destroy = ->
s.destroyed = true
+ s.readable = false
@emit 'close'
return
View
2  package.json
@@ -1,7 +1,7 @@
{
"name":"camera",
"description":"Create readable streams from connected webcams",
- "version":"0.0.1",
+ "version":"0.0.2",
"homepage":"http://github.com/wearefractal/camera",
"repository":"git://github.com/wearefractal/camera.git",
"author":"Fractal <contact@wearefractal.com> (http://wearefractal.com/)",
View
21 test/main.coffee
@@ -42,4 +42,25 @@ describe 'camera', ->
cam.resume()
cam.on 'data', ->
cam.destroy()
+ done()
+
+ describe 'snapshot()', ->
+ it 'should return a buffer', (done) ->
+ return done() if process.env.CI
+ cam = camera.createStream()
+ should.exist cam
+ camera.snapshot cam, (buf) ->
+ should.exist buf
+ cam.destroy()
+ done()
+
+ describe 'snapshot()', ->
+ it 'should return a buffer', (done) ->
+ return done() if process.env.CI
+ cam = camera.createStream()
+ should.exist cam
+ camera.record cam, 1000, (bufs) ->
+ should.exist bufs
+ console.log bufs.length
+ cam.destroy()
done()
Please sign in to comment.
Something went wrong with that request. Please try again.