首先看看識別的效果:


1.下載安裝 ImageMagick zBar
2.新建頭文件
barcode.h
#include <iostream>
#include <string>
//引入頭文件
#include ".\ZBar\include\zbar.h"
#include ".\ImageMagick\include\Magick++.h"
using namespace std;
using namespace zbar;
//加載lib文件
#pragma comment( lib, ".\\ImageMagick\\lib\\CORE_RL_Magick++_.lib" )
#pragma comment( lib, ".\\ImageMagick\\lib\\CORE_RL_MagickCore_.lib" )
#pragma comment( lib, ".\\ImageMagick\\lib\\CORE_RL_MagickWand_.lib" )
#pragma comment( lib, ".\\ZBar\\lib\\libzbar-0.lib" )
//解析 條碼 二維碼圖片
//失敗返回0 成功返回非0 file 圖片路徑 tname條碼類型 zdata條碼
static int getzbar(const char* file, string &tname, string &zdata)
{
int err = 0;
#ifdef MAGICK_HOME
// under Windows it is necessary to initialize the ImageMagick
// library prior to using the Magick++ library
// MAGICK_HOME = STR(C:\Program Files(x86)\ImageMagick - 6.9.1 - Q16)
Magick::InitializeMagick(MAGICK_HOME);
#endif
// create a reader
ImageScanner scanner;
// configure the reader
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
try
{
// obtain image data
Magick::Image magick(file); // 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
Image image(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)
{
tname = symbol->get_type_name();
zdata = symbol->get_data();
err++;
}
// clean up
image.set_data(NULL, 0);
}
catch (std::exception &ex)
{
std::cout << ex.what() << std::endl;
return -1;
}
return err;
}
#include <iostream>
#include "Barcode.h"
using namespace std;
void main()
{
string type, text;
if (getzbar("img\\barcode.png", type, text))
{
cout << "類型:" << type << "序列號:" << text << endl;
}
else{
cout << "識別失敗" << endl;
}
if (getzbar("img\\IMG_0146.JPG", s, b)) cout << "類型:" << s << "序列號:" << b << endl;
if (getzbar("img\\IMG_0226.JPG", s, b)) cout << "類型:" << s << "序列號:" << b << endl;
if (getzbar("img\\IMG_0227.JPG", s, b)) cout << "類型:" << s << "序列號:" << b << endl;
if (getzbar("img\\IMG_0228.JPG", s, b)) cout << "類型:" << s << "序列號:" << b << endl;
if (getzbar("img\\kkk.bmp", s, b)) cout << "類型:" << s << "序列號:" << b << endl;
if (getzbar("img\\liantu.png", s, b)) cout << "類型:" << s << "序列號:" << b << endl;
system("pause");
}
企鵝交流:0x7317AF28
