Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
qr_code.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015
3  *
4  * This file is part of Paparazzi.
5  *
6  * Paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * Paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  *
20  */
21 
26 #include "qr_code.h"
27 #include "cv.h"
28 
29 #include "zbar.h"
30 #include <stdio.h>
31 
32 #ifndef QRCODE_DRAW_RECTANGLE
33 #define QRCODE_DRAW_RECTANGLE FALSE
34 #endif
36 
37 #ifndef QRCODE_FPS
38 #define QRCODE_FPS 0
39 #endif
40 PRINT_CONFIG_VAR(QRCODE_FPS)
41 
42 void qrcode_init(void)
43 {
44  // Add qrscan to the list of image processing tasks in video_thread
45  cv_add_to_device(&QRCODE_CAMERA, qrscan, QRCODE_FPS);
46 }
47 
48 // Telemetry
50 
51 
52 zbar_image_scanner_t *scanner = 0;
53 
54 struct image_t *qrscan(struct image_t *img)
55 {
56  int i, j;
57 
58  // Create the JPEG encoded image
59  struct image_t gray;
60  image_create(&gray, img->w, img->h, IMAGE_GRAYSCALE);
61 
62  uint8_t *ii = (uint8_t *) img->buf;
63  uint8_t *oi = (uint8_t *) gray.buf;
64 
65  for (j = 0; j < img->h; j++) {
66  for (i = 0; i < img->w; i++) {
67  oi[j * img->w + i] = ii[(j * img->w + i) * 2 + 1];
68  }
69  }
70 
71  if (scanner == 0) {
72  // create a reader
73  scanner = zbar_image_scanner_create();
74 
75  // configure the reader
76  //zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_POSITION, 1);
77  //zbar_increase_verbosity();
78  }
79 
80  // wrap image data
81  zbar_image_t *image = zbar_image_create();
82  zbar_image_set_format(image, *(int *)"Y800");
83  zbar_image_set_size(image, gray.w, gray.h);
84  zbar_image_set_data(image, gray.buf, gray.buf_size, zbar_image_free_data);
85 
86  // scan the image for barcodes
87  int n = zbar_scan_image(scanner, image);
88 
89  if (n < 0) {
90  printf("zbar_scan_image returned %d\n", n);
91  }
92 
93  // extract results
94  const zbar_symbol_t *symbol = zbar_image_first_symbol(image);
95  for (; symbol; symbol = zbar_symbol_next(symbol)) {
96  // do something useful with results
97  zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
98  char *data = (char *)zbar_symbol_get_data(symbol);
99  printf("decoded %s symbol \"%s\" at %d %d\n",
100  zbar_get_symbol_name(typ), data, zbar_symbol_get_loc_x(symbol, 0), zbar_symbol_get_loc_y(symbol, 0));
101 
103  uint8_t cornerIndex;
104  struct point_t from;
105  struct point_t to;
106 
107  for (cornerIndex = 0; cornerIndex < zbar_symbol_get_loc_size(symbol); cornerIndex++) {
108  // Determine where to draw from and to
109  uint8_t nextCorner = (cornerIndex + 1) % zbar_symbol_get_loc_size(symbol);
110 
111  from.x = zbar_symbol_get_loc_x(symbol, cornerIndex);
112  from.y = zbar_symbol_get_loc_y(symbol, cornerIndex);
113 
114  to.x = zbar_symbol_get_loc_x(symbol, nextCorner);
115  to.y = zbar_symbol_get_loc_y(symbol, nextCorner);
116 
117  // Draw a line between these two corners
118  image_draw_line(img, &to, &from);
119  }
120 
121  }
122  // TODO: not allowed to access telemetry from vision thread
123 #if DOWNLINK
124  DOWNLINK_SEND_INFO_MSG(DefaultChannel, DefaultDevice, strlen(data), data);
125 #endif
126  }
127 
128 // clean up
129  zbar_image_destroy(image);
130  //zbar_image_scanner_destroy(scanner);
131 
132  return img;
133 }
uint32_t buf_size
The buffer size.
Definition: image.h:52
void qrcode_init(void)
Definition: qr_code.c:42
Periodic telemetry system header (includes downlink utility and generated code).
void image_create(struct image_t *img, uint16_t width, uint16_t height, enum image_type type)
Create a new image.
Definition: image.c:39
Definition: image.h:43
void image_draw_line(struct image_t *img, struct point_t *from, struct point_t *to)
Draw a pink line on the image.
Definition: image.c:876
uint32_t x
The x coordinate of the point.
Definition: image.h:58
struct video_listener * cv_add_to_device(struct video_config_t *device, cv_function func, uint16_t fps)
Definition: cv.c:46
uint16_t w
Image width.
Definition: image.h:45
Computer vision framework for onboard processing.
uint16_t h
Image height.
Definition: image.h:46
void * buf
Image buffer (depending on the image_type)
Definition: image.h:53
uint32_t y
The y coordinate of the point.
Definition: image.h:59
Definition: image.h:57
bool drawRectangleAroundQRCode
Definition: qr_code.c:35
#define QRCODE_DRAW_RECTANGLE
Definition: qr_code.c:33
unsigned char uint8_t
Definition: types.h:14
Parse video stream to detect and decode QR-codes using the ZBAR library.
zbar_image_scanner_t * scanner
Definition: qr_code.c:52
Grayscale image with only the Y part (uint8 per pixel)
Definition: image.h:37
#define QRCODE_FPS
Default FPS (zero means run at camera fps)
Definition: qr_code.c:38
struct image_t * qrscan(struct image_t *img)
Definition: qr_code.c:54