bao_yj
2018-03-06 02:56
采纳率: 85.7%
浏览 2.5k

vs2015使用c++对图片与base64格式的相互转换程序

程序过程:
1、首先将本地图片转化为base64格式;
2、再将base64格式的图片转化为图片格式;
3、使用opencv读取并展示此图片。
急用,拜托大家了!!!!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • HaoLu_0805 2018-03-06 06:14
    已采纳

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

    打赏 评论
  • a1134075691 2018-03-06 03:18
    打赏 评论
  • bao_yj 2018-03-09 02:44

    http://blog.csdn.net/hei_ya/article/details/51637929
    现将base64解码,再将string转换为Mat即可,而不是将BYTE*转换为Mat.

    打赏 评论

相关推荐 更多相似问题