GTK使用开源二维码生成库libqrencode生成二维码

⌚Time: 2022-11-13 00:59:03

👨‍💻Author: Jack Ge

libqrencode下载地址

https://fukuchi.org/works/qrencode

使用QRcode_encodeString()函数生成二维码数据


QRcode_encodeString()

QRcode* QRcode_encodeString (   const char *    string,

int     version,

QRecLevel   level,

QRencodeMode    hint,

int     casesensitive 

)       

Create a symbol from the string.



The library automatically parses the input string and encodes in a QR Code symbol.



Warning

This function is THREAD UNSAFE when pthread is disabled.

Parameters

string  input string. It must be NUL terminated.

version version of the symbol. If 0, the library chooses the minimum version for the given input data.

level   error correction level.

hint    tell the library how Japanese Kanji characters should be encoded. If QR_MODE_KANJI is given, the library assumes that the given string contains Shift-JIS characters and encodes them in Kanji-mode. If QR_MODE_8 is given, all of non-alphanumerical characters will be encoded as is. If you want to embed UTF-8 string, choose this. Other mode will cause EINVAL error.

casesensitive   case-sensitive(1) or not(0).

Returns

an instance of QRcode class. The version of the result QRcode may be larger than the designated version. On error, NULL is returned, and errno is set to indicate the error. See Exceptions for the details.

Exceptions

EINVAL  invalid input object.

ENOMEM  unable to allocate memory for input objects.

ERANGE  input data is too large.

函数返回一个QRcode结构体


QRcode class. More...



#include <qrencode.h>



Data Fields

int     version

    version of the symbol More...

 

int     width

    width of the symbol More...

 

unsigned char *     data

    symbol data More...

其中width成员表示的是二维码宽度和高度,data是1表示二维码暗色,0表示亮色

data的长度应该是width x width,代表整个二维码的信息序列

实现思路:

建立一个guchar的数组,大小是width x 3 x width,代表图像的所有rgb数据。

使用gtk的gdk_pixbuf_new_from_data函数从该数组创建一个3通道的图像缓冲区

使用gtk_image_new_from_pixbuf从图像缓冲区建立图像

代码


#include <gtk/gtk.h>

#include <qrencode.h>

int main(){



    gtk_init(NULL, NULL);

    GtkWidget *window;

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);

    gtk_window_set_title(GTK_WINDOW(window), "Demo");

    

    QRcode *qrCode;

    //生成二维码

    qrCode = QRcode_encodeString("123abc", 0, QR_ECLEVEL_H, QR_MODE_8, 1);

    //获取二维码宽度

    int width = qrCode->width;

    //用于储存图像数据的指针

    guchar *rgbBuf = new guchar[width*3*width];

    int pos=0;

    //建立二维码数据指针

    unsigned char *qrData = qrCode->data;

    //生成rgb图像数据

    for(int i=0;i<width;i++){

        for(int j=0;j<width;j++){

            if(*(qrData)&1){//1是二维码颜色

                //3通道rgb

                rgbBuf[pos*3+0] = 0;//r

                rgbBuf[pos*3+1] = 0;//g

                rgbBuf[pos*3+2] = 0;//b

            }else{//0是二维码背景色

                rgbBuf[pos*3+0] = 255;//r

                rgbBuf[pos*3+1] = 255;//g

                rgbBuf[pos*3+2] = 255;//b

            }

            pos++;

            qrData++;

        }



    }

    //生成pixbuf图像缓冲区

    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data(rgbBuf

            , GDK_COLORSPACE_RGB

            , false

            , 8

            , width

            , width

            , width*3

            , NULL

            , NULL);

    //调整图像大小

    GdkPixbuf *dst =  gdk_pixbuf_scale_simple(pixbuf,150,150,GDK_INTERP_BILINEAR);

    //生成图像

    GtkWidget *image = gtk_image_new_from_pixbuf(dst);

    //释放资源

    g_object_unref(dst);

    g_object_unref(pixbuf);

    delete []rgbBuf;

    QRcode_free(qrCode);    

    

    //显示图像

    gtk_container_add(GTK_CONTAINER(window),image);

    gtk_widget_show_all(window);

    g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);

    gtk_main();

    return 0;

}


编译链接参数


g++ demoGenerateQRCode.cpp -mms-bitfields -ID:/libs/gtk+-bundle-2.12.11-20080720/include/gtk-2.0 -ID:/libs/gtk+-bundle-2.12.11-20080720/lib/gtk-2.0/include -ID:/libs/gtk+-bundle-2.12.11-20080720/include/atk-1.0 -ID:/libs/gtk+-bundle-2.12.11-20080720/include/cairo -ID:/libs/gtk+-bundle-2.12.11-20080720/include/pango-1.0 -ID:/libs/gtk+-bundle-2.12.11-20080720/include/glib-2.0 -ID:/libs/gtk+-bundle-2.12.11-20080720/lib/glib-2.0/include -ID:/libs/gtk+-bundle-2.12.11-20080720/include/libpng12  -LD:/libs/gtk+-bundle-2.12.11-20080720/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -lintl -ID:/libs/QRencode/include  -LD:/libs/QRencode/lib -lqrencode

结果