C++ 无法从编码数据中检索jpg文件

C++ 无法从编码数据中检索jpg文件,c++,opencv,C++,Opencv,我编写了一个opencv程序,它将jpg文件源读入IplImage,对imageData成员进行编码,对imageData进行解码,然后将解码后的数据复制到新的IplImage结构目标中。当我保存这个新的IplImage结构时,我无法检索原始图像。我不确定程序中出了什么问题。我的要求是对jpg文件进行编码,将编码数据存储在文件中,然后从编码数据中检索jpg文件。节目如下: #include <boost/archive/iterators/binary_from_base64.hpp>

我编写了一个opencv程序,它将jpg文件源读入IplImage,对imageData成员进行编码,对imageData进行解码,然后将解码后的数据复制到新的IplImage结构目标中。当我保存这个新的IplImage结构时,我无法检索原始图像。我不确定程序中出了什么问题。我的要求是对jpg文件进行编码,将编码数据存储在文件中,然后从编码数据中检索jpg文件。节目如下:

#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/algorithm/string.hpp>

std::string Base641::encode64(const std::string &val) {
using namespace boost::archive::iterators;
using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>;
auto tmp = std::string(It(std::begin(val)), It(std::end(val)));
return tmp.append((3 - val.size() % 3) % 3, '=');
}

std::string Base641::decode64(const std::string &val) {
using namespace boost::archive::iterators;
using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
return boost::algorithm::trim_right_copy_if(std::string(It(std::begin(val)), It(std::end(val))), [](char c) {
    return c == '\0';
});
}

class Base641
{
public:
std::string encode64(const std::string &val);
std::string decode64(const std::string &val);
};

void main()
{
string path = "C://source.jpg";
string path1 = "C://destination.jpg";

Base641 coder;

IplImage *src_img = cvLoadImage(path.c_str());

IplImage *dest_img = cvCreateImage(cvSize(src_img->width, src_img->height), IPL_DEPTH_8U, 3);

char *data = src_img->imageData;
int len = src_img->height;
int width = src_img->width;
int channels = src_img->nChannels;
int step = src_img->widthStep;

string encode_data = coder.encode64(data);
string decode_data= coder.decode64(encode_data);

string data1 = src_img->imageData;

cout << "sizeof orig data : " << data1.size() << endl;
cout << "sizeof decoded data : " << decode_data.size()<<endl;


if (!strcmp(data1.c_str(), decode_data.c_str()))
    cout << "data same" << endl;
else
    cout << "data not same" << endl;

dest_img->height = len;
dest_img->width = width;
dest_img->nChannels = channels;
dest_img->widthStep = step;

dest_img->imageData = new char[len*width*channels];

memcpy(dest_img->imageData, decode_data.c_str(), decode_data.size());

cvSaveImage(path1.c_str(), dest_img);
}

你是说运行程序后无法打开JPEG文件吗?它会创建一个新的jpg文件,我得到原始图像顶部约5%的部分。新jpg文件的其余部分为灰色,图像已损坏。你能在jpeg上运行hextump吗?它应该以FF D8开始,以FF D9结束。为什么不使用imencode和imdecode?请不要使用IplImages或opencv不再维护的c-api中的任何内容。现在必须使用cv::Mat。然后,不要用base64将图像放大3倍,这太愚蠢了!就像@miki说的,使用imencode/imdecode。
sizeof orig data : 279874
sizeof decoded data : 279874
data same