当前位置:网站首页>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. .
边栏推荐
- Programming in CoDeSys to realize serial communication [based on raspberry pie 4B]
- Fine tune layoutlm V3 for bill data processing and content recognition
- Yolo5face: why reinvent the face detector
- REST风格
- Dynamics crm: sharing records for users and teams
- Research on the efficiency of numpy array access
- 如何防止跨站点脚本 (XSS) 攻击完整指南
- Host PSQL connecting virtual machine Oracle
- Introduction to bermudagrass
- vscode常用快捷键
猜你喜欢

从哪些维度评判代码质量的好坏?如何具备写出高质量代码的能力?

Dynamics 365: how to get the authentication information required to connect to D365 online from azure

Telephone system rules

JUC source code learning note 3 - AQS waiting queue and cyclicbarrier, BlockingQueue

31 next spread

Dynamics 365: how to get the threshold value of executemullerequest in batch requests

Adaptive design and responsive design

Mysql8 encountered the problem of stopping after the service was started
[email protected]"/>ZBar project introduction and installation configuration| [email protected]

电话系统规则
随机推荐
Dedecms editor supports automatic pasting of word pictures
Who is the "roll" king of the prefabricated vegetable track?
Some understanding of the rank sum of matrix and the rank of image
安信证券开户在手机开户安全吗?
Rest style
AttributeError: module ‘seaborn‘ has no attribute ‘histplot‘
ZBar project introduction and installation configuration| [email protected]
矩阵的秩和图像的秩的一些了解
Dynamics crm: sharing records for users and teams
Getting started with OpenMP
By default, the select drop-down box selects the solution ligerui that the selected attribute does not work
Azure key vault (1) Introduction
With this machine learning drawing artifact, papers and blogs can get twice the result with half the effort!
Host PSQL connecting virtual machine Oracle
Deploy ZABBIX monitoring system and email alarm mechanism in lamp architecture
Code shoe set - mt2095 · zigzag jump
[SWT] scrolling container to realize commodity list style
应用修改日志路径log4j.properties
[leetcode] day103 search two-dimensional matrix II
【LOJ3247】「USACO 2020.1 Platinum」Non-Decreasing Subsequences(DP,分治)