zbar条形码识别库vs配置

栏目: Perl · 发布时间: 5年前

内容简介:ZBar是一个开源 C 条形码阅读库,具有C ++,Python,Perl和Ruby 接口。它也作为命令行应用程序和iPhone应用程序在Linux和Microsoft Windows上实现。具有如下特点:首先先到zbar官网下载安装包,

ZBar是一个开源 C 条形码阅读库,具有C ++,Python,Perl和 Ruby 接口。它也作为命令行应用程序和iPhone应用程序在 Linux 和Microsoft Windows上实现。

具有如下特点:

  • 图像扫描
  • 实时扫描视频流
  • C ++,Python,Perl和Ruby的api接口
  • Qt,GTK +和PyGTK GUI的api接口
  • 对EAN-13,UPC-A,UPC-E,EAN-8,Code 128,Code 39,Interleaved 2 of 5和QR码 符号的识别

安装

下载

首先先到zbar官网下载安装包, 下载链接

安装

下载后,点击安装程序,这是一个自解压程序,类似opencv的安装程序,安装到自己所需要的目录就可以。

安装步骤除了两个步骤,其它都按照默认点就好。一个是选择你自己需要安装的路径,默认的安装路径C盘

另一个步骤则是到这里,选择头文件(Headers)和库文件目录(Libraries)的创建,用以接下来的配置。

zbar条形码识别库vs配置

配置

配置过程与opencv的配置类似,步骤是一样的。

这是自解压安装的文件夹,其中bin、include、lib目录是配置需要用到的。

zbar条形码识别库vs配置

  1. 环境变量的配置, bin目录添加到系统的path
    zbar条形码识别库vs配置
  2. 打开vs,新建空项目,添加main.cpp文件。在属性管理器(选x86,亲测不支持x64),右键新建属性表。

zbar条形码识别库vs配置

  1. 编辑新建的属性表。 vc++包含目录填include文件夹路径。库目录填lib文件夹路径。链接器->输入->附加依赖项。填写libzbar-0.lib

zbar条形码识别库vs配置

zbar条形码识别库vs配置

测试

安装文件夹里面有官方示例代码如下。

#include<iostream>
#include<Magick++.h>
#include<zbar.h>
#defineSTR(s) #s

using namespace std;
using namespace zbar;

int main(int argc, char **argv)
{
    if(argc < 2) return(1);

#ifdefMAGICK_HOME
    // http://www.imagemagick.org/Magick++/
    // under Windows it is necessary to initialize the ImageMagick
    // library prior to using the Magick++ library
    Magick::InitializeMagick(MAGICK_HOME);
#endif

    // create a reader
    ImageScanner scanner;

    // configure the reader
    scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

    // obtain image data
    Magick::Imagemagick(argv[1]);  // read an image file
    int width = magick.columns();   // extract dimensions
    int height = magick.rows();
    Magick::Blob blob;              // extract the raw data
    magick.modifyImage();
    magick.write(&blob, "GRAY", 8);
    const void *raw = blob.data();

    // wrap image data
    Imageimage(width, height,"Y800", raw, width * height);

    // scan the image for barcodes
    int n = scanner.scan(image);

    // extract results
    for(Image::SymbolIterator symbol = image.symbol_begin();
        symbol != image.symbol_end();
        ++symbol) {
        // do something useful with results
        cout << "decoded " << symbol->get_type_name()
             << " symbol \"" << symbol->get_data() << '"' << endl;
    }

    // clean up
    image.set_data(NULL, 0);

    return(0);
}

但是示例代码中使用了imageMagick++这个第三方库。没接触使用过,这里就稍加修改,使用opencv代替imageMagick++读入图片的功能。

#include<opencv2/opencv.hpp>
#include<iostream>
#include<zbar.h>

using namespace std;
using namespace zbar;

int main(int argc, char **argv)
{

	// create a reader
	ImageScanner scanner;

	// configure the reader
	scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

	// obtain image data
	cv::Mat image = cv::imread("wechat.jpg");  // read an image file

	if (image.empty())  return -1;
	
	int width = image.rows;						// extract dimensions
	int height = image.cols;

	cv::Mat img_gray;
	cv::cvtColor(image, img_gray, cv::COLOR_BGR2GRAY);

	uchar *raw = (uchar*)img_gray.data;

	// wrap image data
	Imageimage_zbar(width, height,"Y800", raw, width * height);

	// scan the image for barcodes
	int n = scanner.scan(image_zbar);

	if (image_zbar.symbol_begin() == image_zbar.symbol_end())
	{
		cout << "Failed to scan, please check the image!" << endl;
	}
	// extract results
	for (Image::SymbolIterator symbol = image_zbar.symbol_begin();
		symbol != image_zbar.symbol_end();
		++symbol) {
		// do something useful with results
		cout << "decoded " << symbol->get_type_name()
			<< " symbol \"" << symbol->get_data() << '"' << endl;
	}

	cv::imshow("barcode", image);
	cv::waitKey(0);
	// clean up
	image_zbar.set_data(NULL, 0); 
	return(0);
}

以上所述就是小编给大家介绍的《zbar条形码识别库vs配置》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Speed Up Your Site

Speed Up Your Site

Andrew B. King / New Riders Press / 2003-01-14 / USD 39.99

There's a time bomb on the web: user patience. It starts ticking each time someone opens one of your pages. You only have a few seconds to get compelling content onto the screen. Fail, and you can kis......一起来看看 《Speed Up Your Site》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具