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