OpenCV+zbar开源库实现摄像头识别二维码,测试验证识别率非常高,已实现简单的应用。
打包源码在VS2013下可以完全编译成功,附加包含OpenCV库及zbar-0.10-setup.exe,zbar-0.10.tar.bz2
下载Demo后需要安装 zbar-0.10-setup.exe
以下代码可以可以完成整个流程的开发,也可以贡献积分下载资源包。
1、 环境准备
(1) OpenCV库2.49
(2) ZBar开源库
(3) VS2013
2、 VS2013环境配置
(1) 配置附加包含目录 C/C++ -- 附加包含目录
include\opencv\include\
include\opencv\include\opencv
include\opencv\include\opencv2
include
(2) 配置链接器 链接器 -- 附加库目录
lib32\opencv\lib
lib32
(3) 配置链接器 链接器--输入--附加依赖项
opencv_core249d.lib
opencv_highgui249d.lib
opencv_imgproc249d.lib
libzbar-0.lib
3、 代码开发
(1)包含头文件
include
include
include
include
include
include
using namespace std;
using namespace zbar;
using namespace cv;
(2)实现函数
void MatToCImage(cv::Mat &mat, CImage &cImage)
{
//create new CImage
int width = mat.cols;
int height = mat.rows;
int channels = mat.channels();
cImage.Destroy(); //clear
cImage.Create(width, height, 8 * channels); //默认图像像素单通道占用1个字节
//copy values
uchar* ps;
uchar* pimg = (uchar*)cImage.GetBits(); //A pointer to the bitmap buffer
int step = cImage.GetPitch();
for (int i = 0; i (i));
for (int j = 0; j GetDlgItem(IDC_STATIC_IMG)->GetClientRect(▭);
cv::VideoCapture capture(0);//从摄像头读入图像
while (!m_bCloseCamera)
{
cv::Mat frame;
capture >> frame;
cv::Mat newframe;
cv::Size ResImgSiz = cv::Size(rect.Width(), rect.Height());
cv::resize(frame, newframe, ResImgSiz, CV_INTER_CUBIC);
MatToCImage(newframe, imgDst);
imgDst.Draw(pThis->GetDlgItem(IDC_STATIC_IMG)->GetDC()->GetSafeHdc(), rect);
ImageScanner scanner;
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
Mat imageGray;
cvtColor(frame, imageGray, CV_RGB2GRAY);
int width = imageGray.cols;
int height = imageGray.rows;
uchar *raw = (uchar *)imageGray.data;
Image imageZbar(width, height,
1