最近在一个项目中要实现在客户端和服务端之间传送图片文件的功能,采用了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;
}

C++读写图片数据转成Base64格式的一种方法的更多相关文章

  1. C++读写图片数据转成Base64格式

    转载:http://www.cnblogs.com/jeray/p/8746976.html 转载:https://www.cnblogs.com/lujin49/p/4957742.html 转载: ...

  2. 将图片文件转成BASE64格式

    html5Reader (file, item) { const reader = new FileReader() reader.onload = (e) => { this.$set(ite ...

  3. js如何将选中图片文件转换成Base64字符串?

    如何将input type="file"选中的文件转换成Base64的字符串呢? 1.首先了解一下为什么要把图片文件转换成Base64的字符串 在常规的web开发过程中,大部分上传 ...

  4. CAFFE学习笔记(四)将自己的jpg数据转成lmdb格式

    1 引言 1-1 以example_mnist为例,如何加载属于自己的测试集? 首先抛出一个问题:在example_mnist这个例子中,测试集是人家给好了的.那么如果我们想自己试着手写几个数字然后验 ...

  5. 使用gfortran将数据写成Grads格式的代码示例

    使用gfortran将数据写成Grads格式的代码示例: !-----'Fortran4Grads.f90' program Fortran4Grads implicit none integer,p ...

  6. js 将图片文件转换成base64

      1.情景展示 在JavaScript中,如何使用图片文件转换成base64? 2.解决方案 /** * 网络图像文件转Base64 * @param img dom对象 */ function g ...

  7. 图片文件转换成Base64编码实现ajax提交图片

    //上传头像图片 function uploadHead(imgPath) { console.log("imgPath = " + imgPath); var image = n ...

  8. php 将图片文件转成base64编码的方法

    php 将图片文件转成base64编码的方法<pre><?php /** 文件转base64输出 * @param String $file 文件路径 * @return Strin ...

  9. 利用python将excel数据解析成json格式

    利用python将excel数据解析成json格式 转成json方便项目中用post请求推送数据自定义数据,也方便测试: import xlrdimport jsonimport requests d ...

随机推荐

  1. python文档-基本API命令翻译及使用方法!

    1.使用tkinter.Tk() 生成主窗口(window=tkinter.Tk()): window.title('标题名')         修改框体的名字,也可在创建时使用className参数 ...

  2. Oracle课程档案,第十二天

    死锁是由于两个对象在拥有一份资源的情况下申请另一份资源, 而另一份资源恰好又是这两对象正持有的,导致两对象无法完成操作,且所持资源无法释放. 阻塞是由于资源不足引起的排队等待现象. unso:撤销 c ...

  3. inner_product

    版本1: template < class InputIterator1, class InputIterator2, class T> T inner_product(InputIter ...

  4. Saltstack如何修改主机名或者minion id

    参考:http://www.mamicode.com/info-detail-2241784.html 在生产维护的过程中,由于某种需要,可能需要给一些服务器改名,比如根据服务器的用途重新进行定义主机 ...

  5. 树莓派3 之 安装Mysql服务

    需求 在树莓派上 安装Mysql 服务,并开启远程访问 步骤 安装 mysql server $ sudo apt-get install mysql-server 我以为中间会让我提示输入 数据库r ...

  6. css3 伸缩百分比的调整

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. SpringBoot-内部运行jvm参数调优

    SpringBoot JVM参数调优 这个根据服务器的内存大小,来设置堆参数. -Xms :设置Java堆栈的初始化大小 -Xmx :设置最大的java堆大小 实例参数-XX:+PrintGCDeta ...

  8. 基于UVM的verilog验证

    Abstract 本文介绍UVM框架,并以crc7为例进行UVM的验证,最后指出常见的UVM验证开发有哪些坑,以及怎么避免. Introduction 本例使用环境:ModelSim 10.2c,UV ...

  9. app优化篇

    UIImageView高效加个圆角 一般通过clipsToBounds和layer.cornerRadius会强制Core Animation提前渲染屏幕的离屏绘制,影响性能. 通过贝塞尔曲线切割图片 ...

  10. maven build resources

    1.在我用springboot+mytatis时,生成完mapper后,然后访问网站总是报错 错误信息: Servlet.service() for servlet [dispatcherServle ...