Click here to Skip to main content
15,881,248 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.5K   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

 
QuestionFNC1 Pin
Member 934835228-Sep-20 15:30
Member 934835228-Sep-20 15:30 
GeneralMy vote of 5 Pin
professor tourneseul13-Apr-20 7:47
professor tourneseul13-Apr-20 7:47 
GeneralRe: My vote of 5 Pin
Nelek13-Apr-20 7:51
protectorNelek13-Apr-20 7:51 
QuestionEXE file error Pin
Member 140807396-Dec-18 19:24
Member 140807396-Dec-18 19:24 
QuestionI have one question. Pin
Kwon Oh Chang15-Nov-18 19:41
Kwon Oh Chang15-Nov-18 19:41 
QuestionBorland c++ builder Pin
Member 84314158-Dec-17 11:20
Member 84314158-Dec-17 11:20 
AnswerRe: Borland c++ builder Pin
professor tourneseul13-Apr-20 8:41
professor tourneseul13-Apr-20 8:41 
QuestionQr code model 1 ? Pin
Member 1353285720-Nov-17 23:12
Member 1353285720-Nov-17 23:12 
QuestionQR scanner or reader written in C Pin
Member 1239744816-Mar-16 15:07
Member 1239744816-Mar-16 15:07 
QuestionHeap problem Pin
kolio_gg29-Sep-15 3:53
kolio_gg29-Sep-15 3:53 
GeneralMy vote of 3 Pin
Richard Chambers3-Oct-14 19:18
Richard Chambers3-Oct-14 19:18 
Questionbmp file to loadimage PinPopular
Member 1075329316-Apr-14 1:11
Member 1075329316-Apr-14 1:11 
AnswerRe: bmp file to loadimage Pin
Branimir Piškor6-Feb-17 1:42
Branimir Piškor6-Feb-17 1:42 
GeneralRe: bmp file to loadimage Pin
professor tourneseul9-Aug-18 19:24
professor tourneseul9-Aug-18 19:24 
QuestionUnresolved External Symbol Pin
raniam16-Mar-14 21:13
raniam16-Mar-14 21:13 
QuestionRe: Unresolved External Symbol Pin
Member 1097099625-Jul-14 3:42
Member 1097099625-Jul-14 3:42 
AnswerRe: Unresolved External Symbol Pin
professor tourneseul8-Aug-18 21:03
professor tourneseul8-Aug-18 21:03 
QuestionERROR not declared DEscriptors Pin
SepBen24-Feb-14 22:28
SepBen24-Feb-14 22:28 
QuestionQREncode: How to convert QrCode bits to bmp pixels of RGB565 format? Pin
dhivys20-Feb-14 19:43
dhivys20-Feb-14 19:43 
BugNice! but Pin
dven5egr1-Oct-13 3:30
dven5egr1-Oct-13 3:30 
QuestionCapacity of storage characters? Pin
Carlos Alberto Morales Osorio21-Aug-13 8:51
professionalCarlos Alberto Morales Osorio21-Aug-13 8:51 
AnswerRe: Capacity of storage characters? Pin
Fulvio Dominici12-Sep-13 0:24
Fulvio Dominici12-Sep-13 0:24 
QuestionThai / Chinese or German language Pin
Member 98364699-Aug-13 20:33
Member 98364699-Aug-13 20:33 
AnswerRe: Thai / Chinese or German language Pin
Fulvio Dominici21-Nov-13 3:11
Fulvio Dominici21-Nov-13 3:11 
QuestionProject Description Pin
Roshan Jindani20-Jun-13 7:35
Roshan Jindani20-Jun-13 7:35 

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.