Paparazzi UAS  v5.8.2_stable-0-g6260b7c
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 void qrcode_init(void)
33 {
34  // Add qrscan to the list of image processing tasks in video_thread
35  cv_add(qrscan);
36 }
37 
38 // Telemetry
40 
41 
42 zbar_image_scanner_t *scanner = 0;
43 
44 bool_t qrscan(struct image_t *img)
45 {
46  int i, j;
47 
48  // Create the JPEG encoded image
49  struct image_t gray;
50  image_create(&gray, img->w, img->h, IMAGE_GRAYSCALE);
51 
52  uint8_t *ii = (uint8_t *) img->buf;
53  uint8_t *oi = (uint8_t *) gray.buf;
54 
55  for (j = 0; j < img->h; j++) {
56  for (i = 0; i < img->w; i++) {
57  oi[j * img->w + i] = ii[(j * img->w + i) * 2 + 1];
58  }
59  }
60 
61  if (scanner == 0) {
62  // create a reader
63  scanner = zbar_image_scanner_create();
64 
65  // configure the reader
66  //zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_POSITION, 1);
67  //zbar_increase_verbosity();
68  }
69 
70  // wrap image data
71  zbar_image_t *image = zbar_image_create();
72  zbar_image_set_format(image, *(int *)"Y800");
73  zbar_image_set_size(image, gray.w, gray.h);
74  zbar_image_set_data(image, gray.buf, gray.buf_size, zbar_image_free_data);
75 
76  // scan the image for barcodes
77  int n = zbar_scan_image(scanner, image);
78 
79  if (n < 0) {
80  printf("zbar_scan_image returned %d\n", n);
81  }
82 
83  // extract results
84  const zbar_symbol_t *symbol = zbar_image_first_symbol(image);
85  for (; symbol; symbol = zbar_symbol_next(symbol)) {
86  // do something useful with results
87  zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
88  char *data = (char *)zbar_symbol_get_data(symbol);
89  printf("decoded %s symbol \"%s\"\n",
90  zbar_get_symbol_name(typ), data);
91 
92  // TODO: not allowed to access telemetry from vision thread
93 #if DOWNLINK
94  DOWNLINK_SEND_INFO_MSG(DefaultChannel, DefaultDevice, strlen(data), data);
95 #endif
96  }
97 
98 // clean up
99  zbar_image_destroy(image);
100  //zbar_image_scanner_destroy(scanner);
101 }
uint32_t buf_size
The buffer size.
Definition: image.h:49
void qrcode_init(void)
Definition: qr_code.c:32
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:38
Definition: image.h:42
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:50
void cv_add(cvFunction func)
Definition: cv.c:35
bool_t qrscan(struct image_t *img)
Definition: qr_code.c:44
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:42
Grayscale image with only the Y part (uint8 per pixel)
Definition: image.h:36