Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
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 void qrcode_init(void)
38 {
39  // Add qrscan to the list of image processing tasks in video_thread
40  cv_add_to_device(&QRCODE_CAMERA, qrscan);
41 }
42 
43 // Telemetry
45 
46 
47 zbar_image_scanner_t *scanner = 0;
48 
49 struct image_t *qrscan(struct image_t *img)
50 {
51  int i, j;
52 
53  // Create the JPEG encoded image
54  struct image_t gray;
55  image_create(&gray, img->w, img->h, IMAGE_GRAYSCALE);
56 
57  uint8_t *ii = (uint8_t *) img->buf;
58  uint8_t *oi = (uint8_t *) gray.buf;
59 
60  for (j = 0; j < img->h; j++) {
61  for (i = 0; i < img->w; i++) {
62  oi[j * img->w + i] = ii[(j * img->w + i) * 2 + 1];
63  }
64  }
65 
66  if (scanner == 0) {
67  // create a reader
68  scanner = zbar_image_scanner_create();
69 
70  // configure the reader
71  //zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_POSITION, 1);
72  //zbar_increase_verbosity();
73  }
74 
75  // wrap image data
76  zbar_image_t *image = zbar_image_create();
77  zbar_image_set_format(image, *(int *)"Y800");
78  zbar_image_set_size(image, gray.w, gray.h);
79  zbar_image_set_data(image, gray.buf, gray.buf_size, zbar_image_free_data);
80 
81  // scan the image for barcodes
82  int n = zbar_scan_image(scanner, image);
83 
84  if (n < 0) {
85  printf("zbar_scan_image returned %d\n", n);
86  }
87 
88  // extract results
89  const zbar_symbol_t *symbol = zbar_image_first_symbol(image);
90  for (; symbol; symbol = zbar_symbol_next(symbol)) {
91  // do something useful with results
92  zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
93  char *data = (char *)zbar_symbol_get_data(symbol);
94  printf("decoded %s symbol \"%s\" at %d %d\n",
95  zbar_get_symbol_name(typ), data, zbar_symbol_get_loc_x(symbol, 0), zbar_symbol_get_loc_y(symbol, 0));
96 
98  uint8_t cornerIndex;
99  struct point_t from;
100  struct point_t to;
101 
102  for (cornerIndex = 0; cornerIndex < zbar_symbol_get_loc_size(symbol); cornerIndex++) {
103  // Determine where to draw from and to
104  uint8_t nextCorner = (cornerIndex + 1) % zbar_symbol_get_loc_size(symbol);
105 
106  from.x = zbar_symbol_get_loc_x(symbol, cornerIndex);
107  from.y = zbar_symbol_get_loc_y(symbol, cornerIndex);
108 
109  to.x = zbar_symbol_get_loc_x(symbol, nextCorner);
110  to.y = zbar_symbol_get_loc_y(symbol, nextCorner);
111 
112  // Draw a line between these two corners
113  image_draw_line(img, &to, &from);
114  }
115 
116  }
117  // TODO: not allowed to access telemetry from vision thread
118 #if DOWNLINK
119  DOWNLINK_SEND_INFO_MSG(DefaultChannel, DefaultDevice, strlen(data), data);
120 #endif
121  }
122 
123 // clean up
124  zbar_image_destroy(image);
125  //zbar_image_scanner_destroy(scanner);
126 
127  return img;
128 }
uint32_t buf_size
The buffer size.
Definition: image.h:50
void qrcode_init(void)
Definition: qr_code.c:37
Periodic telemetry system header (includes downlink utility and generated code).
struct video_listener * cv_add_to_device(struct video_config_t *device, cv_function func)
Definition: cv.c:45
void image_create(struct image_t *img, uint16_t width, uint16_t height, enum image_type type)
Create a new image.
Definition: image.c:38
Definition: image.h:42
void image_draw_line(struct image_t *img, struct point_t *from, struct point_t *to)
Draw a line on the image.
Definition: image.c:599
uint32_t x
The x coordinate of the point.
Definition: image.h:56
uint16_t w
Image width.
Definition: image.h:44
Computer vision framework for onboard processing.
uint16_t h
Image height.
Definition: image.h:45
void * buf
Image buffer (depending on the image_type)
Definition: image.h:51
uint32_t y
The y coordinate of the point.
Definition: image.h:57
Definition: image.h:55
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:47
Grayscale image with only the Y part (uint8 per pixel)
Definition: image.h:36
struct image_t * qrscan(struct image_t *img)
Definition: qr_code.c:49