【问题标题】:c++ convert png to base64c ++将png转换为base64
【发布时间】:2018-06-26 15:43:33
【问题描述】:

我发现将 png 文件转换为 base64,但它要求从文件流中读取,例如:

ostringstream sout;
istringstream sin;

// this is the object we will use to do the base64 encoding
base64 base64_coder;



// now base64 encode the compressed data
base64_coder.encode(sin,sout);

我将opencv中的png转换为:

                imencode(".png", im, buf);

当我想转换时

    base64_coder.encode(buf,sout);

它要求流..

我的 c++ 知识有限,因此感谢任何帮助。

这样做的目的:

我需要将 png 图像写入可供 meteorjs 使用的 mongodb 。所以他们要求 base64 编码。图片。

谢谢

编辑:我是 Cv::Mat 。对象。我正在将其转换为 png 。 buf 包括 png。

【问题讨论】:

  • 我是什么,buf 是什么?您需要显示带有错误消息的完整示例。
  • 您可以使用gridfs或collectionFS将图像存储在mongodb上,然后您不需要将它们转换为base64
  • im 是 cv::mat 并转换为 png 到 in buf。
  • 检查 Nyffenegger 的 b64 解码/编码实现它需要一个 char *(我假设你的缓冲区是),而不是流,并返回一个 std::string,我做了类似的事情来存储mongodb中的图片一次:)

标签: c++ opencv meteor base64


【解决方案1】:

什么对我有用:

  • https://github.com/ReneNyffenegger/cpp-base64 中的.cpp 和.h 文件添加到我的项目中;

    string encoded_png;
    Mat img; // Load an image here
    
    vector<uchar> buf;
    cv::imencode(".png", img, buf);
    auto base64_png = reinterpret_cast<const unsigned char*>(buf.data());
    encoded_png = "data:image/jpeg;base64," + base64_encode(base64_png, buf.size());
    

encoded_png现在包含base64字符串,可以解码得到原图。

【讨论】:

    【解决方案2】:

    我找到了:

    auto base64_png = reinterpret_cast<const unsigned char*>(buf.data());
                    std::string encoded_png = "data:image/jpeg;base64,"+base64_encode(base64_png,buf.size());
    

    github中的标头

    解决了我的问题

    【讨论】:

      猜你喜欢
      • 2023-02-24
      • 2017-09-12
      • 2011-11-20
      • 2015-05-30
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 2017-07-02
      • 2016-03-20
      相关资源
      最近更新 更多