最近在一个项目中要实现在客户端和服务端之间传送图片文件的功能,采用了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. Material Designer的低版本兼容实现(五)—— ActivityOptionsCompat

    extends:http://www.cnblogs.com/tianzhijiexian/p/4087917.html 本文是对API中的方法做了介绍,如果想要看如何让这些方法兼容4.x或2.x可以 ...

  2. 前端上传 base64 编码图片到七牛云存储

    参考文档 如何上传base64编码图片到七牛云 调试过程 文档中分别有 java 和 html 的 demo,可以根据文档示例调试. 下面是我调试的过程,可以作为参考,特别注意的是,如果需要给文件起名 ...

  3. 【C++ 模板迭代器实例/半素数】

    题目:判断一个数是不是两个素数的乘积,是输出YES,不是输出NO.数据范围为2-1000000. 为了解决这个问题,我们继续使用STL——vector & set,分别用来存储素数和半素数.为 ...

  4. OJ#1002 又是a+b

    题目描述: 给定两个正整数a.b(0 < a,b < =10000),求出a+b的和 输入描述: 多组输入,每组输入为一行,里面有2个数a,b(0 < a,b < =10000 ...

  5. yarn配置日志聚合

    [原文地址] 日志聚集是YARN提供的日志中央化管理功能,它能将运行完成的Container/任务日志上传到HDFS上,从而减轻NodeManager负载,且提供一个中央化存储和分析机制.默认情况下, ...

  6. ubuntu安装notepad++

    sudo add-apt-repository ppa:notepadqq-team/notepadqq sudo apt-get update sudo apt-get install notepa ...

  7. c++课设

    #include <stdio.h>#include <time.h>#include <math.h>#define C 60000;struct Student ...

  8. exe电子书制作教程(超详细)【申明:来源于网络】

    exe电子书制作教程(超详细)[申明:来源于网络] 地址:http://wenku.baidu.com/view/0b046907eff9aef8941e0631.html

  9. mysql小细节随笔

    1, MySQL decimal(x,y)  存入根据y的下一位四舍五入,查了半天以为是laravel模型做了预处理,结果发现不是,是mysql decimal类型数据自动处理的,有好,也不好,合并订 ...

  10. 代码块事务—TransactionScope

    今天上班遇到这样的业务:将删除的用户信息记录到记录表,再删除用户表中的信息. 可以说是不幸也可以说是幸运的. 在以往遇到这样的业务,我会考虑到各种出现异常或者失败的情况.在删除一张表数据失败的情况,对 ...