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 memo