Use the GPhoto2 API to control cameras
masterTo interact with cameras, instantiate GPhoto2 from the gphoto2 module. You can manage logging, list available cameras, and perform actions like taking pictures or downloading files.
Logging
Use setLogLevel(level) to enable logging (levels 0-4). You can listen to the log event to capture output.
Camera Operations
After calling list(), you receive an array of camera objects. Common operations on a camera object include:
getConfig(callback): Retrieves the configuration tree.setConfigValue(key, value, callback): Sets a specific configuration value.takePicture(options, callback): Captures an image. Options includedownload,keep,preview, andtargetPath.downloadPicture(options, callback): Downloads an existing image from the camera usingcameraPathandtargetPath.
Note: The following example requires asynchronous control flow logic to be implemented in a real application.
var fs = require('fs');
var gphoto2 = require('gphoto2');
var GPhoto = new gphoto2.GPhoto2();
// Configure logging
GPhoto.setLogLevel(1);
GPhoto.on('log', function (level, domain, message) {
console.log(domain, message);
});
// List cameras
GPhoto.list(function (list) {
if (list.length === 0) return;
var camera = list[0];
console.log('Found', camera.model);
// Get configuration
camera.getConfig(function (er, settings) {
console.log(settings);
});
// Set configuration
camera.setConfigValue('capturetarget', 1, function (er) {
// handle error/success
});
// Take picture and download to buffer
camera.takePicture({download: true}, function (er, data) {
fs.writeFileSync(__dirname + '/picture.jpg', data);
});
// Take picture and download to specific filesystem path
camera.takePicture({
targetPath: '/tmp/foo.XXXXXX'
}, function (er, tmpname) {
fs.renameSync(tmpname, __dirname + '/picture.jpg');
});
// Download an existing picture
camera.downloadPicture({
cameraPath: '/store_00020001/DCIM/100CANON/IMG_1231.JPG',
targetPath: '/tmp/foo.XXXXXX'
}, function (er, tmpname) {
fs.renameSync(tmpname, __dirname + '/picture.jpg');
});
});