C++ Linux下通过USB控制Nikon相机拍照和抓取照片

本文介绍了如何在Linux系统下,通过C++和libgphoto2库,实现对Nikon相机的USB控制,进行拍照和抓取照片的功能。首先提到了Nikon SDK仅支持Windows和MacOS,而libgphoto2作为一个开源库,能广泛支持多种相机,包括Nikon。接着详细阐述了libgphoto2的下载、编译和安装步骤,以及如何使用gphoto2命令行工具进行初步调试。最后展示了简单的代码编写,包括头文件和cpp文件的声明与实现。

C++ Linux下通过USB控制Nikon相机拍照和抓取照片

前段时间,需要在Linux下通过USB抓取Nikon相机拍摄的照片,但是Nikon相机提供的SDK只支持Windows和MacOS,所以在网上找了开源的库——libgphoto2。

libgphoto2号称支持2300中相机,包括Canon、Nikon、Sony,和各种型号的手机。


下载地址

http://www.gphoto.org/

编译和安装

libgphoto2的编译和安装过程和简单,中间提示缺什么就装上。编译的时候要注意查看依赖项,根据自己的需要选择安装(比如libusb)。

装好libgphoto2后,可以编译和安装gphoto2。gphoto2是使用libgphoto2编写的简单的命令行工具,方便调试设备和在其基础上编写自己的程序。

代码编写

头文件

头文件很简单,声明自己的函数:

#ifndef NIKONCTRL_H
#define NIKONCTRL_H


class NikonCtrl
{
   
   
public:
    NikonCtrl();

    bool Detect();
    void Trigger();
    void Download(const char* file_name);
    int Capture(const char* file_name);
};

#endif // NIKONCTRL_H

cpp文件

cpp文件根据gphoto2的代码改写:

#include "nikonctrl.h"
#include <sys/time.h>
#include <sys/unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

extern "C"  {

#include <gphoto2/gphoto2-port-info-list.h>
#include <gphoto2/gphoto2-port-log.h>
#include <gphoto2/gphoto2-setting.h>
#include <gphoto2/gphoto2-camera.h>
#include <gphoto2/gphoto2-context.h>

#include <gp-params.h>
#include "i18n.h"

}

GPParams gp_params;

#define CR(result) {int r = (result); if (r < 0) return (r);}

NikonCtrl::NikonCtrl()
{

}

void
_get_portinfo_list (GPParams *p) {
    int count, result;
    GPPortInfoList *list = NULL;

    if (p->portinfo_list)
        return;

    if (gp_port_info_list_new (&list) < GP_OK)
        return;
    result = gp_port_info_list_load (list);
    if (result < 0) {
        gp_port_info_list_free (list);
        return;
    }
    count = gp_port_info_list_count (list);
    if (count < 0) {
        gp_port_info_list_free (list);
        return;
    }
    p->portinfo_list = list;
    return;
}

int
action_camera_set_port (GPParams *params, const char *port)
{
    int p, r;
    GPPortInfo info;
    char *path;
    char verified_port[1024];

    verified_port[sizeof (verified_port) - 1] = '\0';
    if (!strchr (port, ':')) {
        gp_log (GP_LOG_DEBUG, "main", _("Ports must look like "
                                        "'serial:/dev/ttyS0' or 'usb:', but '%s' is "
                                        "missing a colon so I am going to guess what you "
                                        "mean."), port);
        if (!strcmp (port, "usb")) {
            strncpy (verified_port, "usb:",
                     sizeof (verified_port) - 1);
        } else if (strncmp (port, "/dev/", 5) == 0) {
            strncpy (verified_port, "serial:",
                     sizeof (verified_port) - 1);
            
Nikon Type0001 Module SDK Revision.3 summary Usage Control the camera. Supported camera D3(*1), D300(*2), D700, D90 (*1) D3 camera has 3type, as follows. The 3type will be the same specification,if there are no special explanations. If there is D3 description, that means all D3 camera. *D3 (Ver.1.0) *D3 FirmUp1 (Ver.1.1) *D3 FirmUp2 (Ver.2.0) (*2) D300 camera has 2type, as follows. The 2type will be the same specification, if there are no special explanations. If there is D300 description, that means all D300 camera. *D300 (Ver.1.0) *D300 FirmUp (Ver.1.1) Environment of operation [Windows] Windows 2000(SP4) Windows XP Home Edition (SP3) / Professional (SP3) Windows Vista SP1 32bit each edition (Home Basic / Home Premium / Business / Enterprise / Ultimate) * Please set Camera to PTP mode by SET UP menu (on camera body). If the camera is set to Mass Storage, you can not control it by Windows. [Macintosh] MacOS X 10.3.9 (PowerPC) MacOS X 10.4.11 (PowerPC/IntelMac) MacOS X 10.5.4 (IntelMac) (Type0001 Module is executable on MacOS X 10.3.9, but "Type0001CtrlSample(Mac)" project needs MacOS X 10.4 and later to open project file,because this project is created with Xcode 2.5.) * Please set Camera to PTP mode by SET UP menu (on camera body). If the camera is set to Mass Storage, you can not control it by Macintosh. Contents [Windows] Documents MAID3(E).pdf : Basic interface specification MAID3Type0001(E).pdf : Extended interface specification used by Type0001 Module Usage of Type0001 Module(E).pdf : Notes for using Type0001 Module Type0001 Sample Guide(E).pdf : The usage of a sample program Binary Files Win2000 Inf files : 丂Device drivers for PTP mode used by Windows 2000 Type0001.md3 : Type0001 Module for Win NkdPTPDi.dll : Driver for PTP mode used by Windows 2000 WinXP_Vista Type0001.md3 : Type0001 Module for Win NkdPTP.dll : Driver for PTP mode used by Windows XP, Vista Header Files Maid3.h : Basic header file of MAID interface Maid3d1.h : Extended header file for Type0001 Module NkTypes.h : Definitions of the types used in Maid3.h. Sample Program Type0001CtrlSample(Win) : Project for Microsoft Visual Studio .Net 2003 Project for Microsoft Visual C++ 6.0 [Macintosh] Documents MAID3(E).pdf : Basic interface specification MAID3Type0001(E).pdf : Extended interface specification used by Type0001 Module Usage of Type0001 Module(E).pdf : Notes for using Type0001 Module Type0001 Sample Guide(E).pdf : The usage of a sample program Binary Files Type0001 Module.bundle : Type0001 Module for Mac (Universal Binary) libNkPTPDriver.dylib : PTP driver for Mac (Universal Binary) Header Files Maid3.h : Basic header file of MAID interface Maid3d1.h : Extended header file for Type0001 Module NkTypes.h : Definitions of the types used in Maid3.h. Sample Program Type0001CtrlSample(Mac) : Sample program project for Xcode 2.5. (Universal Binary) Limitations This module cannot control two or more cameras.
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值