ZBar是一种流行的二维码扫描和解码工具,它在嵌入式系统中拥有广泛的应用。在嵌入式系统中,我们面临着有限的资源和更严格的性能要求,因此,选择适当的库来完成特定的任务非常重要。
ZBar适用于各种嵌入式平台,包括ARM、x86和MIPS等处理器架构。它可以轻松地整合到各种嵌入式系统中,如智能家居设备、智能手机、平板电脑、远程控制设备、工业控制器等。
ZBar使用C/C++编写,具有高度优化的算法,能够快速准确地读取各种二维码和条形码,包括QR码、Data Matrix码、PDF417码、EAN-13码等等。同时,ZBar还支持自定义解码器,开发者可以根据自己的需求配置扫描器以实现更好的解码效果。
ZBar还具有非常灵活的API,可用于C/C++、Python、Java、Ruby等语言,开发人员可以根据自己的需求灵活选择相应的API。此外,ZBar还支持多种操作系统和平台,包括Linux、Windows、Mac OS X等。
总之,ZBar是一种非常有用的嵌入式二维码和条形码扫描库,它提供了高效的解码算法、可定制的解码器和灵活的API,能够轻松地满足嵌入式设备的扫描和解码需求。

这里感谢之前大佬移植zbar库到stm32,具体链接如下:https://www.cnblogs.com/greyorbit/p/8456814.html

移植步骤也很简单,按照博文把对应文件和头文件路径加入到工程中,然后使用图片数组转成灰度数据,在调用zbar既可以识别。

不过移植后会有一个问题,不能重复调用识别二维码,很容易内存就崩了。为了解决这个问题,让这个zbar库可以真正的用起来,不得不找到问题所在。

这里直观的看就是内存问题,奈何如果从源码直接去查找malloc和free的匹配所需时间太大,只能动态调试查找原因,所以第一步,我移植了rt-thread系统,使用rt的内存管理api。

移植rt-thread很方便,现在stm32代码生成工具cubemx可以直接添加rt-thread库,所以移植rt-thread系统很快。具体如下图:

移植完rt-thread后,就需要把zbar库中用到的malloc、calloc、free等操作函数换成 rt-malloc、rt-calloc、rt-free等,直接用全局搜索和替换。

替换后打开rt的内存调试功能宏定义:在rtdebug.h文件中

更改后既可以看到打印内存申请和释放日志,以下为日志打印内容

malloc size 156
allocate memory at 0x2001008c, size: 168
malloc size 296
allocate memory at 0x20010134, size: 308
malloc size 32
allocate memory at 0x20010268, size: 44
malloc size 48
allocate memory at 0x20010294, size: 60
malloc size 2856
allocate memory at 0x200102d0, size: 2868
malloc size 52
allocate memory at 0x20010e04, size: 64
allocate memory at 0x20010e04, size: 64
malloc size 16
allocate memory at 0x20010e44, size: 28
malloc size 20
allocate memory at 0x20011388, size: 32
malloc size 60
allocate memory at 0x200113a8, size: 72
release memory 0x20011388, size: 32
malloc size 140
allocate memory at 0x200113f0, size: 152
release memory 0x200113a8, size: 72
malloc size 300
allocate memory at 0x20011488, size: 312
release memory 0x200113f0, size: 152
malloc size 620
allocate memory at 0x200115c0, size: 632
release memory 0x20011488, size: 312
malloc size 1260
allocate memory at 0x20011838, size: 1272
release memory 0x200115c0, size: 632
malloc size 2540
allocate memory at 0x20011d30, size: 2552
release memory 0x20011838, size: 1272
malloc size 20
allocate memory at 0x20011388, size: 32
malloc size 60
allocate memory at 0x200113a8, size: 72
release memory 0x20011388, size: 32
malloc size 140
allocate memory at 0x200113f0, size: 152
release memory 0x200113a8, size: 72
malloc size 300
allocate memory at 0x20011488, size: 312
release memory 0x200113f0, size: 152
malloc size 620
allocate memory at 0x200115c0, size: 632
release memory 0x20011488, size: 312
malloc size 1260
allocate memory at 0x20011838, size: 1272
release memory 0x200115c0, size: 632
malloc size 2540
allocate memory at 0x20012728, size: 2552
release memory 0x20011838, size: 1272
malloc size 352
allocate memory at 0x20011388, size: 364
malloc size 352
allocate memory at 0x200114f4, size: 364
malloc size 88
allocate memory at 0x20011660, size: 100
release memory 0x20011660, size: 100
malloc size 440
allocate memory at 0x20011660, size: 452
malloc size 440
allocate memory at 0x20011824, size: 452
malloc size 110, but align to 112
allocate memory at 0x200119e8, size: 124
release memory 0x200119e8, size: 124
malloc size 5792
allocate memory at 0x20013120, size: 5804
malloc size 80
allocate memory at 0x200119e8, size: 92
malloc size 20
allocate memory at 0x20011a44, size: 32
malloc size 32
allocate memory at 0x20011a64, size: 44
malloc size 5, but align to 8
allocate memory at 0x20011a90, size: 24
malloc size 8
allocate memory at 0x20011aa8, size: 24
release memory 0x20011aa8, size: 24
release memory 0x20011a90, size: 24
release memory 0x20011a64, size: 44
release memory 0x20011a44, size: 32
release memory 0x20011824, size: 452
release memory 0x20011660, size: 452
release memory 0x200114f4, size: 364
release memory 0x20011388, size: 364
malloc size 57600
allocate memory at 0x200147cc, size: 57612
malloc size 960
allocate memory at 0x20011388, size: 972
release memory 0x20011388, size: 972
malloc size 3, but align to 4
allocate memory at 0x20011388, size: 24
malloc size 360
allocate memory at 0x200113a0, size: 372
release memory 0x200113a0, size: 372
malloc size 360
allocate memory at 0x200113a0, size: 372
release memory 0x200113a0, size: 372
malloc size 176
allocate memory at 0x200113a0, size: 188
release memory 0x200113a0, size: 188
malloc size 176
allocate memory at 0x200113a0, size: 188
release memory 0x200113a0, size: 188
malloc size 552
allocate memory at 0x200113a0, size: 564
malloc size 552
allocate memory at 0x200115d4, size: 564
release memory 0x200113a0, size: 564
release memory 0x200115d4, size: 564
malloc size 52
allocate memory at 0x200113a0, size: 64
allocate memory at 0x200113a0, size: 64
malloc size 116
allocate memory at 0x200113e0, size: 128
malloc size 32 allocate memory at 0x20011460, size: 44
malloc size 32
allocate memory at 0x2001148c, size: 44
release memory 0x20011460, size: 44
release memory 0x2001148c, size: 44
malloc size 116
allocate memory at 0x20011460, size: 128
malloc size 8
allocate memory at 0x200114e0, size: 24
malloc size 70, but align to 72
allocate memory at 0x200114f8, size: 84
release memory 0x200113e0, size: 128
release memory 0x200113a0, size: 64
release memory 0x200114e0, size: 24
release memory 0x20011460, size: 128
malloc size 12
allocate memory at 0x200113a0, size: 24
malloc size 15, but align to 16
allocate memory at 0x200113b8, size: 28
release memory 0x200114f8, size: 84
malloc size 48
allocate memory at 0x200113d4, size: 60
release memory 0x20011388, size: 24
malloc size 4
allocate memory at 0x20011388, size: 24
malloc size 1, but align to 4
allocate memory at 0x20011410, size: 24
malloc size 31, but align to 32
allocate memory at 0x20011428, size: 44
malloc size 52
allocate memory at 0x20011454, size: 64
allocate memory at 0x20011454, size: 64
malloc size 8
allocate memory at 0x20011494, size: 24
malloc size 16
allocate memory at 0x200114ac, size: 28
release memory 0x20011494, size: 24
malloc size 24
allocate memory at 0x200114c8, size: 36
release memory 0x200114ac, size: 28
malloc size 32
allocate memory at 0x20011494, size: 52
release memory 0x200114c8, size: 36
malloc size 16
allocate memory at 0x200114c8, size: 28
release memory 0x20011428, size: 44
release memory 0x20011410, size: 24
release memory 0x200113b8, size: 28
release memory 0x200113a0, size: 24
release memory 0x200113d4, size: 60
release memory 0x200147cc, size: 57612
release memory 0x200119e8, size: 92
release memory 0x20013120, size: 5804
n = 1 decoded QR-Code symbol "EEWorld STM32H5"
len = 15
release memory 0x20010e04, size: 64
release memory 0x20011494, size: 52
release memory 0x200114c8, size: 28
release memory 0x20011454, size: 64
release memory 0x20010e44, size: 28
release memory 0x20010294, size: 60
release memory 0x20010268, size: 44
release memory 0x20010134, size: 308
release memory 0x20011d30, size: 2552
release memory 0x20012728, size: 2552
release memory 0x200102d0, size: 2868
release memory 0x2001008c, size: 168
45 45 57 6F 72 6C 64 20 53 54 4D 33 32 48 35 zbar ok
zbar count;56

 通过对申请和释放的对应关系,我们可以分析得出问题所在,zbar库在图片识别后释放了img->data指针,而这个指针是在zbar调用外部申请的空间,是不需要zbar内部释放的,具体代码如下:

int main(void)
{
/* USER CODE BEGIN 1 */
uint8_t test[]="start test\n";
uint16_t i,j;
int qr_img_width = 240; uint16_t Color;
uint16_t cnt = 0; unsigned char *pic_rgb = (unsigned char *)gImage_test;
unsigned char *pic_hd = NULL;
unsigned char *pic_data = NULL; void * ptr_start; /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
//HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */
//SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_ETH_Init();
MX_ICACHE_Init();
MX_LPUART1_UART_Init();
//MX_USART3_UART_Init();
MX_UCPD1_Init();
MX_USB_PCD_Init();
/* USER CODE BEGIN 2 */ pic_data = rt_malloc(qr_img_width*qr_img_width);
if(pic_data == NULL)
{
printf("malloc error\n");
return 0;
}
else
{
printf("pic_data:0x%x\n",pic_data);
}
//memset(pic_data,0,qr_img_width*qr_img_width);
pic_hd = pic_data;
for(i=0;i<qr_img_width;i++)
{
for(j=0;j<qr_img_width;j++) //将RGB565图片转成灰度
{ Color = (*pic_rgb) | (*(pic_rgb+1)<<8);
*pic_hd = (((Color&0xF800)>> 8)*77+((Color&0x7E0)>>3)*150+((Color&0x001F)<<3)*29)/256;
pic_hd++;
pic_rgb++;
pic_rgb++; }
} /* USER CODE END 2 */ /* Infinite loop */
/* USER CODE BEGIN WHILE */ while (1)
{
/* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ if( Zbar_Test((void* )pic_data,qr_img_width,qr_img_width) == 0 )
{
printf("zbar failed \n");
//rt_free(pic_data);
}
else
{
cnt ++;
printf("zbar ok \n");
//rt_free(pic_data);
}
printf("zbar count;%d\n",cnt);
//list_thread();
rt_thread_mdelay(5000); }
rt_free(pic_data);
/* USER CODE END 3 */
}

  即pic_data灰度图片数据是不需要zbar释放的,但是zbar库中做了释放操作,代码如下:

inline void zbar_image_rt_free_data (zbar_image_t *img)
{
if(!img)
return;
if(img->src) {
/* replace video image w/new copy */
assert(img->refcnt); /* FIXME needs lock */
zbar_image_t *newimg = zbar_image_create();
memcpy(newimg, img, sizeof(zbar_image_t));
/* recycle video image */
newimg->cleanup(newimg);
/* detach old image from src */
img->cleanup = NULL;
img->src = NULL;
img->srcidx = -1;
}
else if(img->cleanup && img->data) {
if(img->cleanup != zbar_image_rt_free_data) {
/* using function address to detect this case is a bad idea;
* windows link libraries add an extra layer of indirection...
* this works around that problem (bug #2796277)
*/
zbar_image_cleanup_handler_t *cleanup = img->cleanup;
img->cleanup = zbar_image_rt_free_data;
cleanup(img);
}
//传入图片为外部指针,zbar内部不用free此指针
// else
// rt_free((void*)img->data);
}
img->data = NULL;
}

  这里把这一句屏蔽,则可以解决问题,经过我测试,现在已经连续运行上千次

decoded QR-Code symbol "EEWorld STM32H5"
len = 15
45 45 57 6F 72 6C 64 20 53 54 4D 33 32 48 35 zbar ok
zbar count;4290
n = 1 decoded QR-Code symbol "EEWorld STM32H5"
len = 15
45 45 57 6F 72 6C 64 20 53 54 4D 33 32 48 35 zbar ok
zbar count;4291
n = 1 decoded QR-Code symbol "EEWorld STM32H5"
len = 15
45 45 57 6F 72 6C 64 20 53 54 4D 33 32 48 35 zbar ok
zbar count;4292
n = 1 decoded QR-Code symbol "EEWorld STM32H5"
len = 15
45 45 57 6F 72 6C 64 20 53 54 4D 33 32 48 35 zbar ok
zbar count;4293
n = 1 decoded QR-Code symbol "EEWorld STM32H5"
len = 15
45 45 57 6F 72 6C 64 20 53 54 4D 33 32 48 35 zbar ok
zbar count;4294

 

STM32H5移植zbar记录的更多相关文章

  1. 【linux】linux内核移植错误记录

       欢迎转载,转载时请保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http ...

  2. linux移植问题记录

    问题一 ~/linux/linux-5.2.8$ make s3c2410_defconfig  HOSTCC  scripts/basic/fixdep/bin/sh: 1: scripts/bas ...

  3. 【转】(笔记)CANopen协议【CANFestival】移植方法

    一.背景 CAN组网就必须得要应用层协议,原因就在于 * 便于网络管理与控制 * 确认数据的收发 * 发送大于8个字节的数据块(CAN每帧数据传输大小为8字节) * 为不同节点分配不同的报文标识符 * ...

  4. MIPS平台移植apache 2.2.7

    参考文章: http://wenku.baidu.com/view/94e08a20a5e9856a561260e2.html http://httpd.apache.org/docs/2.4/ins ...

  5. 移植u-boot-2012.04.01到JZ2440

    开发环境:Ubuntu 12.04 开发板:JZ2440  256M NandFlash  64M SDRAM 交叉编译器:arm-linux-gcc-4.3.2 u-boot:u-boot-2012 ...

  6. nRF52832 SDK15.3.0 基于ble_app_uart demo FreeRTOS移植

    参考资料:https://blog.csdn.net/u010860832/article/details/86235993 这里把移植经验记录下来,供有需要的同学参考,有不对的地方也请大家批评指正. ...

  7. perf 移植

    perf 移植 perf工具用于系统性能的调优,程序优化.源码在kenel/tools/perf目录. 我在imx6平台上进行移植.将自己的移植过程记录如下. 参考链接 http://blog.csd ...

  8. Zbar和Z*算法对比

    博客转载自:https://blog.csdn.net/qishandaxue/article/details/45481387 移植zbar和zxing源码到linux平台,zbar移植的是C源码, ...

  9. EntityFramework 优化建议

    Entity Framework目前最新版本是6.1.3,当然Entity Framework 7 目前还是预览版,并不能投入正式生产环境,估计正式版16年第一季度会出来,了解过EF7的部分新特性后, ...

  10. C#实用杂记-EF全性能优化技巧

    原文链接:http://www.makmong.com/947.html#comment-31 EntityFramework 优化建议 2016年1月15日 下午4:54 LEILINKANG   ...

随机推荐

  1. ElasticSearch 实现分词全文检索 - 高亮查询

    目录 ElasticSearch 实现分词全文检索 - 概述 ElasticSearch 实现分词全文检索 - ES.Kibana.IK安装 ElasticSearch 实现分词全文检索 - Rest ...

  2. ChatGPT能给IOT行业带来哪些改变

    引言 随着移动互联网.传感器的发展,移动互联的潮流逐渐转移到物联网行业,每个设备成为了物联网连接的终端. 与传统的设备相比,智能设备最突出的特点就是智能化.目前,在市场上的智能设备通过智能程序设定或者 ...

  3. active

    rabbitMQ与activeMQ区别 之前的项目中都用到了这两个消息队列 ActiveMq,传统的消息队列,使用Java语言编写.基于JMS(Java Message Service),采用多线程并 ...

  4. CTF-RE-学习记录-汇编

    八进制运算 加法表 1+1=2 1+2=3 2+2=4 1+3=4 2+3=5 3+3=6 1+4=5 2+4=6 3+4=7 4+4=10 1+5=6 2+5=7 3+5=8 4+5=11 5+5= ...

  5. 在线编写Markdown

    部署 editor.md 实现在线编写MD 安装Nginx服务 apt install nginx yum install nginx 修改Nginx配置 root@cby:~# vim /etc/n ...

  6. docker方式实现redis数据持久化离线安装

    保存镜像 root@hello:~# docker pull redis:latest latest: Pulling from library/redis a2abf6c4d29d: Already ...

  7. MySQL 读书笔记(一)

    1 MySQL 表 1.1 索引组织表 在 InnoDB 存储引擎中,表都是根据主键顺序存放的,这种存储方式称为索引组织表. InnoDB存储引擎中,每张 MySQL表 都有一个唯一主键,如果创建表时 ...

  8. PYTHON数据分析——python基础

    利用命令行创建python文件 C:\Users\Your Name>python myfile.py Python 变量命名规则: 变量名必须以字母或下划线字符开头 变量名称不能以数字开头 变 ...

  9. Nginx部署多个vue前端项目

    前言:在前端项目的部署上需要让2个前端项目都部署到一个IP地址和端口下,那么我们这里就要用到Nginx了,接下来我们看看如何在一个Nginx下部署2个前端项目. 例如我的服务器地址是 http://1 ...

  10. MQTT-主题基础

    MQTT主题 MQTT的主题是一个utf-8编码的字符串,最大长度65535字节,严格区分大小写 MQTT主题支持分层结构,主题分隔符用'/'表示,主题的层级长度可以为0 # 将主题划分为3个层级 ' ...