quirc.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* quirc -- QR-code recognition library
  2. * Copyright (C) 2010-2012 Daniel Beer <dlbeer@gmail.com>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef QUIRC_H_
  17. #define QUIRC_H_
  18. #include <stdint.h>
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. struct quirc;
  23. /* Obtain the library version string. */
  24. const char *quirc_version(void);
  25. /* Construct a new QR-code recognizer. This function will return NULL
  26. * if sufficient memory could not be allocated.
  27. */
  28. struct quirc *quirc_new(void);
  29. /* Destroy a QR-code recognizer. */
  30. void quirc_destroy(struct quirc *q);
  31. /* Resize the QR-code recognizer. The size of an image must be
  32. * specified before codes can be analyzed.
  33. *
  34. * This function returns 0 on success, or -1 if sufficient memory could
  35. * not be allocated.
  36. */
  37. int quirc_resize(struct quirc *q, int w, int h);
  38. /* These functions are used to process images for QR-code recognition.
  39. * quirc_begin() must first be called to obtain access to a buffer into
  40. * which the input image should be placed. Optionally, the current
  41. * width and height may be returned.
  42. *
  43. * After filling the buffer, quirc_end() should be called to process
  44. * the image for QR-code recognition. The locations and content of each
  45. * code may be obtained using accessor functions described below.
  46. */
  47. uint8_t *quirc_begin(struct quirc *q, int *w, int *h);
  48. void quirc_end(struct quirc *q);
  49. /* This structure describes a location in the input image buffer. */
  50. struct quirc_point {
  51. int x;
  52. int y;
  53. };
  54. /* This enum describes the various decoder errors which may occur. */
  55. typedef enum {
  56. QUIRC_SUCCESS = 0,
  57. QUIRC_ERROR_INVALID_GRID_SIZE,
  58. QUIRC_ERROR_INVALID_VERSION,
  59. QUIRC_ERROR_FORMAT_ECC,
  60. QUIRC_ERROR_DATA_ECC,
  61. QUIRC_ERROR_UNKNOWN_DATA_TYPE,
  62. QUIRC_ERROR_DATA_OVERFLOW,
  63. QUIRC_ERROR_DATA_UNDERFLOW
  64. } quirc_decode_error_t;
  65. /* Return a string error message for an error code. */
  66. const char *quirc_strerror(quirc_decode_error_t err);
  67. /* Limits on the maximum size of QR-codes and their content. */
  68. #define QUIRC_MAX_BITMAP 3917
  69. #define QUIRC_MAX_PAYLOAD 8896
  70. /* QR-code ECC types. */
  71. #define QUIRC_ECC_LEVEL_M 0
  72. #define QUIRC_ECC_LEVEL_L 1
  73. #define QUIRC_ECC_LEVEL_H 2
  74. #define QUIRC_ECC_LEVEL_Q 3
  75. /* QR-code data types. */
  76. #define QUIRC_DATA_TYPE_NUMERIC 1
  77. #define QUIRC_DATA_TYPE_ALPHA 2
  78. #define QUIRC_DATA_TYPE_BYTE 4
  79. #define QUIRC_DATA_TYPE_KANJI 8
  80. /* Common character encodings */
  81. #define QUIRC_ECI_ISO_8859_1 1
  82. #define QUIRC_ECI_IBM437 2
  83. #define QUIRC_ECI_ISO_8859_2 4
  84. #define QUIRC_ECI_ISO_8859_3 5
  85. #define QUIRC_ECI_ISO_8859_4 6
  86. #define QUIRC_ECI_ISO_8859_5 7
  87. #define QUIRC_ECI_ISO_8859_6 8
  88. #define QUIRC_ECI_ISO_8859_7 9
  89. #define QUIRC_ECI_ISO_8859_8 10
  90. #define QUIRC_ECI_ISO_8859_9 11
  91. #define QUIRC_ECI_WINDOWS_874 13
  92. #define QUIRC_ECI_ISO_8859_13 15
  93. #define QUIRC_ECI_ISO_8859_15 17
  94. #define QUIRC_ECI_SHIFT_JIS 20
  95. #define QUIRC_ECI_UTF_8 26
  96. /* This structure is used to return information about detected QR codes
  97. * in the input image.
  98. */
  99. struct quirc_code {
  100. /* The four corners of the QR-code, from top left, clockwise */
  101. struct quirc_point corners[4];
  102. /* The number of cells across in the QR-code. The cell bitmap
  103. * is a bitmask giving the actual values of cells. If the cell
  104. * at (x, y) is black, then the following bit is set:
  105. *
  106. * cell_bitmap[i >> 3] & (1 << (i & 7))
  107. *
  108. * where i = (y * size) + x.
  109. */
  110. int size;
  111. uint8_t cell_bitmap[QUIRC_MAX_BITMAP];
  112. };
  113. /* This structure holds the decoded QR-code data */
  114. struct quirc_data {
  115. /* Various parameters of the QR-code. These can mostly be
  116. * ignored if you only care about the data.
  117. */
  118. int version;
  119. int ecc_level;
  120. int mask;
  121. /* This field is the highest-valued data type found in the QR
  122. * code.
  123. */
  124. int data_type;
  125. /* Data payload. For the Kanji datatype, payload is encoded as
  126. * Shift-JIS. For all other datatypes, payload is ASCII text.
  127. */
  128. uint8_t payload[QUIRC_MAX_PAYLOAD];
  129. int payload_len;
  130. /* ECI assignment number */
  131. uint32_t eci;
  132. };
  133. /* Return the number of QR-codes identified in the last processed
  134. * image.
  135. */
  136. int quirc_count(const struct quirc *q);
  137. /* Extract the QR-code specified by the given index. */
  138. void quirc_extract(const struct quirc *q, int index,
  139. struct quirc_code *code);
  140. /* Decode a QR-code, returning the payload data. */
  141. quirc_decode_error_t quirc_decode(const struct quirc_code *code,
  142. struct quirc_data *data);
  143. #ifdef __cplusplus
  144. }
  145. #endif
  146. #endif