C++读写图片数据转成Base64格式的一种方法
最近在一个项目中要实现在客户端和服务端之间传送图片文件的功能,采用了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格式的一种方法的更多相关文章
- C++读写图片数据转成Base64格式
转载:http://www.cnblogs.com/jeray/p/8746976.html 转载:https://www.cnblogs.com/lujin49/p/4957742.html 转载: ...
- 将图片文件转成BASE64格式
html5Reader (file, item) { const reader = new FileReader() reader.onload = (e) => { this.$set(ite ...
- js如何将选中图片文件转换成Base64字符串?
如何将input type="file"选中的文件转换成Base64的字符串呢? 1.首先了解一下为什么要把图片文件转换成Base64的字符串 在常规的web开发过程中,大部分上传 ...
- CAFFE学习笔记(四)将自己的jpg数据转成lmdb格式
1 引言 1-1 以example_mnist为例,如何加载属于自己的测试集? 首先抛出一个问题:在example_mnist这个例子中,测试集是人家给好了的.那么如果我们想自己试着手写几个数字然后验 ...
- 使用gfortran将数据写成Grads格式的代码示例
使用gfortran将数据写成Grads格式的代码示例: !-----'Fortran4Grads.f90' program Fortran4Grads implicit none integer,p ...
- js 将图片文件转换成base64
1.情景展示 在JavaScript中,如何使用图片文件转换成base64? 2.解决方案 /** * 网络图像文件转Base64 * @param img dom对象 */ function g ...
- 图片文件转换成Base64编码实现ajax提交图片
//上传头像图片 function uploadHead(imgPath) { console.log("imgPath = " + imgPath); var image = n ...
- php 将图片文件转成base64编码的方法
php 将图片文件转成base64编码的方法<pre><?php /** 文件转base64输出 * @param String $file 文件路径 * @return Strin ...
- 利用python将excel数据解析成json格式
利用python将excel数据解析成json格式 转成json方便项目中用post请求推送数据自定义数据,也方便测试: import xlrdimport jsonimport requests d ...
随机推荐
- Mesos:数据库使用的持久化卷
摘要: Mesos为很多不同的用户场景都提供了精妙的,考虑周全的API.持久化卷是由新的acceptOffers API引入的特性.持久化卷让用户可以为Mesos构建数据库框架,Mesos可以在任何不 ...
- 进程池的同步与异步用法Pool
进程池的同步,如下程序: from multiprocessing import Pool import time import os def func(n): print('start 进程 %s' ...
- git纯净提交代码(只提交自己改过的文件)
添加远程仓库,这个远程仓库是要进行发起合并请求的仓库,简单来说就是项目的主要代码库,不是自己派生的代码库 git remote add main http://xxx 从远端仓库下载新分支与数据gi ...
- template.js artTemplate 简洁语法官网下载不了 template.js artTemplate 新下载地址
参考:https://blog.csdn.net/tavatimsa/article/details/82019792
- css 使元素居中
css使元素水平居中 1.对于行内元素的水平居中 给父元素设置text-align:center <div style="text-align:center;">居中显 ...
- Oracle课程档案,第七天
数据库管理 关闭数据库有4中方式: 1.shutdown modes 关机模式 2.shutdown normal 关机正常 3.shutdown immediate 立即关闭 ★★ 常用命令 4.s ...
- Android100【申明:来源于网络】
Android100[申明:来源于网络] 地址:http://www.android100.org/html/201406/11/23770.html
- 个人小爱好:Operating System:three easy pieces---第6章第4节_担心并发问题?
担心并发问题? 微妙,上下文切换大约6微妙.而,现在的系统有着级数级别的提升,在2-3GHz的处理起中消耗只有亚微妙级. 但应该注意到,不是所有的系统性能都跟着CPU性能的提升而提升,根据Ouster ...
- php钩子原理和实现
2017年3月18日17:22:52 php版本 5.6.27 5.3以下和5.3以上的版本在PHP类与对象区别很大,请注意 其实原理很简单,有些人把事情弄的过于发杂,其实就是调用某个目录下的比如/h ...
- 关于vue执行打包后,如何在本地浏览问题
最近一个人在捣鼓vue,写完项目后发现在npm run dev下可以正常访问,bulid之后却一片空白,查看console出现许多Failed to load resource: net::ERR_F ...