This is a simple example of usage of the libqrencode library from FUKUCHI Kentaro to generate a bmp file with the QRCode of a specific text. There are a number of references online to this library but no simple example of how to use it. In this project, there is also a workaround to the problems that arise when the library is used normally: here the source codes are directly included so that no linker conflict arises.
Libqrencode supports QR Code model 2, described in JIS (Japanese Industrial Standards) X0510:2004 or ISO/IEC 18004. Currently, the following features are not supported: ECI and FNC1 mode QR Code model 1.
QRCodes are bidimensional codes that can be read by a number of different devices. Today most of the smartphones have a QRCode reader. This format can support a number of different data types: URLs, contacts, ... In this example, an URL is encoded and you can read it with your smartphone that will automatically connect you with the Ultramundum Foundation web site. The most used library for QRCode generation is Libqrencode, but simple examples of its usage, especially in C/C++, are not available. The documentation is made only by comments in the source code and useless Doxygen stuff. I created a simple application that converts a URL into a QRCode that is saved into a plain Windows bmp file. By reading this code, people that would like to use the library, even in other languages will learn how to use it and how to convert its internal bitmap format into a standard graphical representation.
Using the Code
The code is really simple: there is a main source called QRGenerator.cpp with a standard C
main() function. The sample application is a Win32 Console one. Changing the
#defines, it is possible to recompile the program to generate different URls/output files/pixels dimensions/pixels colors:
#define QRCODE_TEXT "http://www.ultramundum.org/index.htm" // Text to encode into QRCode
#define OUT_FILE "C:/test.bmp" // Output file name
#define OUT_FILE_PIXEL_PRESCALER 8 // Prescaler (number of pixels
#define PIXEL_COLOR_R 0 // Color of bmp pixels
#define PIXEL_COLOR_G 0
#define PIXEL_COLOR_B 0xff
Points of Interest
At first, I tried to use the library as a standard C one, but the linker kept firing errors on conflicting functions; so I directly included the source code into the project and compiled everything as a whole.
- First release 1.0
- Second release 1.1, thanks to all those that contributed.
- Third release 1.2, release version also working
libqrencode library is Copyright (C) 2006-2012 Kentaro Fukuchi
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
QR Code is registered trademarks of DENSO WAVE INCORPORATED in JAPAN and other countries.
Reed-Solomon code encoder is written by Phil Karn, KA9Q.
Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q
RPM packages are done by Katsumi Saito.