sample code “set Eframing”

usage:
    set <horizontal_denominator> <vertical_denominator> <eframingType>
    addarea <in/out> <areaNo> <x> <y> <width> <height>
    removearea <in/out> <areaNo>
    commit
    update <in/out> <areaNo> <x> <y> <width> <height>
    get
To exit, please enter 'q'.
  • About eframingType
    • None

    • Auto

    • Single

    • PTZ

    • HoldCurrentPosition

    • ForceZoomOut

sample:
   set 640 480 PTZ
   addarea in 0 1 1 640 480
   addarea out 0 0 0 640 480
   commit
   update in 0 10 10 640 480

This sample code is included in SimpleCli.

  1// eframing sample
  2#include <chrono>
  3#include <cstdint>
  4#include <cstdlib>
  5#include <fstream>
  6#include <future>
  7#include <iostream>
  8#include <mutex>
  9#include <sstream>
 10#include <stdexcept>
 11#include <string>
 12#include <thread>
 13#include <vector>
 14
 15#if !defined(__APPLE__)
 16  #if defined(USE_EXPERIMENTAL_FS) // for jetson
 17    #include <experimental/filesystem>
 18    namespace fs = std::experimental::filesystem;
 19  #else
 20    #include <filesystem>
 21    namespace fs = std::filesystem;
 22  #endif
 23#endif
 24
 25#if defined(__APPLE__) || defined(__linux__)
 26  #include <unistd.h>
 27#endif
 28
 29// macro for multibyte character
 30#if defined(_WIN32) || defined(_WIN64)
 31  using CrString = std::wstring;
 32  #define CRSTR(s) L ## s
 33  #define CrCout std::wcout
 34#else
 35  using CrString = std::string;
 36  #define CRSTR(s) s
 37  #define CrCout std::cout
 38#endif
 39
 40
 41#include "CrDeviceProperty.h"
 42#include "CameraRemote_SDK.h"
 43#include "IDeviceCallback.h"
 44
 45#define PrintError(msg, err) { fprintf(stderr, "Error in %s(%d):" msg ",0x%x\n", __FUNCTION__, __LINE__, err); }
 46#define GotoError(msg, err) { PrintError(msg, err); goto Error; }
 47
 48bool  m_connected = false;
 49CrString m_modelId;
 50
 51std::mutex m_eventPromiseMutex;
 52std::promise<void>* m_eventPromise = nullptr;
 53void setEventPromise(std::promise<void>* dp)
 54{
 55    std::lock_guard<std::mutex> lock(m_eventPromiseMutex);
 56    m_eventPromise = dp;
 57}
 58
 59class DeviceCallback : public SCRSDK::IDeviceCallback
 60{
 61public:
 62    DeviceCallback() {};
 63    ~DeviceCallback() {};
 64
 65    void OnConnected(SCRSDK::DeviceConnectionVersioin version)
 66    {
 67        CrCout << "Connected to " << m_modelId << "\n";
 68        m_connected = true;
 69        std::lock_guard<std::mutex> lock(m_eventPromiseMutex);
 70        if(m_eventPromise) {
 71            m_eventPromise->set_value();
 72            m_eventPromise = nullptr;
 73        }
 74    }
 75
 76    void OnError(CrInt32u error)
 77    {
 78        printf("Connection error 0x%x\n", error);
 79        std::lock_guard<std::mutex> lock(m_eventPromiseMutex);
 80        if(m_eventPromise) {
 81            m_eventPromise->set_exception(std::make_exception_ptr(std::runtime_error("error")));
 82            m_eventPromise = nullptr;
 83        }
 84    }
 85
 86    void OnDisconnected(CrInt32u error)
 87    {
 88        CrCout << "Disconnected from " << m_modelId << "\n";
 89        m_connected = false;
 90        std::lock_guard<std::mutex> lock(m_eventPromiseMutex);
 91        if(m_eventPromise) {
 92            m_eventPromise->set_value();
 93            m_eventPromise = nullptr;
 94        }
 95    }
 96
 97    void OnWarning(CrInt32u warning)
 98    {
 99        if (warning == SCRSDK::CrWarning_Connect_Reconnecting) {
100            CrCout << "Reconnecting to " << m_modelId << "\n";
101            return;
102        }
103    }
104
105    void OnWarningExt(CrInt32u warning, CrInt32 param1, CrInt32 param2, CrInt32 param3) {}
106    void OnLvPropertyChanged() {}
107    void OnLvPropertyChangedCodes(CrInt32u num, CrInt32u* codes) {}
108    void OnPropertyChanged() {}
109    void OnPropertyChangedCodes(CrInt32u num, CrInt32u* codes) {}
110};
111
112CrString _getModelId(const SCRSDK::ICrCameraObjectInfo* objInfo)
113{
114    CrString id;
115    if (CrString(objInfo->GetConnectionTypeName()) == CRSTR("IP")) {
116        id = CrString(objInfo->GetMACAddressChar());
117    } else {
118        id = CrString((CrChar*)objInfo->GetId());
119    }
120    return CrString(objInfo->GetModel()).append(CRSTR(" (")).append(id).append(CRSTR(")"));
121}
122
123SCRSDK::CrError _getIdPassword(SCRSDK::ICrCameraObjectInfo* objInfo, std::string& fingerprint, std::string& userId, std::string& userPassword)
124{
125    char fpBuff[128] = {0};
126    CrInt32u fpLen = 0;
127    SCRSDK::CrError err = SCRSDK::GetFingerprint(objInfo, fpBuff, &fpLen);
128    if(err) GotoError("", err);
129    fingerprint = std::string(fpBuff, fpLen);
130
131    std::cout << "fingerprint: " << fingerprint.c_str() << "\n";
132    std::cout << "id:";       std::getline(std::cin, userId);
133    std::cout << "password:"; std::getline(std::cin, userPassword);
134    return 0;
135Error:
136    return err;
137}
138
139std::vector<std::string> _split(std::string inputLine, char delimiter)
140{
141    std::vector<std::string> strArray;
142    if (inputLine.empty()) return strArray;
143
144    std::string tmp;
145    std::stringstream ss{inputLine};
146    while (getline(ss, tmp, delimiter)) {
147        strArray.push_back(tmp);
148    }
149    return strArray;
150}
151
152int main(void)
153{
154    int result = -1;
155    SCRSDK::CrError err = SCRSDK::CrError_None;
156    int64_t  m_device_handle = 0;
157    SCRSDK::ICrEnumCameraObjectInfo* enumCameraObjectInfo = nullptr;
158    SCRSDK::ICrCameraObjectInfo* objInfo = nullptr;
159    DeviceCallback deviceCallback;
160
161  #if defined(__APPLE__)
162    #define MAC_MAX_PATH 255
163    char pathBuf[MAC_MAX_PATH] = {0};
164    if(NULL == getcwd(pathBuf, sizeof(pathBuf) - 1)) return 1;
165    CrString path = pathBuf;
166  #else
167    CrString path = fs::current_path().native();
168  #endif
169
170    SCRSDK::CrEframingInfo eframingInfo;
171
172    bool boolRet = SCRSDK::Init();
173    if(!boolRet) GotoError("", 0);
174
175    // enumeration
176    {
177        uint32_t count = 0;
178        uint32_t index = 1;
179
180        err = SCRSDK::EnumCameraObjects(&enumCameraObjectInfo, 3/*timeInSec*/);
181        if(err || !enumCameraObjectInfo) GotoError("no camera", err);
182
183        count = enumCameraObjectInfo->GetCount();
184        if(count >= 2) {
185            for (uint32_t i = 0; i < count; ++i) {
186                auto* info = enumCameraObjectInfo->GetCameraObjectInfo(i);
187                CrCout << '[' << i + 1 << "] " << _getModelId(info) << "\n";
188            }
189
190            std::string inputLine;
191            std::cout << "select camera:"; std::getline(std::cin, inputLine);
192            try { index = stoi(inputLine); } catch(const std::exception&) { GotoError("", 0); }
193            if(index < 1 || index > count) GotoError("", 0);
194        }
195        objInfo = (SCRSDK::ICrCameraObjectInfo*)enumCameraObjectInfo->GetCameraObjectInfo(index - 1);
196        m_modelId = _getModelId(objInfo);
197    }
198
199    // connect
200    {
201        std::string  fingerprint = "";
202        std::string  userId = "";
203        std::string  userPassword = "";
204        std::promise<void> eventPromise;
205        std::future<void> eventFuture = eventPromise.get_future();
206
207        if (objInfo->GetSSHsupport() == SCRSDK::CrSSHsupport_ON) {
208            err = _getIdPassword(objInfo, fingerprint, userId, userPassword); if(err) goto Error;
209        }
210
211        setEventPromise(&eventPromise);
212        err = SCRSDK::Connect(objInfo, &deviceCallback, &m_device_handle,
213            SCRSDK::CrSdkControlMode_Remote,
214            SCRSDK::CrReconnecting_ON,
215            userId.c_str(), userPassword.c_str(), fingerprint.c_str(), (uint32_t)fingerprint.size());
216        if(err) GotoError("", err);
217
218    //  std::future_status status = eventFuture.wait_for(std::chrono::milliseconds(3000));
219    //  if(status != std::future_status::ready) GotoError("timeout",0);
220        try{
221            eventFuture.get();
222        } catch(const std::exception&) GotoError("", 0);
223    }
224
225    // set work directory
226    {
227        CrCout << "path=" << path.data() << "\n";
228        err = SCRSDK::SetSaveInfo(m_device_handle, const_cast<CrChar*>(path.data()), const_cast<CrChar*>(CRSTR("DSC")), -1/*startNo*/);
229        if(err) GotoError("", err);
230    }
231
232    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
233
234    std::cout << "usage:\n";
235    std::cout << "   set <horizontal_denominator> <vertical_denominator> <eframingType>\n";
236    std::cout << "   addarea <in/out> <areaNo> <x> <y> <width> <height>\n";
237    std::cout << "   removearea <in/out> <areaNo>\n";
238    std::cout << "   commit\n";
239    std::cout << "   update <in/out> <areaNo> <x> <y> <width> <height>\n";
240    std::cout << "To exit, please enter 'q'.\n";
241
242    while(1) {
243        std::string inputLine;
244        std::getline(std::cin, inputLine);
245        std::vector<std::string> args = _split(inputLine, ' ');
246
247        const std::string cmdName = args.size()==0 ? "" : args[0];
248
249        if(cmdName == "set" && args.size() >= 4) {
250            uint32_t val_h=0, val_v=0;
251            try { val_h = stoi(args[1]); } catch(const std::exception&) { GotoError("", 0); }
252            try { val_v = stoi(args[2]); } catch(const std::exception&) { GotoError("", 0); }
253
254            SCRSDK::CrEframingType type = SCRSDK::CrEframingType_None;
255            try {
256                std::string sType = args[3];
257                if(sType == "None") type = SCRSDK::CrEframingType_None;
258                else if(sType == "Auto") type = SCRSDK::CrEframingType_Auto;
259                else if(sType == "Single") type = SCRSDK::CrEframingType_Single;
260                else if(sType == "PTZ") type = SCRSDK::CrEframingType_PTZ;
261                else if(sType == "HoldCurrentPosition") type = SCRSDK::CrEframingType_HoldCurrentPosition;
262                else if(sType == "ForceZoomOut") type = SCRSDK::CrEframingType_ForceZoomOut;
263                else GotoError("unknown type", 0);
264            } catch(const std::exception&) { GotoError("", 0); }
265            
266            eframingInfo.horizontal_denominator = val_h * 1024;
267            eframingInfo.vertical_denominator = val_v * 1024;
268            eframingInfo.eframingType = type;
269
270        }else if(cmdName == "addarea" && args.size() >= 7) {
271            try {
272                SCRSDK::CrEframingAreaGroup group = (args[1] == "in") ? SCRSDK::CrEframingInputArea : SCRSDK::CrEframingOutputArea;
273                SCRSDK::CrEframingAreaNumber areaNo = static_cast<SCRSDK::CrEframingAreaNumber>(std::stoi(args[2]));
274                SCRSDK::CrEframingRectangle rect;
275                rect.x = std::stoi(args[3]) * 1024;
276                rect.y = std::stoi(args[4]) * 1024;
277                rect.width = std::stoi(args[5]) * 1024;
278                rect.height = std::stoi(args[6]) * 1024;
279
280                if (group == SCRSDK::CrEframingInputArea) {
281                    eframingInfo.addInputInfo(areaNo, rect);
282                    std::cout << "Added input area to eframingInfo.\n";
283                } else if (group == SCRSDK::CrEframingOutputArea) {
284                    eframingInfo.addOutputInfo(areaNo, rect);
285                    std::cout << "Added output area to eframingInfo.\n";
286                } else {
287                    GotoError("Invalid group for addarea", 0);
288                }
289            } catch (const std::exception&) {
290                GotoError("Invalid arguments for addarea", 0);
291            }
292        }else if(cmdName == "removearea" && args.size() >= 3) {
293            try {
294                SCRSDK::CrEframingAreaGroup group = (args[1] == "in") ? SCRSDK::CrEframingInputArea : SCRSDK::CrEframingOutputArea;
295                SCRSDK::CrEframingAreaNumber areaNo = static_cast<SCRSDK::CrEframingAreaNumber>(std::stoi(args[2]));
296
297                if (group == SCRSDK::CrEframingInputArea) {
298                    eframingInfo.removeInputInfo(areaNo);
299                    std::cout << "Removed input area from eframingInfo.\n";
300                } else if (group == SCRSDK::CrEframingOutputArea) {
301                    eframingInfo.removeOutputInfo(areaNo);
302                    std::cout << "Removed output area from eframingInfo.\n";
303                } else {
304                    GotoError("Invalid group for removearea", 0);
305                }
306            } catch (const std::exception&) {
307                GotoError("Invalid arguments for removearea", 0);
308            }
309        }else if(cmdName == "commit") {
310            err = SCRSDK::ExecuteEframing(m_device_handle, &eframingInfo);
311            if (err) {
312                GotoError("Failed to execute eframing", err);
313            } else {
314                std::cout << "Eframing executed successfully.\n";
315            }
316        }else if(cmdName == "update" && args.size() >= 7) {
317            try {
318                SCRSDK::CrEframingAreaGroup group = (args[1] == "in") ? SCRSDK::CrEframingInputArea : SCRSDK::CrEframingOutputArea;
319                SCRSDK::CrEframingAreaNumber areaNo = static_cast<SCRSDK::CrEframingAreaNumber>(std::stoi(args[2]));
320                CrInt16 x = static_cast<CrInt16>(std::stoi(args[3]) * 1024);
321                CrInt16 y = static_cast<CrInt16>(std::stoi(args[4]) * 1024);
322                CrInt16 width = static_cast<CrInt16>(std::stoi(args[5]) * 1024);
323                CrInt16 height = static_cast<CrInt16>(std::stoi(args[6]) * 1024);
324
325                err = SCRSDK::UpdateEframingArea(m_device_handle, areaNo, group, x, y, width, height);
326                if (err) {
327                    GotoError("Failed to update eframing area", err);
328                } else {
329                    std::cout << "Eframing area updated successfully.\n";
330                }
331            } catch (const std::exception&) {
332                GotoError("Invalid arguments for update", 0);
333            }
334        }else if(inputLine == "q" || inputLine == "Q") {
335            break;
336        }else{
337            std::cout << "unknown DP nor CMD\n";
338        }
339    }
340
341    result = 0;
342Error:
343    if(enumCameraObjectInfo) enumCameraObjectInfo->Release();
344
345    if(m_connected) {
346        std::promise<void> eventPromise;
347        std::future<void> eventFuture = eventPromise.get_future();
348        setEventPromise(&eventPromise);
349        SCRSDK::Disconnect(m_device_handle);
350        eventFuture.wait_for(std::chrono::milliseconds(3000));
351    }
352    if(m_device_handle) SCRSDK::ReleaseDevice(m_device_handle);
353    SCRSDK::Release();
354
355    return result;
356}