首先base64,了解一下:Base64就是一种基于64个可打印字符来表示二进制数据的方法。
表示二进制数,所以我得先让图片转化为二进制数据,然后对二进制数据进行base64编码;
或者先对base64字符串进行解码,然后再把二进制数据转化为图片保存或展示。
流程
bool imencode(const string& ext, InputArray img, vector& buf, const vector& params=vector())
2.二进制数据 -> base64字符串
网上有很多的 base64编解码 源码,本文末会添加我使用的源码供参考。
附:
#include
#include
#include
#include
#include "base64.h"
using namespace cv;
using namespace std;
void write(string encode_str);
string read(string fileName);
string encode();
Mat decode();int main() { Mat img = decode();imshow("test", img);if (waitKey(1) == 27)return 0;system("pause");return 0;
}
void write(string encode_str)
{ofstream out;out.open("base64_str", ios::out | ios::trunc);out <}
string read(string fileName)
{string base64_str;ifstream in;in.open(fileName, ios::in);in >> base64_str;cout <<"base64_str.length() &#61; " <return base64_str;
}
string encode()
{string img_path("img.jpg");Mat img;img &#61; imread(img_path);if (img.empty()){cout <<"img is empty" <return "Failed";}vector img_data;imencode(".jpg", img, img_data);string str_Encode(img_data.begin(), img_data.end());write(str_Encode);
}
Mat decode()
{string encode_str &#61; read("dog.base");string decode_str;Base64::Decode(encode_str, &decode_str);vector img_data(decode_str.begin(), decode_str.end());Mat img &#61; imdecode(Mat(img_data), CV_LOAD_IMAGE_COLOR);return img;
}
关于base64解码&#xff0c;碰到一个问题&#xff1a;
借助在线编码器&#xff0c;将以下图片&#xff1a;
转为base64字符串&#xff1a;
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGB。。。。。。。。。
如果需要解析字符串&#xff0c;加粗部分需要删除&#xff0c;在未删除时&#xff0c;执行程序提示&#xff1a;
删除后&#xff0c;运行正常&#xff1a;
刚刚发现csdn下载如果上传资源不能免费 0.0
#ifndef BASE64_H
#define BASE64_H#include const char kBase64Alphabet[] &#61; "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789&#43;/";class Base64 {
public:static bool Encode(const std::string &in, std::string *out) {int i &#61; 0, j &#61; 0;size_t enc_len &#61; 0;unsigned char a3[3];unsigned char a4[4];out->resize(EncodedLength(in));int input_len &#61; in.size();std::string::const_iterator input &#61; in.begin();while (input_len--) {a3[i&#43;&#43;] &#61; *(input&#43;&#43;);if (i &#61;&#61; 3) {a3_to_a4(a4, a3);for (i &#61; 0; i <4; i&#43;&#43;) {(*out)[enc_len&#43;&#43;] &#61; kBase64Alphabet[a4[i]];}i &#61; 0;}}if (i) {for (j &#61; i; j <3; j&#43;&#43;) {a3[j] &#61; &#39;\0&#39;;}a3_to_a4(a4, a3);for (j &#61; 0; j 1; j&#43;&#43;) {(*out)[enc_len&#43;&#43;] &#61; kBase64Alphabet[a4[j]];}while ((i&#43;&#43; <3)) {(*out)[enc_len&#43;&#43;] &#61; &#39;&#61;&#39;;}}return (enc_len &#61;&#61; out->size());}static bool Encode(const char *input, size_t input_length, char *out, size_t out_length) {int i &#61; 0, j &#61; 0;char *out_begin &#61; out;unsigned char a3[3];unsigned char a4[4];size_t encoded_length &#61; EncodedLength(input_length);if (out_length return false;while (input_length--) {a3[i&#43;&#43;] &#61; *input&#43;&#43;;if (i &#61;&#61; 3) {a3_to_a4(a4, a3);for (i &#61; 0; i <4; i&#43;&#43;) {*out&#43;&#43; &#61; kBase64Alphabet[a4[i]];}i &#61; 0;}}if (i) {for (j &#61; i; j <3; j&#43;&#43;) {a3[j] &#61; &#39;\0&#39;;}a3_to_a4(a4, a3);for (j &#61; 0; j 1; j&#43;&#43;) {*out&#43;&#43; &#61; kBase64Alphabet[a4[j]];}while ((i&#43;&#43; <3)) {*out&#43;&#43; &#61; &#39;&#61;&#39;;}}return (out &#61;&#61; (out_begin &#43; encoded_length));}static bool Decode(const std::string &in, std::string *out) {int i &#61; 0, j &#61; 0;size_t dec_len &#61; 0;unsigned char a3[3];unsigned char a4[4];int input_len &#61; in.size();std::string::const_iterator input &#61; in.begin();out->resize(DecodedLength(in));while (input_len--) {if (*input &#61;&#61; &#39;&#61;&#39;) {break;}a4[i&#43;&#43;] &#61; *(input&#43;&#43;);if (i &#61;&#61; 4) {for (i &#61; 0; i <4; i&#43;&#43;) {a4[i] &#61; b64_lookup(a4[i]);}a4_to_a3(a3, a4);for (i &#61; 0; i <3; i&#43;&#43;) {(*out)[dec_len&#43;&#43;] &#61; a3[i];}i &#61; 0;}}if (i) {for (j &#61; i; j <4; j&#43;&#43;) {a4[j] &#61; &#39;\0&#39;;}for (j &#61; 0; j <4; j&#43;&#43;) {a4[j] &#61; b64_lookup(a4[j]);}a4_to_a3(a3, a4);for (j &#61; 0; j 1; j&#43;&#43;) {(*out)[dec_len&#43;&#43;] &#61; a3[j];}}return (dec_len &#61;&#61; out->size());}static bool Decode(const char *input, size_t input_length, char *out, size_t out_length) {int i &#61; 0, j &#61; 0;char *out_begin &#61; out;unsigned char a3[3];unsigned char a4[4];size_t decoded_length &#61; DecodedLength(input, input_length);if (out_length return false;while (input_length--) {if (*input &#61;&#61; &#39;&#61;&#39;) {break;}a4[i&#43;&#43;] &#61; *(input&#43;&#43;);if (i &#61;&#61; 4) {for (i &#61; 0; i <4; i&#43;&#43;) {a4[i] &#61; b64_lookup(a4[i]);}a4_to_a3(a3, a4);for (i &#61; 0; i <3; i&#43;&#43;) {*out&#43;&#43; &#61; a3[i];}i &#61; 0;}}if (i) {for (j &#61; i; j <4; j&#43;&#43;) {a4[j] &#61; &#39;\0&#39;;}for (j &#61; 0; j <4; j&#43;&#43;) {a4[j] &#61; b64_lookup(a4[j]);}a4_to_a3(a3, a4);for (j &#61; 0; j 1; j&#43;&#43;) {*out&#43;&#43; &#61; a3[j];}}return (out &#61;&#61; (out_begin &#43; decoded_length));}static int DecodedLength(const char *in, size_t in_length) {int numEq &#61; 0;const char *in_end &#61; in &#43; in_length;while (*--in_end &#61;&#61; &#39;&#61;&#39;) &#43;&#43;numEq;return ((6 * in_length) / 8) - numEq;}static int DecodedLength(const std::string &in) {int numEq &#61; 0;int n &#61; in.size();for (std::string::const_reverse_iterator it &#61; in.rbegin(); *it &#61;&#61; &#39;&#61;&#39;; &#43;&#43;it) {&#43;&#43;numEq;}return ((6 * n) / 8) - numEq;}inline static int EncodedLength(size_t length) {return (length &#43; 2 - ((length &#43; 2) % 3)) / 3 * 4;}inline static int EncodedLength(const std::string &in) {return EncodedLength(in.length());}inline static void StripPadding(std::string *in) {while (!in->empty() && *(in->rbegin()) &#61;&#61; &#39;&#61;&#39;) in->resize(in->size() - 1);}private:static inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {a4[0] &#61; (a3[0] & 0xfc) >> 2;a4[1] &#61; ((a3[0] & 0x03) <<4) &#43; ((a3[1] & 0xf0) >> 4);a4[2] &#61; ((a3[1] & 0x0f) <<2) &#43; ((a3[2] & 0xc0) >> 6);a4[3] &#61; (a3[2] & 0x3f);}static inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {a3[0] &#61; (a4[0] <<2) &#43; ((a4[1] & 0x30) >> 4);a3[1] &#61; ((a4[1] & 0xf) <<4) &#43; ((a4[2] & 0x3c) >> 2);a3[2] &#61; ((a4[2] & 0x3) <<6) &#43; a4[3];}static inline unsigned char b64_lookup(unsigned char c) {if (c >&#61; &#39;A&#39; && c <&#61; &#39;Z&#39;) return c - &#39;A&#39;;if (c >&#61; &#39;a&#39; && c <&#61; &#39;z&#39;) return c - 71;if (c >&#61; &#39;0&#39; && c <&#61; &#39;9&#39;) return c &#43; 4;if (c &#61;&#61; &#39;&#43;&#39;) return 62;if (c &#61;&#61; &#39;/&#39;) return 63;return 255;}
};#endif // BASE64_H
参考:OpenCV学习笔记&#xff08;一&#xff09;&#xff1a;读取、显示、保存图片