node-gphoto2

repository·master·Indexed 43 days ago

https://github.com/lwille/node-gphoto2

A Node.js wrapper for libgphoto2 version ~2.5.x, providing remote control capabilities for DSLRs and other digital cameras. It includes the GPhoto2 and Camera classes for managing camera connections, retrieving configuration, taking pictures, and downloading files via an asynchronous API.

Tokens
2.4K
Snippets
9
Records
11
Agent score
54%

What's inside node-gphoto2

  1. Use the GPhoto2 API to control cameras

    master

    To 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 include download, keep, preview, and targetPath.
    • downloadPicture(options, callback): Downloads an existing image from the camera using cameraPath and targetPath.

    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');
      });
    });
  2. Install node-gphoto2

    master

    Before installing the Node.js package, you must ensure the system dependencies are installed.

    System Prerequisites

    • Node.js: versions 10 (LTS), 12 (LTS), or 13 (current).
    • libgphoto2: version ~2.5.x. Install via brew install libgphoto2 (macOS), apt-get install libgphoto2-dev (Debian/Ubuntu), or build from source.
    • pkg-config or dpkg: used for dependency checking.
    • clang compiler.

    Installation

    Once the system dependencies are ready, install the package via npm:

    npm install gphoto2

    Note: If installation fails, check the output for link errors, which typically indicate missing system dependencies.

  3. Run the test server application

    master

    A small test application is provided that runs a web server. This allows you to change camera settings and take pictures via a web interface.

    Prerequisites for the test server:

    • express
    • jade
    • underscore

    To run the server:

    npx coffee examples/server.coffee

    The server will be available at http://localhost:1337.

  4. Use the gphoto2 module and its EventEmitter-based classes

    master

    The gphoto2 module provides bindings to the gphoto2 library. The core classes GPhoto2 and Camera inherit from Node.js EventEmitter, allowing them to emit events during camera operations.

    To use the module, require it from the package. The primary entry points are gphoto2.GPhoto2 and gphoto2.Camera.

    const gphoto2 = require('node-gphoto2');
    
    // gphoto2.GPhoto2 and gphoto2.Camera are EventEmitters
    const camera = new gphoto2.Camera();
    
    camera.on('event', (data) => {
      console.log('Camera event:', data);
    });
  5. Use the GPCamera API to control a camera

    master

    The GPCamera class provides the primary interface for interacting with a camera via gphoto2. It exposes asynchronous methods for retrieving configuration, setting configuration values, taking pictures, and downloading pictures.

    Note: Most operations are asynchronous and utilize callbacks to return results.

    // Conceptual usage based on exported symbols
    
    // Retrieve the full configuration tree
    camera.getConfig((err, config) => {
      console.log(config);
    });
    
    // Get a specific configuration value by key
    camera.getConfigValue('key_name', (err, value) => {
      console.log(value);
    });
    
    // Set a configuration value
    camera.setConfigValue('key_name', value, (err) => {
      if (err) console.error(err);
    });
    
    // Capture a picture
    camera.takePicture((err, result) => {
      console.log('Picture taken');
    });
    
    // Download a picture
    camera.downloadPicture((err, filePath) => {
      console.log('Downloaded to:', filePath);
    });
  6. Use the GPhoto2 class in Node.js

    master

    The GPhoto2 class is the primary interface for interacting with the gphoto2 library via Node.js. It provides methods for managing camera connections and inspecting available hardware.

    Key methods available on the instance include:

    • List(): Asynchronously retrieves a list of available camera ports/information.
    • SetLogLevel(level): Configures the verbosity of the library's logging.
    • openCamera(camera): Opens a connection to a specific camera instance.
    • closeCamera(camera): Closes the connection to a camera.

    Note: List() is an asynchronous operation that returns results via a callback.

    // Example conceptual usage based on the GPhoto2 class definition
    const gphoto2 = require('node-gphoto2');
    const gphoto = new gphoto2.GPhoto2();
    
    // List available cameras
    gphoto.List((err, list) => {
      if (err) throw err;
      console.log('Available cameras:', list);
    });
    
    // Set log level
    gphoto.SetLogLevel(1);
  7. Reference: GPCamera exported methods

    master

    The following methods are exposed on the GPCamera object for camera control:

    // Exported method symbols
    camera_getConfig_symbol
    camera_getConfigValue_symbol
    camera_setConfigValue_symbol
    camera_takePicture_symbol
    camera_downloadPicture_symbol
  8. Configure logging with SetLogLevel()

    master

    The SetLogLevel(level) method allows you to control the verbosity of the gphoto2 library logs. This is useful for debugging connection issues or monitoring camera state changes.

    // Set the log level
    gphoto.SetLogLevel(level);
  9. List available cameras with List()

    master

    The List() method is used to asynchronously fetch a list of available camera port information (GPPortInfoList). Because it is an asynchronous operation, it utilizes a callback pattern to return the results to the Node.js event loop.

    gphoto.List((err, list) => {
      // list contains GPPortInfoList data
    });