相机dll调用
This commit is contained in:
+42
@@ -0,0 +1,42 @@
|
||||
#ifndef HIK_VISION_GUIDANCE_H
|
||||
#define HIK_VISION_GUIDANCE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
# define HVG_CALL __cdecl
|
||||
#else
|
||||
# define HVG_CALL
|
||||
#endif
|
||||
|
||||
#define HVG_ABI_VERSION 1
|
||||
#define HVG_RESPONSE_CAPACITY (1024u * 1024u)
|
||||
|
||||
typedef void *HVG_Handle;
|
||||
|
||||
enum HVG_Status {
|
||||
HVG_OK = 0,
|
||||
HVG_INVALID_ARGUMENT = 1,
|
||||
HVG_BUFFER_TOO_SMALL = 2,
|
||||
HVG_INVALID_HANDLE = 3,
|
||||
HVG_BUSY = 4,
|
||||
HVG_WRONG_THREAD = 5,
|
||||
HVG_CONFIG_ERROR = 6,
|
||||
HVG_DEVICE_ERROR = 7,
|
||||
HVG_NO_TARGET = 8,
|
||||
HVG_TIMEOUT = 9,
|
||||
HVG_CANCELLED = 10,
|
||||
HVG_NOT_READY = 11,
|
||||
HVG_IO_ERROR = 12,
|
||||
HVG_PROTOCOL_ERROR = 13,
|
||||
HVG_DISTANCE_UNKNOWN = 14,
|
||||
HVG_WRITE_OUTCOME_UNKNOWN = 15,
|
||||
HVG_INTERNAL_ERROR = 100
|
||||
};
|
||||
|
||||
typedef int32_t (HVG_CALL *HVG_Function)(HVG_Handle, const char *, char *, uint32_t,
|
||||
uint32_t *);
|
||||
typedef int32_t (HVG_CALL *HVG_InitializeFunction)(const char *, HVG_Handle *, char *,
|
||||
uint32_t, uint32_t *);
|
||||
|
||||
#endif // HIK_VISION_GUIDANCE_H
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
#ifndef HIK_VISION_GUIDANCE_QT_H
|
||||
#define HIK_VISION_GUIDANCE_QT_H
|
||||
|
||||
#include "HikVisionGuidance.h"
|
||||
|
||||
#include <QHash>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QLibrary>
|
||||
#include <QStringList>
|
||||
|
||||
class HikVisionGuidanceQt
|
||||
{
|
||||
public:
|
||||
struct Reply {
|
||||
int code = HVG_INTERNAL_ERROR;
|
||||
QJsonObject json;
|
||||
bool ok() const { return code == HVG_OK; }
|
||||
QString message() const { return json.value("message").toString(); }
|
||||
};
|
||||
|
||||
bool load(const QString &path)
|
||||
{
|
||||
if (m_handle) {
|
||||
m_error = "Close the context before loading another library";
|
||||
return false;
|
||||
}
|
||||
m_functions.clear();
|
||||
m_library.setFileName(path);
|
||||
m_library.setLoadHints(QLibrary::ResolveAllSymbolsHint);
|
||||
if (!m_library.load()) {
|
||||
m_error = m_library.errorString();
|
||||
return false;
|
||||
}
|
||||
m_initialize = reinterpret_cast<HVG_InitializeFunction>(m_library.resolve("HVG_Initialize"));
|
||||
const QStringList names = {
|
||||
"InitializeCamera", "InitializeLight", "InitializePlc", "LoadData",
|
||||
"ReadRobotPose", "WriteRobotPose", "Measure", "PlanAvoidance",
|
||||
"RecognizeGrooves", "Shutdown", "GetVersion", "GetStatus", "GetLastError",
|
||||
"CancelOperation", "EnumerateCameras", "ConfigureCamera", "ConfigureLight",
|
||||
"CaptureImages", "DisconnectCamera", "ExportResult", "StartCamera", "StopCamera",
|
||||
"TurnOnLight", "TurnOffLight", "DisconnectLight", "DisconnectPlc"
|
||||
};
|
||||
for (const QString &name : names) {
|
||||
HVG_Function function = reinterpret_cast<HVG_Function>(
|
||||
m_library.resolve(("HVG_" + name).toLatin1().constData()));
|
||||
if (!function) {
|
||||
m_error = "Missing export HVG_" + name;
|
||||
m_functions.clear();
|
||||
m_library.unload();
|
||||
return false;
|
||||
}
|
||||
m_functions.insert(name, function);
|
||||
}
|
||||
if (!m_initialize) {
|
||||
m_error = "Missing HVG_Initialize";
|
||||
m_library.unload();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reply initialize(const QJsonObject &request)
|
||||
{
|
||||
if (m_handle) return localError(HVG_BUSY, "Already initialized");
|
||||
if (!m_initialize) return localError(HVG_NOT_READY, "DLL is not loaded");
|
||||
QByteArray buffer(int(HVG_RESPONSE_CAPACITY), 0);
|
||||
uint32_t required = 0;
|
||||
const QByteArray input = QJsonDocument(request).toJson(QJsonDocument::Compact);
|
||||
const int code = m_initialize(input.constData(), &m_handle, buffer.data(),
|
||||
uint32_t(buffer.size()), &required);
|
||||
return decode(code, buffer, required);
|
||||
}
|
||||
|
||||
Reply call(const QString &name, const QJsonObject &request = QJsonObject())
|
||||
{
|
||||
HVG_Function function = m_functions.value(name, nullptr);
|
||||
if (!function) return localError(HVG_NOT_READY, "Unknown or unloaded export");
|
||||
QByteArray buffer(int(HVG_RESPONSE_CAPACITY), 0);
|
||||
uint32_t required = 0;
|
||||
const QByteArray input = QJsonDocument(request).toJson(QJsonDocument::Compact);
|
||||
const int code = function(m_handle, input.constData(), buffer.data(),
|
||||
uint32_t(buffer.size()), &required);
|
||||
Reply reply = decode(code, buffer, required);
|
||||
if (name == "Shutdown" && code == HVG_OK) m_handle = nullptr;
|
||||
return reply;
|
||||
}
|
||||
|
||||
Reply close() { return m_handle ? call("Shutdown") : Reply{HVG_OK, {{"success", true}}}; }
|
||||
bool unload()
|
||||
{
|
||||
if (m_handle) return false;
|
||||
m_functions.clear();
|
||||
m_initialize = nullptr;
|
||||
return !m_library.isLoaded() || m_library.unload();
|
||||
}
|
||||
bool isOpen() const { return m_handle != nullptr; }
|
||||
QString errorString() const { return m_error; }
|
||||
HVG_Handle handle() const { return m_handle; }
|
||||
HVG_Function function(const QString &name) const { return m_functions.value(name, nullptr); }
|
||||
int resolvedCount() const { return m_functions.size() + (m_initialize ? 1 : 0); }
|
||||
|
||||
~HikVisionGuidanceQt() { close(); unload(); }
|
||||
HikVisionGuidanceQt() = default;
|
||||
HikVisionGuidanceQt(const HikVisionGuidanceQt &) = delete;
|
||||
HikVisionGuidanceQt &operator=(const HikVisionGuidanceQt &) = delete;
|
||||
|
||||
private:
|
||||
static Reply localError(int code, const QString &message)
|
||||
{
|
||||
return {code, {{"success", false}, {"message", message}, {"code", code}}};
|
||||
}
|
||||
|
||||
static Reply decode(int code, const QByteArray &buffer, uint32_t required)
|
||||
{
|
||||
if (required > uint32_t(buffer.size()))
|
||||
return localError(code, "Response buffer insufficient; call was not replayed");
|
||||
const QJsonDocument document = QJsonDocument::fromJson(buffer.constData());
|
||||
if (!document.isObject())
|
||||
return localError(code, "DLL returned no valid JSON response");
|
||||
return {code, document.object()};
|
||||
}
|
||||
|
||||
QLibrary m_library;
|
||||
QHash<QString, HVG_Function> m_functions;
|
||||
HVG_InitializeFunction m_initialize = nullptr;
|
||||
HVG_Handle m_handle = nullptr;
|
||||
QString m_error;
|
||||
};
|
||||
|
||||
#endif // HIK_VISION_GUIDANCE_QT_H
|
||||
Reference in New Issue
Block a user