C++读写图片数据转成Base64格式的一种方法

时间:2022-12-25 17:15:09

最近在一个项目中要实现在客户端和服务端之间传送图片文件的功能,采用了C++语言读写图片转化成Base64格式进行传输。具体代码如下:

 //++Base64.h

 #pragma once

 class CBase64
{
public:
public:
CBase64();
~CBase64(); /*编码
DataByte
[in]输入的数据长度,以字节为单位
*/
std::string Encode(const char* Data, int DataByte); /*解码
DataByte
[in]输入的数据长度,以字节为单位
OutByte
[out]输出的数据长度,以字节为单位,请不要通过返回值计算
输出数据的长度
*/
std::string Decode(const char* Data, int DataByte, int& OutByte); }; //++Base64.cpp
#include"stdafx.h"
#include"Base64.h" CBase64::CBase64()
{ } CBase64::~CBase64()
{ } std::string CBase64::Encode(const char* Data, int DataByte)
{
//编码表
const char EncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//返回值
string strEncode;
unsigned char Tmp[] = { };
int LineLength = ;
for (int i = ; i<(int)(DataByte / ); i++)
{
Tmp[] = *Data++;
Tmp[] = *Data++;
Tmp[] = *Data++;
strEncode += EncodeTable[Tmp[] >> ];
strEncode += EncodeTable[((Tmp[] << ) | (Tmp[] >> )) & 0x3F];
strEncode += EncodeTable[((Tmp[] << ) | (Tmp[] >> )) & 0x3F];
strEncode += EncodeTable[Tmp[] & 0x3F];
if (LineLength += , LineLength == ) { strEncode += "\r\n"; LineLength = ; }
}
//对剩余数据进行编码
int Mod = DataByte % ;
if (Mod == )
{
Tmp[] = *Data++;
strEncode += EncodeTable[(Tmp[] & 0xFC) >> ];
strEncode += EncodeTable[((Tmp[] & 0x03) << )];
strEncode += "==";
}
else if (Mod == )
{
Tmp[] = *Data++;
Tmp[] = *Data++;
strEncode += EncodeTable[(Tmp[] & 0xFC) >> ];
strEncode += EncodeTable[((Tmp[] & 0x03) << ) | ((Tmp[] & 0xF0) >> )];
strEncode += EncodeTable[((Tmp[] & 0x0F) << )];
strEncode += "=";
} return strEncode;
} std::string CBase64::Decode(const char* Data, int DataByte, int& OutByte)
{
//解码表
const char DecodeTable[] =
{
, , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , ,
, // '+'
, , ,
, // '/'
, , , , , , , , , , // '0'-'9'
, , , , , , ,
, , , , , , , , , , , , ,
, , , , , , , , , , , , , // 'A'-'Z'
, , , , , ,
, , , , , , , , , , , , ,
, , , , , , , , , , , , , // 'a'-'z'
};
//返回值
string strDecode;
int nValue;
int i = ;
while (i < DataByte)
{
if (*Data != '\r' && *Data != '\n')
{
nValue = DecodeTable[*Data++] << ;
nValue += DecodeTable[*Data++] << ;
strDecode += (nValue & 0x00FF0000) >> ;
OutByte++;
if (*Data != '=')
{
nValue += DecodeTable[*Data++] << ;
strDecode += (nValue & 0x0000FF00) >> ;
OutByte++;
if (*Data != '=')
{
nValue += DecodeTable[*Data++];
strDecode += nValue & 0x000000FF;
OutByte++;
}
}
i += ;
}
else// 回车换行,跳过
{
Data++;
i++;
}
}
return strDecode;
} 以下是读写图片的调用代码:
bool CBusinessDataMgr::ReadPhotoFile(std::basic_string<TCHAR> strFileName,std::string &strData)
{
HANDLE hFile;
hFile = CreateFile(strFileName.c_str(), GENERIC_READ, , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE)
{
return false;
} DWORD dFileSize = GetFileSize(hFile, NULL);
char * pBuffer = new char[dFileSize + ]; if(pBuffer == NULL)
return false; memset(pBuffer, , dFileSize); DWORD dReadSize();
if (!ReadFile(hFile, pBuffer, dFileSize, &dReadSize, NULL))
{
delete[]pBuffer;
CloseHandle(hFile);
return false;
} CBase64 base64;
strData = "";
strData = base64.Encode((const char*)pBuffer, dReadSize); delete[]pBuffer;
CloseHandle(hFile);
return true;
} bool CBusinessDataMgr::WritePhotoFile(std::basic_string<TCHAR> strFileName, std::string &strData)
{
HANDLE hFile;
hFile = CreateFile(strFileName.c_str(), GENERIC_WRITE, , NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE)
{
return false;
} CBase64 base64;
int datalen();
DWORD dwritelen();
std::string strdcode = base64.Decode(strData.data(),strData.size(), datalen);
if (!WriteFile(hFile, strdcode.data(), datalen, &dwritelen, NULL))
{
CloseHandle(hFile);
return false;
}
CloseHandle(hFile);
return true;
}