【问题标题】:How to transfer a C++ object to a web service using POCO library如何使用 POCO 库将 C++ 对象传输到 Web 服务
【发布时间】:2016-04-21 13:40:33
【问题描述】:

我有一个使用 Qt 和 openCV 的图像处理应用程序。

对于每一帧,我应该将捕获的 cv::Mat 图像对象发送到服务器进行处理并得到结果。

我应该使用 REST 架构,因为它的负载较低。

我应该使用什么工具将 cv::Mat 发送到服务器。

我使用 POCO 是为了便于携带。

我寻求最轻量级的解决方案来做到这一点,我需要服务器在一秒钟内处理 10 帧的最低速度。

我的意思是,有没有一种方法可以在没有显式序列化的情况下将 C++ 对象传递给服务器?

【问题讨论】:

  • 1) 将图像序列化为字符串(使用imencode 并最终使用base64 编码轻松完成),2) 通过REST 发送字符串 3) 在服务器上解码图像(base64 解码和imdecode ) 4) 现在你有你的图像服务器端了。
  • 谢谢,你为什么不把它作为答案?问题是我仍然是 POCO 的初学者,并且正在寻找在 POCO 中发送 cv::Mat 的完整示例。
  • 因为一个完整的答案会太宽泛(如问题;D)。我只是给你一些指示。您可以轻松找到每个步骤的大量信息
  • 是的,您的观点非常有用。

标签: c++ web-services rest opencv poco-libraries


【解决方案1】:

编辑

使用 POCO 库,你可以看看这个答案:HttpRequest PUT content in poco library。他正在通过ifstream 发送文件。

在此答案中,您可以查看如何将 cv::Mat 转换为 istreamOpenCV cv::Mat to std::ifstream for base64 encoding

最后,由于多态性,istream 被隐式转换为 ifstream。


您可以使用 C++ Rest SDK。 PUT 命令的代码示例。

Source of code

Library Github where you can find the full documentation.

#include <http_client.h>
#include <filestream.h>
#include <iostream>
#include <sstream>

using namespace web::http;
using namespace web::http::client;

// Upload a file to an HTTP server.
pplx::task<void> UploadFileToHttpServerAsync()
{
    using concurrency::streams::file_stream;
    using concurrency::streams::basic_istream;

    // To run this example, you must have a file named myfile.txt in the current folder. 
    // Alternatively, you can use the following code to create a stream from a text string. 
    // std::string s("abcdefg");
    // auto ss = concurrency::streams::stringstream::open_istream(s); 

    // Open stream to file. 
    return file_stream<unsigned char>::open_istream(L"myfile.txt").then([](pplx::task<basic_istream<unsigned char>> previousTask)
    {
        try
        {
            auto fileStream = previousTask.get();

            // Make HTTP request with the file stream as the body.
            http_client client(L"http://www.fourthcoffee.com");
            return client.request(methods::PUT, L"myfile", fileStream).then([fileStream](pplx::task<http_response> previousTask)
            {
                fileStream.close();

                std::wostringstream ss;
                try
                {
                    auto response = previousTask.get();
                    ss << L"Server returned returned status code " << response.status_code() << L"." << std::endl;
                }
                catch (const http_exception& e)
                {
                    ss << e.what() << std::endl;
                }
                std::wcout << ss.str();
            });
        }
        catch (const std::system_error& e)
        {
            std::wostringstream ss;
            ss << e.what() << std::endl;
            std::wcout << ss.str();

            // Return an empty task. 
            return pplx::task_from_result();
        }
    });

    /* Sample output:
    The request must be resent
    */
}

【讨论】:

  • 非常感谢,+1。但我在编辑时使用 POCO 来实现可移植性。
  • 我编辑了我的答案,看起来链接的答案有你需要的。希望对您有所帮助。
  • 谢谢,但在示例中他正在发送文件。我一般搜索发送一个 cv::Mat 或纯 C++ 对象。
  • 你真的需要直接从内存中传递值吗?一种选择是使用cv::imwrite 命令将图像保存到文件中。
  • 这是一个解决方案。但这会使应用程序更重。我应该以 10 帧/秒的最低速度进行处理。我搜索以了解如何从客户端部分序列化对象并能够从服务器部分读取它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
相关资源
最近更新 更多