当前位置:网站首页>ZBar source code analysis - img_ scanner. c | [email protected]
ZBar source code analysis - img_ scanner. c | [email protected]
2022-07-24 16:15:00 【Mai゛】
[email protected]
One 、ZBar workflow

Through analysis ZBar Project structure , You can see ZBar The workflow of is roughly divided into 4 A step :
( One ) Read in images and configure parameters ;
( Two ) Scan the read image and analyze its light and dark width flow according to the gradient change ( According to the light and dark width flow, the barcode type read into the image can be obtained , Such as : QR code ,code93,code128 etc. );
( 3、 ... and ) Analyze the pixels and their characteristics of the read image ;
( Four ) Find the barcode format information and decode , Finally, output the recovered codeword .
Two 、Image Scanner
Image Scanner, As the name suggests, it is a functional module to scan the read image . Of this secondary analysis scanner.c It is one of the cores of this functional module .
ZBar Realization Image Scanner The core of is mainly composed of img_scanner.c and scanner.c Two documents .
among ,img_scanner.c The core function in is zbar_scan_image(), and scanner.c The core function in is zbar_scan_y(). After simple analysis, we get ,zbar_scan_image Mainly responsible for ZBar Scanning the read in image , The function is mainly based on the set scanning density (density) Control pixel reading ( Press Z Font reading , This is also ZBar The origin of the name ),scanner.c In file zbar_scan_y() To complete the filtering , threshold , Determine the edge , Convert to width flow .
zbar_scan_image() The implementation of needs the help of zbar_scan_y() The scanning image forms the result of width flow .
3、 ... and 、img_scanner.c
This source code analysis mainly focuses on img_scanner.c Document analysis ( Main analysis zbar_scan_image() function ).
zbar_image_scanner_create() establish ( install ) Picture scanner Image Scanner It is applied to decoding and recognition of various bar codes .
// Function implementation of creating image scanner
zbar_image_scanner_t *zbar_image_scanner_create ()
{
zbar_image_scanner_t *iscn = calloc(1, sizeof(zbar_image_scanner_t));
// If the creation fails , return NULL
if(!iscn)
return(NULL);
// Initialize the picture scanner ( Initialize the decoder and scanner in the picture scanner )
iscn->dcode = zbar_decoder_create();
iscn->scn = zbar_scanner_create(iscn->dcode);
// If decoder or scanner initialization fails , return NULL
if(!iscn->dcode || !iscn->scn) {
zbar_image_scanner_destroy(iscn);
return(NULL);
}
// Apply the picture scanner to the decoder
zbar_decoder_set_userdata(iscn->dcode, iscn);
zbar_decoder_set_handler(iscn->dcode, symbol_handler);
#ifdef ENABLE_QRCODE
iscn->qr = _zbar_qr_create();
#endif
// Apply the picture scanner to the recognition of various bar codes
CFG(iscn, ZBAR_CFG_X_DENSITY) = 1;
CFG(iscn, ZBAR_CFG_Y_DENSITY) = 1;
zbar_image_scanner_set_config(iscn, 0, ZBAR_CFG_POSITION, 1);
zbar_image_scanner_set_config(iscn, 0, ZBAR_CFG_UNCERTAINTY, 2);
zbar_image_scanner_set_config(iscn, ZBAR_QRCODE, ZBAR_CFG_UNCERTAINTY, 0);
zbar_image_scanner_set_config(iscn, ZBAR_CODE128, ZBAR_CFG_UNCERTAINTY, 0);
zbar_image_scanner_set_config(iscn, ZBAR_CODE93, ZBAR_CFG_UNCERTAINTY, 0);
zbar_image_scanner_set_config(iscn, ZBAR_CODE39, ZBAR_CFG_UNCERTAINTY, 0);
zbar_image_scanner_set_config(iscn, ZBAR_CODABAR, ZBAR_CFG_UNCERTAINTY, 1);
zbar_image_scanner_set_config(iscn, ZBAR_COMPOSITE, ZBAR_CFG_UNCERTAINTY, 0);
return(iscn);
}
When you need to uninstall Image Scanner when , Not directly free(img_scanner) Free up memory , This will directly lead to ZBar Scanner zbar_scanner、ZBar decoder zbar_decoder And all kinds of bar code recognition applications have null pointer exceptions . So we need to judge Image Scanner Usage in various components and Applications , If you are using , You need to remove Image Scanner Application , Finally, the memory can be directly released .
// Unloading of image scanner
void zbar_image_scanner_destroy (zbar_image_scanner_t *iscn)
{
int i;
dump_stats(iscn);
// Determine the status of the image scanner , If it is in the application state , You cannot uninstall
if(iscn->syms) {
if(iscn->syms->refcnt)
zbar_symbol_set_ref(iscn->syms, -1);
else
_zbar_symbol_set_free(iscn->syms);
iscn->syms = NULL;
}
// Determine whether the image scanner is applied to ZBar Scanner , If it is , From ZBar Unload on the scanner
if(iscn->scn)
zbar_scanner_destroy(iscn->scn);
iscn->scn = NULL;
Determine whether the image scanner is applied to ZBar decoder , If it is , From ZBar Uninstall on the decoder
if(iscn->dcode)
zbar_decoder_destroy(iscn->dcode);
iscn->dcode = NULL;
// Unload the picture scanner from the recognition application of various bar codes
for(i = 0; i < RECYCLE_BUCKETS; i++) {
zbar_symbol_t *sym, *next;
for(sym = iscn->recycle[i].head; sym; sym = next) {
next = sym->next;
_zbar_symbol_free(sym);
}
}
#ifdef ENABLE_QRCODE
if(iscn->qr) {
_zbar_qr_destroy(iscn->qr);
iscn->qr = NULL;
}
#endif
free(iscn);
}
The following is true. zbar_scan_image() Analysis of some code fragments :
zbar_scan_image() Mainly achieved Z Font scanning image density , The core idea is through scanning density (density) To control pixel reading .
The function is divided into two parts: scanning the image horizontally and vertically , The implementation logic is not much different , The code of the longitudinal scanning part is given below .
density = CFG(iscn, ZBAR_CFG_Y_DENSITY);
if(density > 0) {
const uint8_t *p = data;
int x = 0, y = 0;
int border = (((img->crop_h - 1) % density) + 1) / 2;
if(border > img->crop_h / 2)
border = img->crop_h / 2;
border += img->crop_y;
assert(border <= h);
svg_group_start("scanner", 0, 1, 1, 0, 0);
iscn->dy = 0;
movedelta(img->crop_x, border);
iscn->v = y;
while(y < cy1) {
int cx0 = img->crop_x;;
zprintf(128, "img_x+: %04d,%04d @%p\n", x, y, p);
svg_path_start("vedge", 1. / 32, 0, y + 0.5);
iscn->dx = iscn->du = 1;
iscn->umin = cx0;
while(x < cx1) {
uint8_t d = *p;
movedelta(1, 0);
zbar_scan_y(scn, d);
}
ASSERT_POS;
quiet_border(iscn);
svg_path_end();
movedelta(-1, density);
iscn->v = y;
if(y >= cy1)
break;
zprintf(128, "img_x-: %04d,%04d @%p\n", x, y, p);
svg_path_start("vedge", -1. / 32, w, y + 0.5);
iscn->dx = iscn->du = -1;
iscn->umin = cx1;
while(x >= cx0) {
uint8_t d = *p;
movedelta(-1, 0);
zbar_scan_y(scn, d);
}
ASSERT_POS;
quiet_border(iscn);
svg_path_end();
movedelta(1, density);
iscn->v = y;
}
svg_group_end();
}
iscn->dx = 0;
This part of the code mainly realizes the longitudinal scanning image density , Because the calling function relationship is complex , If you only look at the coordinates , It can be simplified to the following code :
// First judge whether there is a setting y density
if(ydensity > 0)
{
while(y < h)
//y from 0 With ydensity Increase to h
{
while(x < w)
//x First from 0 Increase to w, Re decrement back 0
{
x += 1;
zbar_scan_y();
}
x = w - 1;
y = y + ydensity
//y from 0 With ydensity Increase to h
if(y >= h)
break;
while(x >= 0)
// x Begin to decline
{
x -= 1;
zbar_scan_y();
}
x = 0 + 1;
y = y + ydensity;
}
// Then judge x Directional scanning density , Empathy Z Glyph scanning
}w = img->width; h = img->height;
In the above code w Represents the width of the image ,h Represents the height of the image , The function is called repeatedly during the loop zbar_scan_y() To complete the filtering , threshold , Determine the edge , Convert to width flow .
Draw according to the code , You can roughly get ZBar The order of scanning image pixels is roughly as follows :

The above is the code analysis report , If there is a mistake , Please correct me. .
边栏推荐
- 287 finding duplicates
- Leetcode 223. 矩形面积
- Varnish4.0 cache agent configuration
- Fast RCNN trains its own data set
- Fine tune layoutlm V3 for bill data processing and content recognition
- vscode常用快捷键
- yolov4 训练自己的数据集
- Lsyncd real time synchronization
- Will the capital market be optimistic about TCL's folding screen story?
- 3、 Set foundation ArrayList set and simple student management system
猜你喜欢

Hard core innovation that database needs to care about in the future

机器学习笔记 - 构建推荐系统(5) 前馈神经网络用于协同过滤

253 Conference Room II

Introduction to kettle messy notes

普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像

287 finding duplicates

AttributeError: module ‘seaborn‘ has no attribute ‘histplot‘
![[SWT] user defined data table](/img/bf/a0c60f1ac9461874b8a573f805e1fe.png)
[SWT] user defined data table

Using JS to implement click events

124 maximum path sum in binary tree
随机推荐
What is the ranking of good securities companies? Is online account opening safe
Windows10安装免安装版redis
【LOJ3247】「USACO 2020.1 Platinum」Non-Decreasing Subsequences(DP,分治)
kubernetes GPU的困境和破局
Code shoe set - mt2095 · zigzag jump
Will the capital market be optimistic about TCL's folding screen story?
Use of ec200u-cn module
20. Shell programming variables
C TCP client form application asynchronous receiving mode
MySQL converts strings to numeric types and sorts them
机器学习笔记 - 构建推荐系统(5) 前馈神经网络用于协同过滤
Caikeng Alibaba cloud Kex_ exchange_ identification: read: Connection reset by peer
Configuring WAPI certificate security policy for Huawei wireless devices
Software - prerequisite software
Leetcode 220. 存在重复元素 III
Fast RCNN trains its own data set
Software recommendation - Mechanical Major
Host PSQL connecting virtual machine Oracle
聊聊C指针
With this machine learning drawing artifact, papers and blogs can get twice the result with half the effort!