1. 程式人生 > >C++實現圖片的base64編碼

C++實現圖片的base64編碼

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

<span style="font-size:18px;">#include <string>
using namespace std;

class ZBase64
{
public:
    /*編碼
    DataByte
        [in]輸入的資料長度,以位元組為單位
    */
    string Encode(const unsigned char* Data,int DataByte);
    /*解碼
    DataByte
        [in]輸入的資料長度,以位元組為單位
    OutByte
        [out]輸出的資料長度,以位元組為單位,請不要通過返回值計算
        輸出資料的長度
    */
    string Decode(const char* Data,int DataByte,int& OutByte);
};</span>
ZBase64.cpp
#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'-'9'
        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, // 'A'-'Z'
        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, // 'a'-'z'
    };
    //返回值
    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

<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;                               //Mat 圖片資料轉換為vector<uchar>
	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());     //實現圖片的base64編碼

	cout << imgbase64 << endl;
}</span>

                       

    

參考文獻