📜 ⬆️ ⬇️

Creation of QR codes in C / C ++

image
This is a simple example of using Kentaro's libqrencode library from FUKUCHI to generate a bmp file with a QR code for some text. The Internet is full of links to this library, but not a single example of its use. libqrencode supports the QR Code model 2, described in JIS (Japanese Industrial Standards) X0510: 2004 or ISO / IEC 18004. Currently, ECI and FNC1 QR Code model 1 are not supported.


Foreword


A QR code is a two-dimensional code that can be read by various devices. Today, almost every smartphone has a QRCode reader. This format supports different data types: url, contacts, etc. In the example of this article, the url is encoded, which will lead you to a wikipedia article on QR codes. The most used library for generating codes is libqrencode , but there are libqrencode simple examples of its use, especially in C / C ++. The documentation is just comments in the source code. It was written a simple application that converts the url into a QR code and stores it in a regular bmp file.

Code use


To use the code above, you need to download the library libqrencode , a link to which is at the very beginning of the post and put its source (you can in a separate folder) in the folder with the project. So, a simple Win32 application for converting url into a QR code that can be used in your projects:
 #include <stdio.h> #include <tchar.h> #include <string.h> #include <errno.h> #include <conio.h> #include <ctype.h> #include <stddef.h> #include <stdlib.h> #include <wchar.h> #include "qrencode.h" //by libqrencode #define QRCODE_TEXT "http://ru.wikipedia.org/wiki/QR-%EA%EE%E4" // Text to encode into QRCode #define OUT_FILE "C:/test.bmp" // Output file name #define OUT_FILE_PIXEL_PRESCALER 8 // Number of pixels in bmp file for each QRCode pixel, on each dimension #define PIXEL_COLOR_R 0 // Color of bmp pixels #define PIXEL_COLOR_G 0 #define PIXEL_COLOR_B 0xff typedef unsigned short WORD; typedef unsigned long DWORD; typedef signed long LONG; #define BI_RGB 0L #pragma pack(push, 2) typedef struct { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } BITMAPFILEHEADER; typedef struct { DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } BITMAPINFOHEADER; #pragma pack(pop) int _tmain(int argc, _TCHAR** argv) { char* szSourceSring = QRCODE_TEXT; unsigned int unWidth, x, y, l, n, unWidthAdjusted, unDataBytes; unsigned char* pRGBData, *pSourceData, *pDestData; QRcode* pQRC; FILE* f; /* *    .   *         QR-. * WARNING:   THREAD-UNSAFE,  pthread . * : * string:  ;    * version:  ;  0,       * level:    * hint:    ,       * QR_MODE_KANJI -     Shif-JIS * QR_MODE_8 -        .  UTF-8,    * casesensitive: case-sensitive(1)  (0). * return:   QRcode;    NULL  errno   * EINVAL:    * ENOMEM:        * ERANGE:     */ //  QRcode if (pQRC = QRcode_encodeString(szSourceSring, 0, QR_ECLEVEL_H, QR_MODE_8, 1)) { unWidth = pQRC->width; unWidthAdjusted = unWidth * OUT_FILE_PIXEL_PRESCALER * 3; if (unWidthAdjusted % 4) unWidthAdjusted = (unWidthAdjusted / 4 + 1) * 4; unDataBytes = unWidthAdjusted * unWidth * OUT_FILE_PIXEL_PRESCALER; if (!(pRGBData = (unsigned char*)malloc(unDataBytes))) { printf("Out of memory"); exit(1); } memset(pRGBData, 0xff, unDataBytes); //   bmp BITMAPFILEHEADER kFileHeader; kFileHeader.bfType = 0x4d42; // "BM" kFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nDataBytes; kFileHeader.bfReserved1 = 0; kFileHeader.bfReserved2 = 0; kFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); BITMAPINFOHEADER kInfoHeader; kInfoHeader.biSize = sizeof(BITMAPINFOHEADER); kInfoHeader.biWidth = unWidth * OUT_FILE_PIXEL_PRESCALER; kInfoHeader.biHeight = -((int)unWidth * OUT_FILE_PIXEL_PRESCALER); kInfoHeader.biPlanes = 1; kInfoHeader.biBitCount = 24; kInfoHeader.biCompression = BI_RGB; kInfoHeader.biSizeImage = 0; kInfoHeader.biXPelsPerMeter = 0; kInfoHeader.biYPelsPerMeter = 0; kInfoHeader.biClrUsed = 0; kInfoHeader.biClrImportant = 0; //   QrCode  bmp  pSourceData = pQRC->data; for(y = 0; y < unWidth; y++) { pDestData = pRGBData + unWidthAdjusted * y * OUT_FILE_PIXEL_PRESCALER; for(x = 0; x < unWidth; x++) { if (*pSourceData & 1) for(l = 0; l < OUT_FILE_PIXEL_PRESCALER; l++) for(n = 0; n < OUT_FILE_PIXEL_PRESCALER; n++) { *(pDestData + n * 3 + unWidthAdjusted * l) = PIXEL_COLOR_B; *(pDestData + 1 + n * 3 + unWidthAdjusted * l) = PIXEL_COLOR_G; *(pDestData + 2 + n * 3 + unWidthAdjusted * l) = PIXEL_COLOR_R; } pDestData += 3 * OUT_FILE_PIXEL_PRESCALER; pSourceData++; } } if (!(fopen_s(&f, OUT_FILE, "wb"))) { fwrite(&kFileHeader, sizeof(BITMAPFILEHEADER), 1, f); fwrite(&kInfoHeader, sizeof(BITMAPINFOHEADER), 1, f); fwrite(pRGBData, sizeof(unsigned char), unDataBytes, f); fclose(f); } else { printf("Unable to open file"); exit(1); } free(pRGBData); QRcode_free(pQRC); } else { printf("NULL returned"); exit(1); } return 0; } 

By changing defaults, you can recompile the program to generate different url / output files / pixel sizes / colors of pixels.

')

Source: https://habr.com/ru/post/182638/


All Articles