Qrencode生成二维码并通过SDL显示

主要是在各种PI上面通过SDL显示二维码 ...
// 安装 
apt-get install libqrencode-dev // 会自动关联安装 libqrencode
// 这些都是摘抄于官方源码包中 ...
https://github.com/fukuchi/libqrencode
https://github.com/fukuchi/libqrencode/blob/master/tests/view_qrcode.c
// L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
// 这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比。

// 实现字符串转二维码 ...
QRcode *qrcode = QRcode_encodeString(m_data.toStdString().c_str(), 2, QR_ECLEVEL_L, QR_MODE_8, 1);

int margin = 1; // 控制生成二维码的空白区域大小
int size = 4; // 控制生成图片的大小,默认为 4
int colorize = 0;

int width = (qrcode->width + margin * 2) * size;
SDL_Surface *m_surface = SDL_CreateRGBSurface(0, width, width, 32, 0, 0, 0, 0);
SDL_FillRect(m_surface, nullptr, SDL_MapRGBA(m_surface->format, 255, 255, 255, 255));

int x, y;
unsigned char *p;
SDL_Rect rect;
Uint32 color[8];
int col;

color[0] = SDL_MapRGBA(m_surface->format, 255, 255, 255, 255);
color[1] = SDL_MapRGBA(m_surface->format,   0,   0,   0, 255);
color[2] = SDL_MapRGBA(m_surface->format, 192, 192, 255, 255);
color[3] = SDL_MapRGBA(m_surface->format,   0,   0,  64, 255);
color[4] = SDL_MapRGBA(m_surface->format, 255, 255, 192, 255);
color[5] = SDL_MapRGBA(m_surface->format,  64,  64,   0, 255);
color[6] = SDL_MapRGBA(m_surface->format, 255, 192, 192, 255);
color[7] = SDL_MapRGBA(m_surface->format,  64,   0,   0, 255);

int ox = 0;
int oy = 0;
ox += margin * size;
oy += margin * size;
width = qrcode->width;
p = qrcode->data;
for(y = 0; y < width; y++) {
    for(x = 0; x < width; x++) {
    rect.x = ox + x * size;
    rect.y = oy + y * size;
    rect.w = size;
    rect.h = size;
    if(!colorize) {
        col = 0;
    } else {
        if(*p & 0x80) {
        col = 6;
        } else if(*p & 0x02) {
        col = 4;
        } else {
        col = 2;
        }
    }
    col += (*p & 1);
    SDL_FillRect(m_surface, &rect, color[col]);
    p++;
    }
}

// Surface to Texture ...
SDL_Texture *m_texture = SDL_CreateTextureFromSurface(_r, m_surface);

// 释放 ...
SDL_DestroyTexture(m_texture);
SDL_FreeSurface(m_surface);
QRcode_free(qrcode);