ZBar bar code reader . http://zbar.sourceforge.net/ ZBar is licensed under the GNU LGPL 2.1 to enable development of both open source and commercial projects.
Dependents: GR-PEACH_Camera_in_barcode levkov_ov7670
decoder.c
00001 /*------------------------------------------------------------------------ 00002 * Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net> 00003 * 00004 * This file is part of the ZBar Bar Code Reader. 00005 * 00006 * The ZBar Bar Code Reader is free software; you can redistribute it 00007 * and/or modify it under the terms of the GNU Lesser Public License as 00008 * published by the Free Software Foundation; either version 2.1 of 00009 * the License, or (at your option) any later version. 00010 * 00011 * The ZBar Bar Code Reader is distributed in the hope that it will be 00012 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00013 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU Lesser Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser Public License 00017 * along with the ZBar Bar Code Reader; if not, write to the Free 00018 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, 00019 * Boston, MA 02110-1301 USA 00020 * 00021 * http://sourceforge.net/projects/zbar 00022 *------------------------------------------------------------------------*/ 00023 00024 #include <config.h> 00025 #include <stdlib.h> /* malloc, calloc, free */ 00026 #include <stdio.h> /* snprintf */ 00027 #include <string.h> /* memset, strlen */ 00028 00029 #include <zbar.h> 00030 #include "decoder.h" 00031 00032 #if defined(DEBUG_DECODER) || defined(DEBUG_EAN) || \ 00033 defined(DEBUG_CODE39) || defined(DEBUG_I25) || \ 00034 defined(DEBUG_CODE128) || defined(DEBUG_QR_FINDER) || \ 00035 (defined(DEBUG_PDF417) && (DEBUG_PDF417 >= 4)) 00036 # define DEBUG_LEVEL 1 00037 #endif 00038 #include "zbar_debug.h" 00039 00040 zbar_decoder_t *zbar_decoder_create () 00041 { 00042 zbar_decoder_t *dcode = calloc(1, sizeof(zbar_decoder_t)); 00043 dcode->buf_alloc = BUFFER_MIN; 00044 dcode->buf = malloc(dcode->buf_alloc); 00045 00046 /* initialize default configs */ 00047 #ifdef ENABLE_EAN 00048 dcode->ean.enable = 1; 00049 dcode->ean.ean13_config = ((1 << ZBAR_CFG_ENABLE) | 00050 (1 << ZBAR_CFG_EMIT_CHECK)); 00051 dcode->ean.ean8_config = ((1 << ZBAR_CFG_ENABLE) | 00052 (1 << ZBAR_CFG_EMIT_CHECK)); 00053 dcode->ean.upca_config = 1 << ZBAR_CFG_EMIT_CHECK; 00054 dcode->ean.upce_config = 1 << ZBAR_CFG_EMIT_CHECK; 00055 dcode->ean.isbn10_config = 1 << ZBAR_CFG_EMIT_CHECK; 00056 dcode->ean.isbn13_config = 1 << ZBAR_CFG_EMIT_CHECK; 00057 #endif 00058 #ifdef ENABLE_I25 00059 dcode->i25.config = 1 << ZBAR_CFG_ENABLE; 00060 CFG(dcode->i25, ZBAR_CFG_MIN_LEN) = 6; 00061 #endif 00062 #ifdef ENABLE_CODE39 00063 dcode->code39.config = 1 << ZBAR_CFG_ENABLE; 00064 CFG(dcode->code39, ZBAR_CFG_MIN_LEN) = 1; 00065 #endif 00066 #ifdef ENABLE_CODE128 00067 dcode->code128.config = 1 << ZBAR_CFG_ENABLE; 00068 #endif 00069 #ifdef ENABLE_PDF417 00070 dcode->pdf417.config = 1 << ZBAR_CFG_ENABLE; 00071 #endif 00072 #ifdef ENABLE_QRCODE 00073 dcode->qrf.config = 1 << ZBAR_CFG_ENABLE; 00074 #endif 00075 00076 zbar_decoder_reset(dcode); 00077 return(dcode); 00078 } 00079 00080 void zbar_decoder_destroy (zbar_decoder_t *dcode) 00081 { 00082 if(dcode->buf) 00083 free(dcode->buf); 00084 free(dcode); 00085 } 00086 00087 void zbar_decoder_reset (zbar_decoder_t *dcode) 00088 { 00089 memset(dcode, 0, (long)&dcode->buf_alloc - (long)dcode); 00090 #ifdef ENABLE_EAN 00091 ean_reset(&dcode->ean); 00092 #endif 00093 #ifdef ENABLE_I25 00094 i25_reset(&dcode->i25); 00095 #endif 00096 #ifdef ENABLE_CODE39 00097 code39_reset(&dcode->code39); 00098 #endif 00099 #ifdef ENABLE_CODE128 00100 code128_reset(&dcode->code128); 00101 #endif 00102 #ifdef ENABLE_PDF417 00103 pdf417_reset(&dcode->pdf417); 00104 #endif 00105 #ifdef ENABLE_QRCODE 00106 qr_finder_reset(&dcode->qrf); 00107 #endif 00108 } 00109 00110 void zbar_decoder_new_scan (zbar_decoder_t *dcode) 00111 { 00112 /* soft reset decoder */ 00113 memset(dcode->w, 0, sizeof(dcode->w)); 00114 dcode->lock = 0; 00115 dcode->idx = 0; 00116 #ifdef ENABLE_EAN 00117 ean_new_scan(&dcode->ean); 00118 #endif 00119 #ifdef ENABLE_I25 00120 i25_reset(&dcode->i25); 00121 #endif 00122 #ifdef ENABLE_CODE39 00123 code39_reset(&dcode->code39); 00124 #endif 00125 #ifdef ENABLE_CODE128 00126 code128_reset(&dcode->code128); 00127 #endif 00128 #ifdef ENABLE_PDF417 00129 pdf417_reset(&dcode->pdf417); 00130 #endif 00131 #ifdef ENABLE_QRCODE 00132 qr_finder_reset(&dcode->qrf); 00133 #endif 00134 } 00135 00136 00137 zbar_color_t zbar_decoder_get_color (const zbar_decoder_t *dcode) 00138 { 00139 return(get_color(dcode)); 00140 } 00141 00142 const char *zbar_decoder_get_data (const zbar_decoder_t *dcode) 00143 { 00144 return((char*)dcode->buf); 00145 } 00146 00147 unsigned int zbar_decoder_get_data_length (const zbar_decoder_t *dcode) 00148 { 00149 return(dcode->buflen); 00150 } 00151 00152 zbar_decoder_handler_t * 00153 zbar_decoder_set_handler (zbar_decoder_t *dcode, 00154 zbar_decoder_handler_t handler) 00155 { 00156 zbar_decoder_handler_t *result = dcode->handler; 00157 dcode->handler = handler; 00158 return(result); 00159 } 00160 00161 void zbar_decoder_set_userdata (zbar_decoder_t *dcode, 00162 void *userdata) 00163 { 00164 dcode->userdata = userdata; 00165 } 00166 00167 void *zbar_decoder_get_userdata (const zbar_decoder_t *dcode) 00168 { 00169 return(dcode->userdata); 00170 } 00171 00172 zbar_symbol_type_t zbar_decoder_get_type (const zbar_decoder_t *dcode) 00173 { 00174 return(dcode->type); 00175 } 00176 00177 zbar_symbol_type_t zbar_decode_width (zbar_decoder_t *dcode, 00178 unsigned w) 00179 { 00180 dcode->w[dcode->idx & (DECODE_WINDOW - 1)] = w; 00181 dprintf(1, " decode[%x]: w=%d (%g)\n", dcode->idx, w, (w / 32.)); 00182 00183 /* each decoder processes width stream in parallel */ 00184 zbar_symbol_type_t sym = dcode->type = ZBAR_NONE; 00185 00186 #ifdef ENABLE_EAN 00187 if((dcode->ean.enable) && 00188 (sym = _zbar_decode_ean(dcode))) 00189 dcode->type = sym; 00190 #endif 00191 #ifdef ENABLE_CODE39 00192 if(TEST_CFG(dcode->code39.config, ZBAR_CFG_ENABLE) && 00193 (sym = _zbar_decode_code39(dcode)) > ZBAR_PARTIAL) 00194 dcode->type = sym; 00195 #endif 00196 #ifdef ENABLE_CODE128 00197 if(TEST_CFG(dcode->code128.config, ZBAR_CFG_ENABLE) && 00198 (sym = _zbar_decode_code128(dcode)) > ZBAR_PARTIAL) 00199 dcode->type = sym; 00200 #endif 00201 #ifdef ENABLE_I25 00202 if(TEST_CFG(dcode->i25.config, ZBAR_CFG_ENABLE) && 00203 (sym = _zbar_decode_i25(dcode)) > ZBAR_PARTIAL) 00204 dcode->type = sym; 00205 #endif 00206 #ifdef ENABLE_PDF417 00207 if(TEST_CFG(dcode->pdf417.config, ZBAR_CFG_ENABLE) && 00208 (sym = _zbar_decode_pdf417(dcode)) > ZBAR_PARTIAL) 00209 dcode->type = sym; 00210 #endif 00211 #ifdef ENABLE_QRCODE 00212 if(TEST_CFG(dcode->qrf.config, ZBAR_CFG_ENABLE) && 00213 (sym = _zbar_find_qr(dcode)) > ZBAR_PARTIAL) 00214 dcode->type = sym; 00215 #endif 00216 00217 dcode->idx++; 00218 if(dcode->type) { 00219 if(dcode->handler) 00220 dcode->handler(dcode); 00221 if(dcode->lock && dcode->type > ZBAR_PARTIAL) 00222 dcode->lock = 0; 00223 } 00224 return(dcode->type); 00225 } 00226 00227 static inline int decoder_set_config_bool (zbar_decoder_t *dcode, 00228 zbar_symbol_type_t sym, 00229 zbar_config_t cfg, 00230 int val) 00231 { 00232 unsigned *config = NULL; 00233 switch(sym) { 00234 #ifdef ENABLE_EAN 00235 case ZBAR_EAN13: 00236 config = &dcode->ean.ean13_config; 00237 break; 00238 00239 case ZBAR_EAN8: 00240 config = &dcode->ean.ean8_config; 00241 break; 00242 00243 case ZBAR_UPCA: 00244 config = &dcode->ean.upca_config; 00245 break; 00246 00247 case ZBAR_UPCE: 00248 config = &dcode->ean.upce_config; 00249 break; 00250 00251 case ZBAR_ISBN10: 00252 config = &dcode->ean.isbn10_config; 00253 break; 00254 00255 case ZBAR_ISBN13: 00256 config = &dcode->ean.isbn13_config; 00257 break; 00258 #endif 00259 00260 #ifdef ENABLE_I25 00261 case ZBAR_I25: 00262 config = &dcode->i25.config; 00263 break; 00264 #endif 00265 00266 #ifdef ENABLE_CODE39 00267 case ZBAR_CODE39: 00268 config = &dcode->code39.config; 00269 break; 00270 #endif 00271 00272 #ifdef ENABLE_CODE128 00273 case ZBAR_CODE128: 00274 config = &dcode->code128.config; 00275 break; 00276 #endif 00277 00278 #ifdef ENABLE_PDF417 00279 case ZBAR_PDF417: 00280 config = &dcode->pdf417.config; 00281 break; 00282 #endif 00283 00284 #ifdef ENABLE_QRCODE 00285 case ZBAR_QRCODE: 00286 config = &dcode->qrf.config; 00287 break; 00288 #endif 00289 00290 /* FIXME handle addons */ 00291 00292 default: 00293 return(1); 00294 } 00295 if(!config || cfg >= ZBAR_CFG_NUM) 00296 return(1); 00297 00298 if(!val) 00299 *config &= ~(1 << cfg); 00300 else if(val == 1) 00301 *config |= (1 << cfg); 00302 else 00303 return(1); 00304 00305 #ifdef ENABLE_EAN 00306 dcode->ean.enable = TEST_CFG(dcode->ean.ean13_config | 00307 dcode->ean.ean8_config | 00308 dcode->ean.upca_config | 00309 dcode->ean.upce_config | 00310 dcode->ean.isbn10_config | 00311 dcode->ean.isbn13_config, 00312 ZBAR_CFG_ENABLE); 00313 #endif 00314 00315 return(0); 00316 } 00317 00318 static inline int decoder_set_config_int (zbar_decoder_t *dcode, 00319 zbar_symbol_type_t sym, 00320 zbar_config_t cfg, 00321 int val) 00322 { 00323 switch(sym) { 00324 00325 #ifdef ENABLE_I25 00326 case ZBAR_I25: 00327 CFG(dcode->i25, cfg) = val; 00328 break; 00329 #endif 00330 #ifdef ENABLE_CODE39 00331 case ZBAR_CODE39: 00332 CFG(dcode->code39, cfg) = val; 00333 break; 00334 #endif 00335 #ifdef ENABLE_CODE128 00336 case ZBAR_CODE128: 00337 CFG(dcode->code128, cfg) = val; 00338 break; 00339 #endif 00340 #ifdef ENABLE_PDF417 00341 case ZBAR_PDF417: 00342 CFG(dcode->pdf417, cfg) = val; 00343 break; 00344 #endif 00345 00346 default: 00347 return(1); 00348 } 00349 return(0); 00350 } 00351 00352 int zbar_decoder_set_config (zbar_decoder_t *dcode, 00353 zbar_symbol_type_t sym, 00354 zbar_config_t cfg, 00355 int val) 00356 { 00357 if(sym == ZBAR_NONE) { 00358 zbar_decoder_set_config(dcode, ZBAR_EAN13, cfg, val); 00359 zbar_decoder_set_config(dcode, ZBAR_EAN8, cfg, val); 00360 zbar_decoder_set_config(dcode, ZBAR_UPCA, cfg, val); 00361 zbar_decoder_set_config(dcode, ZBAR_UPCE, cfg, val); 00362 zbar_decoder_set_config(dcode, ZBAR_ISBN10, cfg, val); 00363 zbar_decoder_set_config(dcode, ZBAR_ISBN13, cfg, val); 00364 zbar_decoder_set_config(dcode, ZBAR_I25, cfg, val); 00365 zbar_decoder_set_config(dcode, ZBAR_CODE39, cfg, val); 00366 zbar_decoder_set_config(dcode, ZBAR_CODE128, cfg, val); 00367 zbar_decoder_set_config(dcode, ZBAR_PDF417, cfg, val); 00368 zbar_decoder_set_config(dcode, ZBAR_QRCODE, cfg, val); 00369 return(0); 00370 } 00371 00372 if(cfg >= 0 && cfg < ZBAR_CFG_NUM) 00373 return(decoder_set_config_bool(dcode, sym, cfg, val)); 00374 else if(cfg >= ZBAR_CFG_MIN_LEN && cfg <= ZBAR_CFG_MAX_LEN) 00375 return(decoder_set_config_int(dcode, sym, cfg, val)); 00376 else 00377 return(1); 00378 } 00379 00380 00381 static char *decoder_dump = NULL; 00382 static unsigned decoder_dumplen = 0; 00383 00384 const char *_zbar_decoder_buf_dump (unsigned char *buf, 00385 unsigned int buflen) 00386 { 00387 int dumplen = (buflen * 3) + 12; 00388 if(!decoder_dump || dumplen > decoder_dumplen) { 00389 if(decoder_dump) 00390 free(decoder_dump); 00391 decoder_dump = malloc(dumplen); 00392 decoder_dumplen = dumplen; 00393 } 00394 char *p = decoder_dump + 00395 snprintf(decoder_dump, 12, "buf[%04x]=", 00396 (buflen > 0xffff) ? 0xffff : buflen); 00397 int i; 00398 for(i = 0; i < buflen; i++) 00399 p += snprintf(p, 4, "%s%02x", (i) ? " " : "", buf[i]); 00400 return(decoder_dump); 00401 } 00402
Generated on Tue Jul 12 2022 18:54:12 by 1.7.2