zbar_pipeline.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "zbar_pipeline.h"
  2. #include "pipeline.h"
  3. #include "main.h"
  4. #include "io_pipeline.h"
  5. #include <zbar.h>
  6. #include <assert.h>
  7. static MPPipeline *pipeline;
  8. static volatile int frames_processed = 0;
  9. static volatile int frames_received = 0;
  10. static zbar_image_scanner_t *scanner;
  11. static void setup(MPPipeline *pipeline, const void *data)
  12. {
  13. scanner = zbar_image_scanner_create();
  14. zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);
  15. }
  16. void
  17. mp_zbar_pipeline_start()
  18. {
  19. pipeline = mp_pipeline_new();
  20. mp_pipeline_invoke(pipeline, setup, NULL, 0);
  21. }
  22. void
  23. mp_zbar_pipeline_stop()
  24. {
  25. mp_pipeline_free(pipeline);
  26. }
  27. static bool is_3d_code(zbar_symbol_type_t type)
  28. {
  29. switch (type) {
  30. case ZBAR_EAN2:
  31. case ZBAR_EAN5:
  32. case ZBAR_EAN8:
  33. case ZBAR_UPCE:
  34. case ZBAR_ISBN10:
  35. case ZBAR_UPCA:
  36. case ZBAR_EAN13:
  37. case ZBAR_ISBN13:
  38. case ZBAR_I25:
  39. case ZBAR_DATABAR:
  40. case ZBAR_DATABAR_EXP:
  41. case ZBAR_CODABAR:
  42. case ZBAR_CODE39:
  43. case ZBAR_CODE93:
  44. case ZBAR_CODE128:
  45. return false;
  46. case ZBAR_COMPOSITE:
  47. case ZBAR_PDF417:
  48. case ZBAR_QRCODE:
  49. case ZBAR_SQCODE:
  50. return true;
  51. default:
  52. return false;
  53. }
  54. }
  55. static MPZBarCode
  56. process_symbol(const zbar_symbol_t *symbol)
  57. {
  58. MPZBarCode code;
  59. unsigned loc_size = zbar_symbol_get_loc_size(symbol);
  60. assert(loc_size > 0);
  61. zbar_symbol_type_t type = zbar_symbol_get_type(symbol);
  62. if (is_3d_code(type) && loc_size == 4) {
  63. for (unsigned i = 0; i < loc_size; ++i) {
  64. code.bounds_x[i] = zbar_symbol_get_loc_x(symbol, i);
  65. code.bounds_y[i] = zbar_symbol_get_loc_y(symbol, i);
  66. }
  67. } else {
  68. int min_x = zbar_symbol_get_loc_x(symbol, 0);
  69. int min_y = zbar_symbol_get_loc_y(symbol, 0);
  70. int max_x = min_x, max_y = min_y;
  71. for (unsigned i = 1; i < loc_size; ++i) {
  72. int x = zbar_symbol_get_loc_x(symbol, i);
  73. int y = zbar_symbol_get_loc_y(symbol, i);
  74. min_x = MIN(min_x, x);
  75. min_y = MIN(min_y, y);
  76. max_x = MAX(max_x, x);
  77. max_y = MAX(max_y, y);
  78. }
  79. code.bounds_x[0] = min_x;
  80. code.bounds_y[0] = min_y;
  81. code.bounds_x[1] = max_x;
  82. code.bounds_y[1] = min_y;
  83. code.bounds_x[2] = max_x;
  84. code.bounds_y[2] = max_y;
  85. code.bounds_x[3] = min_x;
  86. code.bounds_y[3] = max_y;
  87. }
  88. const char *data = zbar_symbol_get_data(symbol);
  89. unsigned int data_size = zbar_symbol_get_data_length(symbol);
  90. code.type = zbar_get_symbol_name(type);
  91. code.data = strndup(data, data_size+1);
  92. code.data[data_size] = 0;
  93. return code;
  94. }
  95. static void
  96. process_surface(MPPipeline *pipeline, cairo_surface_t **_surface)
  97. {
  98. cairo_surface_t *surface = *_surface;
  99. int width = cairo_image_surface_get_width(surface);
  100. int height = cairo_image_surface_get_height(surface);
  101. const uint32_t *surface_data = (const uint32_t *)cairo_image_surface_get_data(surface);
  102. // Create a grayscale image for scanning from the current preview
  103. uint8_t *data = malloc(width * height * sizeof(uint8_t));
  104. for (size_t i = 0; i < width * height; ++i) {
  105. data[i] = (surface_data[i] >> 16) & 0xff;
  106. }
  107. // Create image for zbar
  108. zbar_image_t *zbar_image = zbar_image_create();
  109. zbar_image_set_format(zbar_image, zbar_fourcc('Y', '8', '0', '0'));
  110. zbar_image_set_size(zbar_image, width, height);
  111. zbar_image_set_data(zbar_image, data, width * height * sizeof(uint8_t), zbar_image_free_data);
  112. int res = zbar_scan_image(scanner, zbar_image);
  113. assert(res >= 0);
  114. if (res > 0) {
  115. MPZBarScanResult *result = malloc(sizeof(MPZBarScanResult));
  116. result->size = res;
  117. const zbar_symbol_t *symbol = zbar_image_first_symbol(zbar_image);
  118. for (int i = 0; i < MIN(res, 8); ++i) {
  119. assert(symbol != NULL);
  120. result->codes[i] = process_symbol(symbol);
  121. symbol = zbar_symbol_next(symbol);
  122. }
  123. mp_main_set_zbar_result(result);
  124. } else {
  125. mp_main_set_zbar_result(NULL);
  126. }
  127. zbar_image_destroy(zbar_image);
  128. cairo_surface_destroy(surface);
  129. ++frames_processed;
  130. }
  131. void
  132. mp_zbar_pipeline_process_image(cairo_surface_t *surface)
  133. {
  134. // If we haven't processed the previous frame yet, drop this one
  135. if (frames_received != frames_processed) {
  136. return;
  137. }
  138. ++frames_received;
  139. mp_pipeline_invoke(pipeline, (MPPipelineCallback)process_surface, &surface,
  140. sizeof(cairo_surface_t *));
  141. }