我编写了一个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);
}
当我运行程序时,我在控制台上获得此输出:
sizeof orig data : 279874
sizeof decoded data : 279874
data same