From 01acb08c7b8444ad0fc66d7395844cfad6e52d4d Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Mon, 28 Aug 2017 16:57:52 +0200 Subject: [PATCH] videoio: ximea - allow opening capture by serial number --- modules/videoio/src/cap.cpp | 6 ++++- modules/videoio/src/cap_ximea.cpp | 41 +++++++++++++++++++++++++++---- modules/videoio/src/precomp.hpp | 1 + 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/modules/videoio/src/cap.cpp b/modules/videoio/src/cap.cpp index 97c3a6447e..0f4fdb9c75 100644 --- a/modules/videoio/src/cap.cpp +++ b/modules/videoio/src/cap.cpp @@ -354,7 +354,11 @@ CV_IMPL CvCapture * cvCreateFileCaptureWithPreference (const char * filename, in TRY_OPEN(result, cvCreateFileCapture_OpenNI2 (filename)) if (apiPreference) break; #endif - +#ifdef HAVE_XIMEA + case CAP_XIAPI: + TRY_OPEN(result, cvCreateCameraCapture_XIMEA(filename)) + if (apiPreference) break; +#endif case CAP_IMAGES: TRY_OPEN(result, cvCreateFileCapture_Images (filename)) } diff --git a/modules/videoio/src/cap_ximea.cpp b/modules/videoio/src/cap_ximea.cpp index 1d1aeea754..85138c8e6c 100644 --- a/modules/videoio/src/cap_ximea.cpp +++ b/modules/videoio/src/cap_ximea.cpp @@ -18,6 +18,7 @@ public: virtual ~CvCaptureCAM_XIMEA() { close(); } virtual bool open( int index ); + bool open( const char* deviceName ); virtual void close(); virtual double getProperty(int) const; virtual bool setProperty(int, double); @@ -26,6 +27,7 @@ public: virtual int getCaptureDomain() { return CV_CAP_XIAPI; } // Return the type of the capture object: CV_CAP_VFW, etc... private: + bool _open(); void init(); void errMsg(const char* msg, int errNum) const; void resetCvImage(); @@ -51,6 +53,17 @@ CvCapture* cvCreateCameraCapture_XIMEA( int index ) return 0; } +CvCapture* cvCreateCameraCapture_XIMEA( const char* serialNumber ) +{ + CvCaptureCAM_XIMEA* capture = new CvCaptureCAM_XIMEA; + + if( capture->open( serialNumber )) + return capture; + + delete capture; + return 0; +} + /**********************************************************************************/ // Enumerate connected devices void CvCaptureCAM_XIMEA::init() @@ -75,13 +88,10 @@ void CvCaptureCAM_XIMEA::init() // Initialize camera input bool CvCaptureCAM_XIMEA::open( int wIndex ) { -#define HandleXiResult(res) if (res!=XI_OK) goto error; - - int mvret = XI_OK; - if(numDevices == 0) return false; + int mvret = XI_OK; if((mvret = xiOpenDevice( wIndex, &hmv)) != XI_OK) { #if defined _WIN32 @@ -97,12 +107,33 @@ bool CvCaptureCAM_XIMEA::open( int wIndex ) #endif } + return _open(); +} + +bool CvCaptureCAM_XIMEA::open( const char* serialNumber ) +{ + if(numDevices == 0) + return false; + + int mvret = XI_OK; + if((mvret = xiOpenDeviceBy(XI_OPEN_BY_SN, serialNumber, &hmv)) != XI_OK) + { + errMsg("Open XI_DEVICE failed", mvret); + return false; + } + + return _open(); +} + +bool CvCaptureCAM_XIMEA::_open() +{ +#define HandleXiResult(res) if (res!=XI_OK) goto error; int width = 0; int height = 0; int isColor = 0; // always use auto exposure/gain - mvret = xiSetParamInt( hmv, XI_PRM_AEAG, 1); + int mvret = xiSetParamInt( hmv, XI_PRM_AEAG, 1); HandleXiResult(mvret); mvret = xiGetParamInt( hmv, XI_PRM_WIDTH, &width); diff --git a/modules/videoio/src/precomp.hpp b/modules/videoio/src/precomp.hpp index 10babeeb95..3a29a89f56 100644 --- a/modules/videoio/src/precomp.hpp +++ b/modules/videoio/src/precomp.hpp @@ -126,6 +126,7 @@ CvCapture* cvCreateFileCapture_OpenNI( const char* filename ); CvCapture* cvCreateFileCapture_OpenNI2( const char* filename ); CvCapture* cvCreateCameraCapture_Android( int index ); CvCapture* cvCreateCameraCapture_XIMEA( int index ); +CvCapture* cvCreateCameraCapture_XIMEA( const char* serialNumber ); CvCapture* cvCreateCameraCapture_AVFoundation(int index); CvCapture* cvCreateCameraCapture_Aravis( int index );