基于opencv对图片进行base64格式的编解码

首先base64,了解一下:Base64就是一种基于64个可打印字符来表示二进制数据的方法。
表示二进制数,所以我得先让图片转化为二进制数据,然后对二进制数据进行base64编码;
或者先对base64字符串进行解码,然后再把二进制数据转化为图片保存或展示。

流程

  • 编码
    1. cv::Mat图片 -> 二进制数据
bool imencode(const string& ext, InputArray img, vector& buf, const vector& params=vector())
//ext:  图片后缀名,如".jpg"或".png"
//img:  需要进行相关操作的图片
//buf:  输出二进制数据到该缓存。

//params:格式相关的参数
//params中的每个参数成对出现,即paramId_1, paramValue_1, paramId_2, paramValue_2, … ,当前支持如下参数:
//JPEG:压缩质量 ( CV_IMWRITE_JPEG_QUALITY ),从0到100(数值越高质量越好),默认值为95。
//PNG:  compression level ( CV_IMWRITE_PNG_COMPRESSION ) 从0到9。 数值越高,文件大小越小,压缩时间越长。默认值为3。
//PPM,  PGM, or PBM:二进制标志 ( CV_IMWRITE_PXM_BINARY ),0 或 1。默认值为1。

2.二进制数据 -> base64字符串
网上有很多的 base64编解码 源码,本文末会添加我使用的源码供参考。


附:

/*****test.cpp*****/
#include <iostream>
#include <opencv.hpp>
#include <vector>
#include <fstream>
#include "base64.h"
using namespace cv;
using namespace std;
void write(string encode_str);
string read(string fileName);
string encode();
Mat decode();

int main() {    
    Mat img = decode();
    imshow("test", img);
    if (waitKey(1) == 27)
        return 0;
    system("pause");
    return 0;
}

/***编码得到的 base64字符串 写入 “base64_str” 保存***/
void write(string encode_str)
{
    ofstream out;
    out.open("base64_str", ios::out | ios::trunc);
    out << encode_str << endl;
    out.close();
}

/***读取指定文件保存的 base64 字符串并返回***/
string read(string fileName)
{
    string base64_str;
    ifstream in;
    in.open(fileName, ios::in);
    in >> base64_str;
    cout << "base64_str.length() = " << base64_str.length() << endl;
    in.close();
    return base64_str;
}

/***编码***/
string encode()
{
    string img_path("img.jpg");
    Mat img;
    img = imread(img_path);
    if (img.empty())
    {
        cout << "img is empty" << endl;
        return "Failed";
    }

    vector<uchar> img_data;
    imencode(".jpg", img, img_data);
    string str_Encode(img_data.begin(), img_data.end());
    write(str_Encode);
}

/***解码***/
Mat decode()
{
    string encode_str = read("dog.base");
    string decode_str;

    //这里可以看到,我调用的第三方库支持std::string类的参数形式
    Base64::Decode(encode_str, &decode_str);
    vector<uchar> img_data(decode_str.begin(), decode_str.end());
    Mat img = imdecode(Mat(img_data), CV_LOAD_IMAGE_COLOR);
    return img;
}

关于base64解码,碰到一个问题:
借助在线编码器,将以下图片:
这里写图片描述
转为base64字符串:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGB。。。。。。。。。
如果需要解析字符串,加粗部分需要删除,在未删除时,执行程序提示:
这里写图片描述
删除后,运行正常:
这里写图片描述


刚刚发现csdn下载如果上传资源不能免费 0.0

/*****base64.h*****/
#ifndef BASE64_H
#define BASE64_H

#include <string>

const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

class Base64 {
public:
    static bool Encode(const std::string &in, std::string *out) {
        int i = 0, j = 0;
        size_t enc_len = 0;
        unsigned char a3[3];
        unsigned char a4[4];

        out->resize(EncodedLength(in));

        int input_len = in.size();
        std::string::const_iterator input = in.begin();

        while (input_len--) {
            a3[i++] = *(input++);
            if (i == 3) {
                a3_to_a4(a4, a3);

                for (i = 0; i < 4; i++) {
                    (*out)[enc_len++] = kBase64Alphabet[a4[i]];
                }

                i = 0;
            }
        }

        if (i) {
            for (j = i; j < 3; j++) {
                a3[j] = '\0';
            }

            a3_to_a4(a4, a3);

            for (j = 0; j < i + 1; j++) {
                (*out)[enc_len++] = kBase64Alphabet[a4[j]];
            }

            while ((i++ < 3)) {
                (*out)[enc_len++] = '=';
            }
        }

        return (enc_len == out->size());
    }

    static bool Encode(const char *input, size_t input_length, char *out, size_t out_length) {
        int i = 0, j = 0;
        char *out_begin = out;
        unsigned char a3[3];
        unsigned char a4[4];

        size_t encoded_length = EncodedLength(input_length);

        if (out_length < encoded_length) return false;

        while (input_length--) {
            a3[i++] = *input++;
            if (i == 3) {
                a3_to_a4(a4, a3);

                for (i = 0; i < 4; i++) {
                    *out++ = kBase64Alphabet[a4[i]];
                }

                i = 0;
            }
        }

        if (i) {
            for (j = i; j < 3; j++) {
                a3[j] = '\0';
            }

            a3_to_a4(a4, a3);

            for (j = 0; j < i + 1; j++) {
                *out++ = kBase64Alphabet[a4[j]];
            }

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

        return (out == (out_begin + encoded_length));
    }

    static bool Decode(const std::string &in, std::string *out) {
        int i = 0, j = 0;
        size_t dec_len = 0;
        unsigned char a3[3];
        unsigned char a4[4];

        int input_len = in.size();
        std::string::const_iterator input = in.begin();

        out->resize(DecodedLength(in));

        while (input_len--) {
            if (*input == '=') {
                break;
            }

            a4[i++] = *(input++);
            if (i == 4) {
                for (i = 0; i < 4; i++) {
                    a4[i] = b64_lookup(a4[i]);
                }

                a4_to_a3(a3, a4);

                for (i = 0; i < 3; i++) {
                    (*out)[dec_len++] = a3[i];
                }

                i = 0;
            }
        }

        if (i) {
            for (j = i; j < 4; j++) {
                a4[j] = '\0';
            }

            for (j = 0; j < 4; j++) {
                a4[j] = b64_lookup(a4[j]);
            }

            a4_to_a3(a3, a4);

            for (j = 0; j < i - 1; j++) {
                (*out)[dec_len++] = a3[j];
            }
        }

        return (dec_len == out->size());
    }

    static bool Decode(const char *input, size_t input_length, char *out, size_t out_length) {
        int i = 0, j = 0;
        char *out_begin = out;
        unsigned char a3[3];
        unsigned char a4[4];

        size_t decoded_length = DecodedLength(input, input_length);

        if (out_length < decoded_length) return false;

        while (input_length--) {
            if (*input == '=') {
                break;
            }

            a4[i++] = *(input++);
            if (i == 4) {
                for (i = 0; i < 4; i++) {
                    a4[i] = b64_lookup(a4[i]);
                }

                a4_to_a3(a3, a4);

                for (i = 0; i < 3; i++) {
                    *out++ = a3[i];
                }

                i = 0;
            }
        }

        if (i) {
            for (j = i; j < 4; j++) {
                a4[j] = '\0';
            }

            for (j = 0; j < 4; j++) {
                a4[j] = b64_lookup(a4[j]);
            }

            a4_to_a3(a3, a4);

            for (j = 0; j < i - 1; j++) {
                *out++ = a3[j];
            }
        }

        return (out == (out_begin + decoded_length));
    }

    static int DecodedLength(const char *in, size_t in_length) {
        int numEq = 0;

        const char *in_end = in + in_length;
        while (*--in_end == '=') ++numEq;

        return ((6 * in_length) / 8) - numEq;
    }

    static int DecodedLength(const std::string &in) {
        int numEq = 0;
        int n = in.size();

        for (std::string::const_reverse_iterator it = in.rbegin(); *it == '='; ++it) {
            ++numEq;
        }

        return ((6 * n) / 8) - numEq;
    }

    inline static int EncodedLength(size_t length) {
        return (length + 2 - ((length + 2) % 3)) / 3 * 4;
    }

    inline static int EncodedLength(const std::string &in) {
        return EncodedLength(in.length());
    }

    inline static void StripPadding(std::string *in) {
        while (!in->empty() && *(in->rbegin()) == '=') in->resize(in->size() - 1);
    }

private:
    static inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
        a4[0] = (a3[0] & 0xfc) >> 2;
        a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
        a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
        a4[3] = (a3[2] & 0x3f);
    }

    static inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {
        a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
        a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
        a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
    }

    static inline unsigned char b64_lookup(unsigned char c) {
        if (c >= 'A' && c <= 'Z') return c - 'A';
        if (c >= 'a' && c <= 'z') return c - 71;
        if (c >= '0' && c <= '9') return c + 4;
        if (c == '+') return 62;
        if (c == '/') return 63;
        return 255;
    }
};

#endif // BASE64_H

参考:OpenCV学习笔记(一):读取、显示、保存图片

猜你喜欢

转载自blog.csdn.net/guo_lei_lamant/article/details/80592120