生活随笔
收集整理的這篇文章主要介紹了
C++实现图片的base64编码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
from:http://blog.csdn.net/hei_ya/article/details/51637929
1.base64編碼的原因
網絡傳送渠道并不支持所有的字節,例如傳統的郵件只支持可見字符的傳送,像ASCII碼的控制字符就不能通過郵件傳送。這樣用途就受到了很大的限制,比如圖片二進制流的每個字節不可能全部是可見字符,所以就傳送不了。最好的方法就是在不改變傳統協議的情 況下,做一種擴展方案來支持二進制文件的傳送。把不可打印的字符也能用可打印字符來表示,問題就解決了。Base64編碼應運而生,Base64就是一種 基于64個可打印字符來表示二進制數據的表示方法。
2.base64編碼原理
?Base64編碼的思想是是采用64個基本的ASCII碼字符對數據進行重新編碼。它將需要編碼的數據拆分成字節數組。以3個字節為一組。按順序排列24 位數據,再把這24位數據分成4組,即每組6位。再在每組的的最高位前補兩個0湊足一個字節。這樣就把一個3字節為一組的數據重新編碼成了4個字節。當所要編碼的數據的字節數不是3的整倍數,也就是說在分組時最后一組不夠3個字節。這時在最后一組填充1到2個0字節。
ZBase64.h
[cpp] view plaincopy
<span?style="font-size:18px;">#include?<string>??using?namespace?std;????class?ZBase64??{??public:???????????????string?Encode(const?unsigned?char*?Data,int?DataByte);??????????????????string?Decode(const?char*?Data,int?DataByte,int&?OutByte);??};</span>??
ZBase64.cpp
[cpp] view plaincopy
#include?"ZBase64.h"????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,?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,???????????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,???????};????????????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;??}??
3.使用示例(結合OpenCV)
main.cpp
[cpp] view plaincopy
<span?style="font-size:18px;">#include<opencv2/opencv.hpp>??#include<iostream>??#include"ZBase64.h"??#include<vector>??????using?namespace?std;??using?namespace?cv;????void?main()??{??????Mat?img?=?imread("1.bmp");????????vector<uchar>?vecImg;?????????????????????????????????????vector<int>?vecCompression_params;??????vecCompression_params.push_back(CV_IMWRITE_JPEG_QUALITY);??????vecCompression_params.push_back(90);??????imencode(".jpg",?img,?vecImg,?vecCompression_params);????????ZBase64?base64;??????string?imgbase64?=?base64.Encode(vecImg.data(),?vecImg.size());?????????????cout?<<?imgbase64?<<?endl;??}</span>??
? ? ? ? ? ? ? ?? ? ? ??
? ??
參考文獻
【1】http://www.cnblogs.com/chengxiaohui/articles/3951129.html
【2】http://www.ithao123.cn/content-18
【3】http://www.cppblog.com/wanghaiguang/archive/2013/03/20/198626.aspx
總結
以上是生活随笔為你收集整理的C++实现图片的base64编码的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。