1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 【开源项目】C++BASE64图像编解码算法

【开源项目】C++BASE64图像编解码算法

时间:2019-09-08 09:22:02

相关推荐

【开源项目】C++BASE64图像编解码算法

ZBase64.h

#pragma once#include <string>using namespace std;class ZBase64{public:ZBase64(void);~ZBase64(void);/*编码DataByte[in]输入的数据长度,以字节为单位*/string Encode(const unsigned char* Data, int DataByte);/*解码DataByte[in]输入的数据长度,以字节为单位OutByte[out]输出的数据长度,以字节为单位,请不要通过返回值计算输出数据的长度*/string Decode(const char* Data, int DataByte, int& OutByte);};

ZBase64.cpp

#include "ZBase64.h"ZBase64::ZBase64(void){}ZBase64::~ZBase64(void){}string ZBase64::Encode(const unsigned char * Data, int DataByte){//编码表const char EncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//返回值string strEncode;unsigned char Tmp[4] = { 0 };int LineLength = 0;for (int i = 0; i < (int)(DataByte / 3); i++){Tmp[1] = *Data++;Tmp[2] = *Data++;Tmp[3] = *Data++;strEncode += EncodeTable[Tmp[1] >> 2];strEncode += EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F];strEncode += EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F];strEncode += EncodeTable[Tmp[3] & 0x3F];if (LineLength += 4, LineLength == 76) { strEncode += "\r\n"; LineLength = 0; }}//对剩余数据进行编码int Mod = DataByte % 3;if (Mod == 1){Tmp[1] = *Data++;strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2];strEncode += EncodeTable[((Tmp[1] & 0x03) << 4)];strEncode += "==";}else if (Mod == 2){Tmp[1] = *Data++;Tmp[2] = *Data++;strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2];strEncode += EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)];strEncode += EncodeTable[((Tmp[2] & 0x0F) << 2)];strEncode += "=";}return strEncode;}string ZBase64::Decode(const char * Data, int DataByte, int & OutByte){//解码表const char DecodeTable[] ={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,62, // '+'0, 0, 0,63, // '/'52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'0, 0, 0, 0, 0, 0, 0,0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'0, 0, 0, 0, 0, 0,26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'};//返回值string strDecode;int nValue;int i = 0;while (i < DataByte){if (*Data != '\r' && *Data != '\n'){nValue = DecodeTable[*Data++] << 18;nValue += DecodeTable[*Data++] << 12;strDecode += (nValue & 0x00FF0000) >> 16;OutByte++;if (*Data != '='){nValue += DecodeTable[*Data++] << 6;strDecode += (nValue & 0x0000FF00) >> 8;OutByte++;if (*Data != '='){nValue += DecodeTable[*Data++];strDecode += nValue & 0x000000FF;OutByte++;}}i += 4;}else// 回车换行,跳过{Data++;i++;}}return strDecode;}

图像Base64编码,需要依赖OpenCV

CString BA64(char* a){CString strMfc;Mat img = imread(a);vector<uchar> vecImg; //Mat 图片数据转换为vector<uchar>vector<int> vecCompression_params;vecCompression_params.push_back(IMWRITE_JPEG_QUALITY);vecCompression_params.push_back(90);imencode(".jpg", img, vecImg, vecCompression_params);ZBase64 base64;string imgbase64 = base64.Encode(vecImg.data(), vecImg.size());//实现图片的base64编码strMfc = imgbase64.c_str();return strMfc;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。