Click here to Skip to main content
15,886,085 members
Articles / Programming Languages / C

Simple C/C++ QRCode Library

Rate me:
Please Sign up or sign in to vote.
4.93/5 (32 votes)
11 Jun 2013GPL33 min read 338.8K   20K   86   52
How to generate a QRCode with the libqrencode library, with a clear example

Introduction

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.

Background

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:

C++
#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 
			//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
...

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. 

History

  • First release 1.0
  • Second release 1.1, thanks to all those that contributed.
  • Third release 1.2, release version also working

License

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.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
President Ultramundum foundation
Italy Italy
Fulvio Dominci Carnino was born in Torino, Italy, in 1965.
One of the founders of the first italian computer club, he studied electronics in the Turin university. Before graduating he wrote his first videogame, Specventure.
The title sold more than 15000 copies all over Europe, a success for 1985, and madeDominici the first italian ever to sell a videogame on the international market.
In 1986 creates a real-time multitasking operating system: I-Wave.
In 1990 he started his first company for automated test systems.
During his army service developes an advanced neural network artificial creature, creating the theory of bottom-up artificial intelligence.
In the 90s starts a firm for the automated reproduction of ancient paintings by secret computer techniques.
From 1995 to 2002 Dominici is co-founder of the largest italian public network of computers.
From 2001 is president of Ultramundum Foundation.

Comments and Discussions

 
QuestionCompilation Pin
Roshan Jindani17-Jun-13 11:08
Roshan Jindani17-Jun-13 11:08 
AnswerRe: Compilation Pin
Fulvio Dominici17-Jun-13 21:18
Fulvio Dominici17-Jun-13 21:18 
GeneralI would like more information Pin
Salatiel Bairros12-Jun-13 7:36
professionalSalatiel Bairros12-Jun-13 7:36 
GeneralRe: I would like more information Pin
Fulvio Dominici17-Jun-13 21:17
Fulvio Dominici17-Jun-13 21:17 
QuestionHow to support chinese? Pin
andylauxing11-Jun-13 3:36
andylauxing11-Jun-13 3:36 
AnswerRe: How to support chinese? Pin
Fulvio Dominici11-Jun-13 3:57
Fulvio Dominici11-Jun-13 3:57 
GeneralRe: How to support chinese? Pin
andylauxing11-Jun-13 4:44
andylauxing11-Jun-13 4:44 
GeneralRe: How to support chinese? Pin
Fulvio Dominici17-Jun-13 21:19
Fulvio Dominici17-Jun-13 21:19 
try emailing Kentaro Fukuchi <kentaro@fukuchi.org>
GeneralRe: How to support chinese? Pin
Ben Fung14-Mar-14 0:17
Ben Fung14-Mar-14 0:17 
QuestionIt compiles fine Pin
T800G10-Jun-13 10:19
T800G10-Jun-13 10:19 
AnswerRe: It compiles fine Pin
Fulvio Dominici11-Jun-13 0:25
Fulvio Dominici11-Jun-13 0:25 
QuestionError Pin
Sascha 4Go6-Jun-13 9:26
Sascha 4Go6-Jun-13 9:26 
AnswerRe: Error Pin
Fulvio Dominici6-Jun-13 21:41
Fulvio Dominici6-Jun-13 21:41 
QuestionMissing Files Pin
robertmatura3-Jun-13 11:55
robertmatura3-Jun-13 11:55 
AnswerRe: Missing Files Pin
Fulvio Dominici3-Jun-13 22:04
Fulvio Dominici3-Jun-13 22:04 
GeneralRe: Missing Files Pin
robertmatura4-Jun-13 11:46
robertmatura4-Jun-13 11:46 
GeneralRe: Missing Files Pin
Fulvio Dominici4-Jun-13 22:35
Fulvio Dominici4-Jun-13 22:35 
GeneralRe: Missing Files Pin
robertmatura5-Jun-13 4:14
robertmatura5-Jun-13 4:14 
GeneralRe: Missing Files Pin
Fulvio Dominici5-Jun-13 4:49
Fulvio Dominici5-Jun-13 4:49 
GeneralRe: Missing Files Pin
robertmatura5-Jun-13 5:01
robertmatura5-Jun-13 5:01 
GeneralRe: Missing Files Pin
Fulvio Dominici6-Jun-13 0:04
Fulvio Dominici6-Jun-13 0:04 
AnswerSolution to missing files Pin
Member 78048185-Jun-13 5:17
Member 78048185-Jun-13 5:17 
GeneralRe: Solution to missing files Pin
Fulvio Dominici6-Jun-13 0:05
Fulvio Dominici6-Jun-13 0:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.