123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #include "LHCompareAPI.h"
- #include <QCoreApplication>
- #include <QLibrary>
- #include <QString>
- #include "spdlog/spdlog.h"
- // extern "C" CompareResult* compare(const char *source_file, const char *target_file, double compare_sensitivity, ChromaprintAlgorithm algorithm=CHROMAPRINT_ALGORITHM_TEST5_2);
- // extern "C" void free_compare_result(CompareResult* result);
- using Fncompare = CompareResult* (*)(const char *, const char *, double, ChromaprintAlgorithm);
- using FnFreeCompareResult = void (*)(CompareResult*);
- Fncompare fnCompare = nullptr;
- FnFreeCompareResult fnFreeCompareResult = nullptr;
- static bool isLoaded = false;
- /* 加载动态库 */
- bool LoadCompareLibrary()
- {
- if(isLoaded)
- {
- SPDLOG_INFO("libsignalcompare.so 已加载!");
- return true;
- }
- QLibrary lib;
- QString strLibPath = QCoreApplication::applicationDirPath() + "/libsignalcompare.so";
- lib.setFileName(strLibPath);
- if (!lib.load())
- {
- SPDLOG_ERROR("加载比对动态库失败: {}", strLibPath.toStdString());
- SPDLOG_ERROR("错误信息: {}", lib.errorString().toStdString());
- return false;
- }
- fnCompare = reinterpret_cast<Fncompare>(lib.resolve("compare"));
- if (fnCompare == nullptr) {
- SPDLOG_ERROR("加载比对函数失败: {}", lib.errorString().toStdString());
- return false;
- }
- fnFreeCompareResult = reinterpret_cast<FnFreeCompareResult>(lib.resolve("free_compare_result"));
- if (fnFreeCompareResult == nullptr) {
- SPDLOG_ERROR("加载释放比对结果函数失败: {}", lib.errorString().toStdString());
- return false;
- }
- SPDLOG_INFO("加载比对动态库成功!");
- isLoaded = true;
- return true;
- }
- /* 获取结果 */
- CompareResult* LHCompare(const char *source_file, const char *target_file, double compare_sensitivity, ChromaprintAlgorithm algorithm)
- {
- if(fnCompare == nullptr) {
- SPDLOG_ERROR("“compare”函数未初始化!");
- return nullptr;
- }
- return fnCompare(source_file, target_file, compare_sensitivity, algorithm);
- }
- /* 释放结果数据 */
- void LHFreeCompareResult(CompareResult* result)
- {
- if(fnFreeCompareResult == nullptr) {
- SPDLOG_ERROR("“free_compare_result”函数未初始化!");
- return;
- }
- fnFreeCompareResult(result);
- result = nullptr;
- }
|