最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)圖片轉(zhuǎn)base64的示例代碼

 更新時(shí)間:2024年04月20日 15:14:28   作者:hei_ya  
Base64就是一種 基于64個(gè)可打印字符來表示二進(jìn)制數(shù)據(jù)的表示方法,本文主要為大家詳細(xì)介紹了如何使用C++實(shí)現(xiàn)圖片轉(zhuǎn)base64,需要的可以參考下

1.base64編碼的原因

網(wǎng)絡(luò)傳送渠道并不支持所有的字節(jié),例如傳統(tǒng)的郵件只支持可見字符的傳送,像ASCII碼的控制字符就不能通過郵件傳送。這樣用途就受到了很大的限制,比如圖片二進(jìn)制流的每個(gè)字節(jié)不可能全部是可見字符,所以就傳送不了。最好的方法就是在不改變傳統(tǒng)協(xié)議的情 況下,做一種擴(kuò)展方案來支持二進(jìn)制文件的傳送。把不可打印的字符也能用可打印字符來表示,問題就解決了。Base64編碼應(yīng)運(yùn)而生,Base64就是一種 基于64個(gè)可打印字符來表示二進(jìn)制數(shù)據(jù)的表示方法。

2.base64編碼原理

Base64編碼的思想是是采用64個(gè)基本的ASCII碼字符對(duì)數(shù)據(jù)進(jìn)行重新編碼。它將需要編碼的數(shù)據(jù)拆分成字節(jié)數(shù)組。以3個(gè)字節(jié)為一組。按順序排列24 位數(shù)據(jù),再把這24位數(shù)據(jù)分成4組,即每組6位。再在每組的的最高位前補(bǔ)兩個(gè)0湊足一個(gè)字節(jié)。這樣就把一個(gè)3字節(jié)為一組的數(shù)據(jù)重新編碼成了4個(gè)字節(jié)。當(dāng)所要編碼的數(shù)據(jù)的字節(jié)數(shù)不是3的整倍數(shù),也就是說在分組時(shí)最后一組不夠3個(gè)字節(jié)。這時(shí)在最后一組填充1到2個(gè)0字節(jié)。

3.實(shí)現(xiàn)代碼

ZBase64.h

#include string
using namespace std;
 
class ZBase64
{
public:
    /*編碼
    DataByte
        [in]輸入的數(shù)據(jù)長(zhǎng)度,以字節(jié)為單位
    */
    string Encode(const unsigned char* Data,int DataByte);
    /*解碼
    DataByte
        [in]輸入的數(shù)據(jù)長(zhǎng)度,以字節(jié)為單位
    OutByte
        [out]輸出的數(shù)據(jù)長(zhǎng)度,以字節(jié)為單位,請(qǐng)不要通過返回值計(jì)算
        輸出數(shù)據(jù)的長(zhǎng)度
    */
    string Decode(const char* Data,int DataByte,int& OutByte);
};

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;}
    }
    //對(duì)剩余數(shù)據(jù)進(jìn)行編碼
    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;
}

4.使用示例(結(jié)合opencv)

main.cpp

#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 圖片數(shù)據(jù)轉(zhuǎn)換為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());     //實(shí)現(xiàn)圖片的base64編碼
 
	cout << imgbase64 << endl;
}

5.效果圖      

6.方法補(bǔ)充

除了上文的方法,小編還為大家整理了其他C++實(shí)現(xiàn)圖片轉(zhuǎn)base64的方法,希望對(duì)大家有所幫助

方法一

//++Base64.h

#pragma once

class CBase64
{
public:
public:
    CBase64();
    ~CBase64();

    /*編碼
    DataByte
    [in]輸入的數(shù)據(jù)長(zhǎng)度,以字節(jié)為單位
    */
    std::string Encode(const char* Data, int DataByte);

    /*解碼
    DataByte
    [in]輸入的數(shù)據(jù)長(zhǎng)度,以字節(jié)為單位
    OutByte
    [out]輸出的數(shù)據(jù)長(zhǎng)度,以字節(jié)為單位,請(qǐng)不要通過返回值計(jì)算
    輸出數(shù)據(jù)的長(zhǎng)度
    */
    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[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; }
    }
    //對(duì)剩余數(shù)據(jù)進(jìn)行編碼
    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;
}

std::string CBase64::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;
}

以下是讀寫圖片的調(diào)用代碼:
bool CBusinessDataMgr::ReadPhotoFile(std::basic_string<TCHAR> strFileName,std::string &strData)
{
    HANDLE hFile;
    hFile = CreateFile(strFileName.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile == INVALID_HANDLE_VALUE)
    {
        return false;
    }

    DWORD dFileSize = GetFileSize(hFile, NULL);
    char * pBuffer = new char[dFileSize + 1];

    if(pBuffer == NULL)
        return false;

    memset(pBuffer, 0, dFileSize);

    DWORD dReadSize(0);
    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, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile == INVALID_HANDLE_VALUE)
    {
        return false;
    }

    CBase64 base64;
    int datalen(0);
    DWORD dwritelen(0);
    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;
}

方法二

#include <string>
#include <iostream>

static std::string base64Decode(const char* Data, int DataByte) {
    //解碼表
    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'
    };
    std::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;
            if (*Data != '=') {
                nValue += DecodeTable[*Data++] << 6;
                strDecode += (nValue & 0x0000FF00) >> 8;
                if (*Data != '=') {
                    nValue += DecodeTable[*Data++];
                    strDecode += nValue & 0x000000FF;
                }
            }
            i += 4;
        }
        else {
            Data++;
            i++;
        }
    }
    return strDecode;
}
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string base64_encode(const char* bytes_to_encode, unsigned int in_len)
{
    std::string ret;
    int i = 0;
    int j = 0;
    unsigned char char_array_3[3];
    unsigned char char_array_4[4];

    while (in_len--)
    {
        char_array_3[i++] = *(bytes_to_encode++);
        if (i == 3)
        {
            char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
            char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
            char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
            char_array_4[3] = char_array_3[2] & 0x3f;
            for (i = 0; (i < 4); i++)
            {
                ret += base64_chars[char_array_4[i]];
            }
            i = 0;
        }
    }
    if (i)
    {
        for (j = i; j < 3; j++)
        {
            char_array_3[j] = '\0';
        }

        char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
        char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
        char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
        char_array_4[3] = char_array_3[2] & 0x3f;

        for (j = 0; (j < i + 1); j++)
        {
            ret += base64_chars[char_array_4[j]];
        }

        while ((i++ < 3))
        {
            ret += '=';
        }

    }
    return ret;
}
int main(){
    std::fstream f;
    f.open("2.jpeg", std::ios::in | std::ios::binary);
    f.seekg(0, std::ios_base::end);     //設(shè)置偏移量至文件結(jié)尾
    std::streampos sp = f.tellg();      //獲取文件大小
    int size = sp;
    char* buffer = (char*)malloc(sizeof(char) * size);
    f.seekg(0, std::ios_base::beg);     //設(shè)置偏移量至文件開頭
    f.read(buffer, size);                //將文件內(nèi)容讀入buffer
    std::string imgBase64 = base64_encode(buffer, size);    
    std::string s_mat = base64Decode(imgBase64.c_str(), imgBase64.size());
    FILE* stream;
    if ((stream = fopen("1.JPG", "wb")) != NULL)
    {
        int numwritten = fwrite(s_mat.data(), sizeof(char), s_mat.size(), stream);
        fclose(stream);
    }     //編碼
}

到此這篇關(guān)于C++實(shí)現(xiàn)圖片轉(zhuǎn)base64的示例代碼的文章就介紹到這了,更多相關(guān)C++圖片轉(zhuǎn)base64內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++ opencv霍夫圓檢測(cè)使用案例詳解

    C++ opencv霍夫圓檢測(cè)使用案例詳解

    這篇文章主要介紹了C++ opencv霍夫圓檢測(cè)使用案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 聊聊C++ 運(yùn)算符重載知識(shí)

    聊聊C++ 運(yùn)算符重載知識(shí)

    運(yùn)算符重載是一種形式的C++多態(tài),重載運(yùn)算符可以使代碼看起來更加自然,下面通過例子介紹下C++ 運(yùn)算符重載知識(shí),感興趣的朋友一起看看吧
    2021-11-11
  • VC枚舉串口端口應(yīng)用

    VC枚舉串口端口應(yīng)用

    這篇文章主要介紹了VC枚舉串口端口應(yīng)用,羅列了常見的一些串口端口的應(yīng)用實(shí)例,需要的朋友可以參考下
    2014-10-10
  • 如何利用C語言實(shí)現(xiàn)最簡(jiǎn)單的HTTP服務(wù)器詳解

    如何利用C語言實(shí)現(xiàn)最簡(jiǎn)單的HTTP服務(wù)器詳解

    這篇文章主要給大家介紹了關(guān)于如何利用C語言實(shí)現(xiàn)最簡(jiǎn)單的HTTP服務(wù)器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C語言具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • C++初級(jí)線程管理

    C++初級(jí)線程管理

    這篇文章主要介紹了C++初級(jí)線程管理,C++11中提供了std::thread庫(kù),本文將從線程的啟動(dòng)、線程等待、線程分離、線程傳參、線程識(shí)別等幾個(gè)方面介紹初級(jí)線程管理的知識(shí),需要的朋友可以參考一下
    2021-12-12
  • C++實(shí)踐分?jǐn)?shù)類中運(yùn)算符重載的方法參考

    C++實(shí)踐分?jǐn)?shù)類中運(yùn)算符重載的方法參考

    今天小編就為大家分享一篇關(guān)于C++實(shí)踐分?jǐn)?shù)類中運(yùn)算符重載的方法參考,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • C++簡(jiǎn)明圖解分析靜態(tài)成員與單例設(shè)計(jì)模式

    C++簡(jiǎn)明圖解分析靜態(tài)成員與單例設(shè)計(jì)模式

    與靜態(tài)數(shù)據(jù)成員不同,靜態(tài)成員函數(shù)的作用不是為了對(duì)象之間的溝通,而是為了能處理靜態(tài)數(shù)據(jù)成員,靜態(tài)成員函數(shù)沒有this指針。既然它沒有指向某一對(duì)象,也就無法對(duì)一個(gè)對(duì)象中的非靜態(tài)成員進(jìn)行默認(rèn)訪問
    2022-06-06
  • C++利用隨機(jī)策略實(shí)現(xiàn)優(yōu)化二叉樹操作效率

    C++利用隨機(jī)策略實(shí)現(xiàn)優(yōu)化二叉樹操作效率

    這篇文章中我們主要來詳細(xì)探討隨機(jī)化二叉搜索樹的基本思想、實(shí)現(xiàn)方法,以及如何在C++中應(yīng)用這些策略來優(yōu)化我們的數(shù)據(jù)結(jié)構(gòu),感興趣的可以了解下
    2024-02-02
  • C++使用文件實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)

    C++使用文件實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++使用文件實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • 深入理解C++函數(shù)棧幀

    深入理解C++函數(shù)棧幀

    本文主要介紹了C++函數(shù)棧幀,詳細(xì)的介紹了C++函數(shù)棧幀的概念以及使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評(píng)論

牙克石市| 乐安县| 旺苍县| 靖边县| 武乡县| 册亨县| 巴林右旗| 綦江县| 鄂州市| 石柱| 蕲春县| 思茅市| 玉环县| 改则县| 沙雅县| 黑河市| 兴宁市| 新和县| 客服| 玉田县| 连平县| 淳安县| 明光市| 无锡市| 类乌齐县| 眉山市| 都昌县| 宜都市| 吴江市| 共和县| 尉氏县| 兰溪市| 临城县| 增城市| 阳谷县| 海安县| 广安市| 天气| 神农架林区| 深泽县| 巴里|