### Control GPhoto2 Cameras with Node.js
Source: https://github.com/lwille/node-gphoto2/blob/master/README.md
This comprehensive JavaScript example demonstrates how to initialize and interact with cameras using the `node-gphoto2` library. It covers setting log levels, listing connected cameras, retrieving and modifying camera configurations, and various methods for taking and downloading pictures, including options for immediate download, keeping images on the camera, and saving to specific file paths. It also shows how to download existing pictures and get live previews.
```javascript
var fs = require('fs');
var gphoto2 = require('gphoto2');
var GPhoto = new gphoto2.GPhoto2();
// Negative value or undefined will disable logging, levels 0-4 enable it.
GPhoto.setLogLevel(1);
GPhoto.on('log', function (level, domain, message) {
console.log(domain, message);
});
// List cameras / assign list item to variable to use below options
GPhoto.list(function (list) {
if (list.length === 0) return;
var camera = list[0];
console.log('Found', camera.model);
// get configuration tree
camera.getConfig(function (er, settings) {
console.log(settings);
});
// Set configuration values
camera.setConfigValue('capturetarget', 1, function (er) {
//...
});
// Take picture with camera object obtained from list()
camera.takePicture({download: true}, function (er, data) {
fs.writeFileSync(__dirname + '/picture.jpg', data);
});
// Take picture and keep image on camera
camera.takePicture({
download: true,
keep: true
}, function (er, data) {
fs.writeFileSync(__dirname + '/picture.jpg', data);
});
// Take picture without downloading immediately
camera.takePicture({download: false}, function (er, path) {
console.log(path);
});
// Take picture and download it to filesystem
camera.takePicture({
targetPath: '/tmp/foo.XXXXXX'
}, function (er, tmpname) {
fs.renameSync(tmpname, __dirname + '/picture.jpg');
});
// Download a picture from camera
camera.downloadPicture({
cameraPath: '/store_00020001/DCIM/100CANON/IMG_1231.JPG',
targetPath: '/tmp/foo.XXXXXX'
}, function (er, tmpname) {
fs.renameSync(tmpname, __dirname + '/picture.jpg');
});
// Get preview picture (from AF Sensor, fails silently if unsupported)
camera.takePicture({
preview: true,
targetPath: '/tmp/foo.XXXXXX'
}, function (er, tmpname) {
fs.renameSync(tmpname, __dirname + '/picture.jpg');
});
});
```
--------------------------------
### Install node-gphoto2 via npm
Source: https://github.com/lwille/node-gphoto2/blob/master/README.md
This command installs the `gphoto2` Node.js package using npm. Before running, ensure all prerequisites, such as Node.js and `libgphoto2`, are properly installed to avoid common link errors during the build process.
```bash
npm install gphoto2
```
--------------------------------
### Start Live Camera Preview (CoffeeScript)
Source: https://github.com/lwille/node-gphoto2/blob/master/examples/public/index.html
Implements the `startPreview` method, which continuously fetches images from the '/preview' endpoint and displays them on an HTML canvas. It uses a cache-busting mechanism with `Date.parse`.
```CoffeeScript
startPreview: ()->
canvas = document.getElementById "preview"
context = canvas.getContext("2d")
image = new Image()
onload = ()->
context.drawImage(image, 0, 0, canvas.width, canvas.height)
image.src = '/preview'+Date.parse(new Date())
image.src = '/preview'
$(image).bind 'load', onload
onload()
```
--------------------------------
### Initialize UI and Display Camera Controls (CoffeeScript)
Source: https://github.com/lwille/node-gphoto2/blob/master/examples/public/index.html
The `displaySettings` method initializes the `dat.GUI` panel. It adds buttons for 'Start live preview' and 'Take picture', handling the corresponding actions. Finally, it calls `loadSettings` and `enumSettings` to populate the GUI with current camera settings.
```CoffeeScript
displaySettings: ()=>
@gui = new dat.GUI()
unless @gui
foo = 'Start live preview' : ()=> @startPreview()
'Take picture': ()=> request.get('takePicture', download:false).then (res)=>
console.log res if res.text
$('').attr('href', res.text).text(res.text).appendTo($('#downloads')).css('display':'block')
@gui.add foo, 'Take picture'
@gui.add foo, 'Start live preview'
@loadSettings @enumSettings
window.gphoto = new GPhoto()
window.gphoto.displaySettings()
```
--------------------------------
### Load Camera Settings from Server (CoffeeScript)
Source: https://github.com/lwille/node-gphoto2/blob/master/examples/public/index.html
Defines the `loadSettings` method, which makes an HTTP GET request to the '/settings' endpoint to retrieve camera configuration. It parses the JSON response and passes it to a provided callback function.
```CoffeeScript
loadSettings: (cb)=> request.get('/settings').then (settings)=> cb JSON.parse(settings.text) if cb
```
--------------------------------
### GPhoto Class Definition (CoffeeScript)
Source: https://github.com/lwille/node-gphoto2/blob/master/examples/public/index.html
Defines the main GPhoto class, which encapsulates all camera interaction logic. It uses `superagent` for HTTP requests and `dat.GUI` for dynamic UI generation.
```CoffeeScript
class GPhoto
startPreview: ()->
canvas = document.getElementById "preview"
context = canvas.getContext("2d")
image = new Image()
onload = ()->
context.drawImage(image, 0, 0, canvas.width, canvas.height)
image.src = '/preview'+Date.parse(new Date())
image.src = '/preview'
$(image).bind 'load', onload
onload()
stopPreview: ()->
$('#img').unbind('load')
loadSettings: (cb)=> request.get('/settings').then (settings)=> cb JSON.parse(settings.text) if cb
enumSettings: (settings, gui)=>
unless gui
gui = @gui
$.each settings, (key, val)=>
if val.type in ['window', 'section']
@enumSettings val.children, gui.addFolder val.label
else
foo ={}
foo[val.label] = val.value
changeFn = (newValue)->
console.log val.label, "changed to", newValue
request.put("/settings/#{key}", newValue:newValue).then (response)-> console.log response
if val.type is 'string'
gui.add(foo, val.label).onChange changeFn
else if val.type is 'toggle'
foo[val.label] = val.value != 0
gui.add(foo, val.label).onChange (newValue)-> request.put("/settings/#{key}", newValue: (if newValue then 1 else 0)).then (response)-> console.log response
else if val.type is 'choice'
gui.add(foo, val.label, val.choices).onChange(changeFn)
else if val.type is 'range'
gui.add(foo, val.label, val.min, val.max, val.step).onChange(changeFn)
displaySettings: ()=>
@gui = new dat.GUI()
unless @gui
foo = 'Start live preview' : ()=> @startPreview()
'Take picture': ()=> request.get('takePicture', download:false).then (res)=>
console.log res if res.text
$('').attr('href', res.text).text(res.text).appendTo($('#downloads')).css('display':'block')
@gui.add foo, 'Take picture'
@gui.add foo, 'Start live preview'
@loadSettings @enumSettings
window.gphoto = new GPhoto()
window.gphoto.displaySettings()
```
--------------------------------
### Enumerate and Display Camera Settings with dat.GUI (CoffeeScript)
Source: https://github.com/lwille/node-gphoto2/blob/master/examples/public/index.html
The `enumSettings` method recursively processes camera settings. It dynamically creates appropriate UI controls (string input, toggle, choice dropdown, range slider) using `dat.GUI` based on the setting's type. Changes made in the UI trigger an HTTP PUT request to update the setting on the server.
```CoffeeScript
enumSettings: (settings, gui)=>
unless gui
gui = @gui
$.each settings, (key, val)=>
if val.type in ['window', 'section']
@enumSettings val.children, gui.addFolder val.label
else
foo ={}
foo[val.label] = val.value
changeFn = (newValue)->
console.log val.label, "changed to", newValue
request.put("/settings/#{key}", newValue:newValue).then (response)-> console.log response
if val.type is 'string'
gui.add(foo, val.label).onChange changeFn
else if val.type is 'toggle'
foo[val.label] = val.value != 0
gui.add(foo, val.label).onChange (newValue)-> request.put("/settings/#{key}", newValue: (if newValue then 1 else 0)).then (response)-> console.log response
else if val.type is 'choice'
gui.add(foo, val.label, val.choices).onChange(changeFn)
else if val.type is 'range'
gui.add(foo, val.label, val.min, val.max, val.step).onChange(changeFn)
```
--------------------------------
### Stop Live Camera Preview (CoffeeScript)
Source: https://github.com/lwille/node-gphoto2/blob/master/examples/public/index.html
Provides the `stopPreview` method to halt the live camera feed by unbinding the 'load' event listener from the image element used for the preview.
```CoffeeScript
stopPreview: ()->
$('#img').unbind('load')
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.