#include<iostream>
#include "zbar.h"
#include <opencv2/opencv.hpp>
#include<codecvt>
using namespace std;
using namespace zbar; //添加zbar名称空间
using namespace cv;
bool WideStringToString(const std::wstring& src, std::string &str)
{
std::locale sys_locale("");
const wchar_t* data_from = src.c_str();
const wchar_t* data_from_end = src.c_str() + src.size();
const wchar_t* data_from_next = 0;
int wchar_size = 4;
char* data_to = new char[(src.size() + 1) * wchar_size];
char* data_to_end = data_to + (src.size() + 1) * wchar_size;
char* data_to_next = 0;
memset(data_to, 0, (src.size() + 1) * wchar_size);
typedef std::codecvt<wchar_t, char, mbstate_t> convert_facet;
mbstate_t out_state = { 0 };
auto result = std::use_facet<convert_facet>(sys_locale).out(
out_state, data_from, data_from_end, data_from_next,
data_to, data_to_end, data_to_next);
if (result == convert_facet::ok)
{
str = data_to;
delete[] data_to;
return true;
}
delete[] data_to;
return false;
}
bool StringToWideString(const std::string& src, std::wstring &wstr)
{
std::locale sys_locale("");
const char* data_from = src.c_str();
const char* data_from_end = src.c_str() + src.size();
const char* data_from_next = 0;
wchar_t* data_to = new wchar_t[src.size() + 1];
wchar_t* data_to_end = data_to + src.size() + 1;
wchar_t* data_to_next = 0;
wmemset(data_to, 0, src.size() + 1);
typedef std::codecvt<wchar_t, char, mbstate_t> convert_facet;
mbstate_t in_state = { 0 };
auto result = std::use_facet<convert_facet>(sys_locale).in(
in_state, data_from, data_from_end, data_from_next,
data_to, data_to_end, data_to_next);
if (result == convert_facet::ok)
{
wstr = data_to;
delete[] data_to;
return true;
}
delete[] data_to;
return false;
}
bool WCharStringToUTF8String(const std::wstring &wstr, std::string &u8str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
u8str = conv.to_bytes(wstr);
return true;
}
bool UTF8StringToWCharString(const std::string &u8str, std::wstring &wstr)
{
std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;
wstr = conv.from_bytes(u8str);
return true;
}
//当类型为条形码时候,用来确定位置
void calcu_grads(Mat& src) {
Mat imageGray, imageGuussian;
Mat imageSobelX, imageSobelY, imageSobelout;
//原图像大小调整,提高效率
//resize(src,image,Size(500,500));
//imshow("二维码原图",image);
cvtColor(src,imageGray,CV_BGR2GRAY);
GaussianBlur(imageGray,imageGuussian,Size(3,3),0);
Mat imageX16S, imageY16S;
Sobel(imageGuussian,imageX16S,CV_16S,1,0,3,1,0,4);
Sobel(imageGuussian, imageY16S, CV_16S, 0, 1, 3, 1, 0, 4);
convertScaleAbs(imageX16S,imageSobelX,1,0);
convertScaleAbs(imageY16S,imageSobelY,1,0);
imageSobelout = imageSobelX - imageSobelY;
blur(imageSobelout,imageSobelout,Size(3,3));
Mat imageThreshold;
threshold(imageSobelout,imageThreshold,180,255,CV_THRESH_BINARY);
Mat element = getStructuringElement(0,Size(7,7));
//开操作:先腐蚀后膨胀的操作称之为开操作。它具有消除细小物体,在纤细处分离物体和平滑较大物体边界的作用。
//闭操作:先膨胀后腐蚀的操作称之为闭操作。它具有填充物体内细小空洞,连接邻近物体和平滑边界的作用。
morphologyEx(imageThreshold,imageThreshold,MORPH_CLOSE,element);
//imshow("threshold", imageThreshold);
erode(imageThreshold,imageThreshold,element);
//imshow("erode", imageThreshold);
dilate(imageThreshold,imageThreshold,element);
dilate(imageThreshold, imageThreshold, element);
dilate(imageThreshold, imageThreshold, element);
//imshow("2.膨胀图",imageThreshold);
vector<vector<Point>> contours;
vector<Vec4i> hiera;
//CV_CHAIN_APPROX_NONE获取轮廓上所有像素点
//CV_RETR_EXTERNALCV_RETR_EXTERNAL只检测最外围轮廓,包含在外围轮廓内的内围轮廓被忽略,
//CV_RETR_LIST 检测所有的轮廓,包括内围、外围轮廓,但是检测到的轮廓不建立等级关系,彼此之间独立,没有等级关系,这就意味着这个检索模式下不存在父轮廓或内嵌轮廓,
findContours(imageThreshold,contours,hiera,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
int max_area = 0;
int max_index=0;
for (int i = 0; i < contours.size(); i++) {
Rect rect = boundingRect((Mat)contours[i]);
if (rect.area() > max_area)
{
max_area = rect.area();
max_index = i;
}
}
Rect temp = boundingRect((Mat)contours[max_index]);
rectangle(src,temp,Scalar(0,0,255),3);
}
void ZbarDecoder(Mat& img) {
ImageScanner scanner;
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
Mat imgout;
cvtColor(img, imgout, CV_RGB2GRAY);
int width = img.cols;
int height = img.rows;
uchar *raw = (uchar *)imgout.data;
Image image(width, height, "Y800", raw, width * height);
int n = scanner.scan(image);
Image::SymbolIterator symbol = image.symbol_begin();
for (; symbol != image.symbol_end(); ++symbol)
{
vector<Point> vp;
cout << "Decoded:"<<symbol->get_type_name() << endl << endl;
wstring str;
string str1;
UTF8StringToWCharString(symbol->get_data(),str);
WideStringToString(str, str1);
cout << str1;
//cout << "Symbol:"<< symbol->get_data() << endl << endl;
if (symbol->get_type_name()=="QR-Code") {
int n = symbol->get_location_size();
for (int i = 0; i<n; i++)
{
vp.push_back(Point(symbol->get_location_x(i), symbol->get_location_y(i)));
}
RotatedRect r = minAreaRect(vp);
Point2f pts[4];
r.points(pts);
for (int i = 0; i < 4; i++)
{
line(img, pts[i], pts[(i + 1) % 4], Scalar(0,0,255), 3);
}
}
else {
Mat res;
calcu_grads(img);
}
}
imshow("img", img);
waitKey();
}
int main()
{
Mat image = imread("w3.jpg");
if (!image.data)
{
cout << "The picture is empty!" << endl;
system("pause");
return 0;
}
Mat res;
ZbarDecoder(image);
system("pause");
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
采用Zbar第三方库,实现二维码定位,并识别内容,解决了中文乱码
共40个文件
tlog:12个
jpg:4个
pdb:3个
4星 · 超过85%的资源 需积分: 50 173 下载量 39 浏览量
2018-05-17
21:34:01
上传
评论 7
收藏 12.42MB RAR 举报
温馨提示
采用Zbar第三方库,实现二维码定位,并识别内容,解决了Zbar中文乱码问题. 利用梯度信息进行条形码的定位,QR-code利用矩阵点信息定位
资源推荐
资源详情
资源评论
收起资源包目录
ConsoleApplication4.rar (40个子文件)
ConsoleApplication4
.vs
ConsoleApplication4
v14
.suo 45KB
ConsoleApplication4.VC.VC.opendb 58B
ConsoleApplication4
test.cpp 6KB
w2.jpg 36KB
ConsoleApplication4.vcxproj.filters 945B
Zbar_Debug_Setting.props 575B
w.jpg 35KB
CvxText.h 4KB
w1.jpg 194KB
ConsoleApplication4.vcxproj 8KB
ConsoleApplication4.vcxproj.user 165B
Debug
test.obj 772KB
ConsoleA.C797BC56.tlog
ConsoleApplication4.lastbuildstate 247B
CL.write.1.tlog 932B
CL.read.1.tlog 11KB
CL.command.1.tlog 872B
link.write.1.tlog 910B
link.command.1.tlog 2KB
link.read.1.tlog 5KB
vc140.pdb 2.19MB
ZbarTest.tlog
CL.write.1.tlog 932B
CL.read.1.tlog 35KB
ZbarTest.lastbuildstate 247B
CL.command.1.tlog 872B
link.write.1.tlog 844B
link.command.1.tlog 2KB
link.read.1.tlog 5KB
vc140.idb 1.7MB
ConsoleApplication4.log 738B
ConsoleApplication4.Build.CppClean.log 2KB
timg.jpg 23KB
CvText.cpp 4KB
ConsoleApplication4.sln 1KB
ConsoleApplication4.VC.db 42.03MB
Debug
ConsoleApplication4.exe 55KB
ZbarTest.pdb 3.3MB
ConsoleApplication4.ilk 404KB
ZbarTest.exe 184KB
ConsoleApplication4.pdb 2.41MB
ZbarTest.ilk 953KB
共 40 条
- 1
Are_he
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页