|
| 1 | +Quirc |
| 2 | +===== |
| 3 | + |
| 4 | +QR codes are a type of high-density matrix barcodes, and quirc is a library for |
| 5 | +extracting and decoding them from images. It has several features which make it |
| 6 | +a good choice for this purpose: |
| 7 | + |
| 8 | +* It is fast enough to be used with realtime video: extracting and decoding |
| 9 | + from VGA frame takes about 50 ms on a modern x86 core. |
| 10 | + |
| 11 | +* It has a robust and tolerant recognition algorithm. It can correctly |
| 12 | + recognise and decode QR codes which are rotated and/or oblique to the camera. |
| 13 | + It can also distinguish and decode multiple codes within the same image. |
| 14 | + |
| 15 | +* It is easy to use, with a simple API described in a single commented header |
| 16 | + file (see below for an overview). |
| 17 | + |
| 18 | +* It is small and easily embeddable, with no dependencies other than standard C |
| 19 | + functions. |
| 20 | + |
| 21 | +* It has a very small memory footprint: one byte per image pixel, plus a few kB |
| 22 | + per decoder object. |
| 23 | + |
| 24 | +* It uses no global mutable state, and is safe to use in a multithreaded |
| 25 | + application. |
| 26 | + |
| 27 | +* BSD-licensed, with almost no restrictions regarding use and/or modification. |
| 28 | + |
| 29 | +The distribution comes with, in addition to the library, several test programs. |
| 30 | +While the core library is very portable, these programs have some additional |
| 31 | +dependencies. All of them require libjpeg, and two (`quirc-demo` and `inspect`) |
| 32 | +require SDL. The camera demos use Linux-specific APIs: |
| 33 | + |
| 34 | +### quirc-demo |
| 35 | + |
| 36 | +This is an real-time demo which requires a camera and a graphical display. The |
| 37 | +video stream is displayed on screen as it's received, and any QR codes |
| 38 | +recognised are highlighted in the image, with the decoded information both |
| 39 | +displayed on the image and printed on stdout. |
| 40 | + |
| 41 | +### quirc-scanner |
| 42 | + |
| 43 | +This program turns your camera into a barcode scanner. It's almost the same as |
| 44 | +the `demo` application, but it doesn't display the video stream, and thus |
| 45 | +doesn't require a graphical display. |
| 46 | + |
| 47 | +### qrtest |
| 48 | + |
| 49 | +This test is used to evaluate the performance of library. Given a directory |
| 50 | +tree containing a bunch of JPEG images, it will attempt to locate and decode QR |
| 51 | +codes in each image. Speed and success statistics are collected and printed on |
| 52 | +stdout. |
| 53 | + |
| 54 | +### inspect |
| 55 | + |
| 56 | +This test is used for debugging. Given a single JPEG image, it will display a |
| 57 | +diagram showing the internal state of the decoder as well as printing |
| 58 | +additional information on stdout. |
| 59 | + |
| 60 | +Installation |
| 61 | +------------ |
| 62 | +To build the library and associated demos/tests, type `make`. If you need to |
| 63 | +decode "large" image files build with `CFLAGS="-DQUIRC_MAX_REGIONS=65534" make` |
| 64 | +instead. Note that this will increase the memory usage, it is discouraged for |
| 65 | +low resource devices (i.e. embedded). |
| 66 | + |
| 67 | +Type `make install` to install the library, header file and camera demos. |
| 68 | + |
| 69 | +You can specify one or several of the following targets if you don't want, or |
| 70 | +are unable to build everything: |
| 71 | + |
| 72 | +* libquirc.a |
| 73 | +* libquirc.so |
| 74 | +* qrtest |
| 75 | +* inspect |
| 76 | +* quirc-scanner |
| 77 | +* quirc-demo |
| 78 | + |
| 79 | +Library use |
| 80 | +----------- |
| 81 | +All of the library's functionality is exposed through a single header file, |
| 82 | +which you should include: |
| 83 | + |
| 84 | +```C |
| 85 | +#include <quirc.h> |
| 86 | +``` |
| 87 | + |
| 88 | +To decode images, you'll need to instantiate a `struct quirc` object, which is |
| 89 | +done with the `quirc_new` function. Later, when you no longer need to decode |
| 90 | +anything, you should release the allocated memory with `quirc_destroy`: |
| 91 | + |
| 92 | +```C |
| 93 | +struct quirc *qr; |
| 94 | + |
| 95 | +qr = quirc_new(); |
| 96 | +if (!qr) { |
| 97 | + perror("Failed to allocate memory"); |
| 98 | + abort(); |
| 99 | +} |
| 100 | + |
| 101 | +/* ... */ |
| 102 | + |
| 103 | +quirc_destroy(qr); |
| 104 | +``` |
| 105 | +
|
| 106 | +Having obtained a decoder object, you need to set the image size that you'll be |
| 107 | +working with, which is done using `quirc_resize`: |
| 108 | +
|
| 109 | +```C |
| 110 | +if (quirc_resize(qr, 640, 480) < 0) { |
| 111 | + perror("Failed to allocate video memory"); |
| 112 | + abort(); |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +`quirc_resize` and `quirc_new` are the only library functions which allocate |
| 117 | +memory. If you plan to process a series of frames (or a video stream), you |
| 118 | +probably want to allocate and size a single decoder and hold onto it to process |
| 119 | +each frame. |
| 120 | + |
| 121 | +Processing frames is done in two stages. The first stage is an |
| 122 | +image-recognition stage called identification, which takes a grayscale image |
| 123 | +and searches for QR codes. Using `quirc_begin` and `quirc_end`, you can feed a |
| 124 | +grayscale image directly into the buffer that `quirc` uses for image |
| 125 | +processing: |
| 126 | + |
| 127 | +```C |
| 128 | +uint8_t *image; |
| 129 | +int w, h; |
| 130 | + |
| 131 | +image = quirc_begin(qr, &w, &h); |
| 132 | + |
| 133 | +/* Fill out the image buffer here. |
| 134 | + * image is a pointer to a w*h bytes. |
| 135 | + * One byte per pixel, w pixels per line, h lines in the buffer. |
| 136 | + */ |
| 137 | + |
| 138 | +quirc_end(qr); |
| 139 | +``` |
| 140 | +
|
| 141 | +Note that `quirc_begin` simply returns a pointer to a previously allocated |
| 142 | +buffer. The buffer will contain uninitialized data. After the call to |
| 143 | +`quirc_end`, the decoder holds a list of detected QR codes which can be queried |
| 144 | +via `quirc_count` and `quirc_extract`. |
| 145 | +
|
| 146 | +At this point, the second stage of processing occurs -- decoding. This is done |
| 147 | +via the call to `quirc_decode`, which is not associated with a decoder object. |
| 148 | +
|
| 149 | +```C |
| 150 | +int num_codes; |
| 151 | +int i; |
| 152 | +
|
| 153 | +/* We've previously fed an image to the decoder via |
| 154 | +* quirc_begin/quirc_end. |
| 155 | +*/ |
| 156 | +
|
| 157 | +num_codes = quirc_count(qr); |
| 158 | +for (i = 0; i < num_codes; i++) { |
| 159 | + struct quirc_code code; |
| 160 | + struct quirc_data data; |
| 161 | + quirc_decode_error_t err; |
| 162 | +
|
| 163 | + quirc_extract(qr, i, &code); |
| 164 | +
|
| 165 | + /* Decoding stage */ |
| 166 | + err = quirc_decode(&code, &data); |
| 167 | + if (err) |
| 168 | + printf("DECODE FAILED: %s\n", quirc_strerror(err)); |
| 169 | + else |
| 170 | + printf("Data: %s\n", data.payload); |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +`quirc_code` and `quirc_data` are flat structures which don't need to be |
| 175 | +initialized or freed after use. |
| 176 | + |
| 177 | +Copyright |
| 178 | +--------- |
| 179 | +Copyright (C) 2010-2012 Daniel Beer <<dlbeer@gmail.com>> |
| 180 | + |
| 181 | +Permission to use, copy, modify, and/or distribute this software for |
| 182 | +any purpose with or without fee is hereby granted, provided that the |
| 183 | +above copyright notice and this permission notice appear in all |
| 184 | +copies. |
| 185 | + |
| 186 | +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
| 187 | +WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
| 188 | +WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE |
| 189 | +AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 190 | +DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 191 | +PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 192 | +TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 193 | +PERFORMANCE OF THIS SOFTWARE. |
0 commit comments