记录日志
G_HelperMain.h
#pragma once
#include "include/glog/logging.h"
#ifdef DEBUG
#pragma comment(lib,"lib/glogd.lib")
#else
#pragma comment(lib,"lib/glog.lib")
#endif // DEBUG
class GlogHelper
{
public:
GlogHelper(const char* name);
~GlogHelper();
void CreateFolder(char* fileName);
void Info(int id, std::string);
void Error(int id, std::string);
};
G_HelperMain.cpp
#include <iostream>
#include "G_HelperMain.h"
#include <io.h>
#include <direct.h>
GlogHelper::GlogHelper(const char* name)
{
// 初始化应用进程名
google::InitGoogleLogging(name);
// 设置错误级别大于等于多少时输出到文件
// 参数2为日志存放目录和日志文件前缀
char a[] = "log";
CreateFolder(a);
google::SetLogDestination(google::GLOG_INFO, ".//log//InfoCollect_");
google::SetLogDestination(google::GLOG_ERROR, ".//log/ErrorCollect_");
google::SetStderrLogging(google::GLOG_INFO);
google::SetLogFilenameExtension(".log");
FLAGS_colorlogtostderr = true; //set log color
// 是否将日志输出到标准错误是不是日志文件
FLAGS_logtostderr = false;
// 是否同时将日志发送到标准错误和日志文件中
FLAGS_alsologtostderr = true;
// 当日志级别大于此级别时,自动将此日志输出到标准错误中
FLAGS_stderrthreshold = google::FATAL;
// 当日志级别大于此级别时会马上输出,而不缓存
FLAGS_logbuflevel = google::WARNING;
// 缓存最久长时间为多久
FLAGS_logbufsecs = 0;
// 当日志文件达到多少时,进行转存,以M为单位
FLAGS_max_log_size = 10;
// 当磁盘已满时,停止输出日志文件
FLAGS_stop_logging_if_full_disk = true;
}
GlogHelper::~GlogHelper()
{
google::ShutdownGoogleLogging();
}
void GlogHelper::CreateFolder(char* fileName)
{
//文件夹名称
char* folderName = fileName;
// 文件夹不存在则创建文件夹
if (_access(folderName, 0) == -1)
{
_mkdir(folderName);
}
}
void GlogHelper::Info(int id, std::string msg)
{
#if 1
LOG(INFO) << std::to_string(id) + ":" + msg;
#endif // 1
}
void GlogHelper::Error(int id, std::string msg)
{
#if 1
LOG(ERROR) << std::to_string(id) + ":" + msg;
#endif // 1
}
zbar扫码
#pragma once
#include <zbar.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include "G_HelperMain.h"
#include <thread>
using namespace std;
using namespace zbar;
using namespace cv;
zbar_image_scanner_t *scanner[4] = { NULL };
zbar_image_t *image[4] = { NULL };
//glog初始配置
GlogHelper Collect("Collect");
const int barCount = 4;
void test(const int& aIndex) {
Collect.Info(aIndex, "识别开始");
int n = zbar_scan_image(scanner[aIndex], image[aIndex]);
Collect.Info(aIndex, "识别结束");
const zbar_symbol_t *symbol = zbar_image_first_symbol(image[aIndex]);
for (; symbol; symbol = zbar_symbol_next(symbol)) {
zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
const char *data = zbar_symbol_get_data(symbol);
printf("%d:decoded %s symbol \"%s\"\n",
aIndex,zbar_get_symbol_name(typ), data);
}
}
enum StatusDe
{
Default=0,
Success=1,
Yes=1,
Fail=-1,
No=-1,
Err=-2,
};
StatusDe IsReadImgFail(int index,Mat getImage) {
if (!getImage.data) {
cout << "请确认图片" << endl;
Collect.Error(index, "请确认图片");
system("pause");
return Fail;
}
return Success;
}
int main(int argc, char*argv[])
{
Collect.Info(0, "初始化配置开始");
for (int aIndex = 0; aIndex < barCount; aIndex++)
{
scanner[aIndex] = zbar_image_scanner_create();
zbar_image_scanner_set_config(scanner[aIndex], ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
}
Collect.Info(0, "初始化配置结束");
Mat src[barCount];
Mat imageGray[barCount];
int width[barCount];
int height[barCount];
uchar *raw[barCount];
Collect.Info(0, "读入图像开始");
//读入图像
src[0] = imread("./image/0.png");
src[1] = imread("./image/1.png");
src[2] = imread("./image/2.png");
src[3] = imread("./image/3.png");
for (int aIndex = 0; aIndex < barCount; aIndex++)
{
if (Success !=IsReadImgFail(aIndex,src[aIndex]))
{
Collect.Error(aIndex,"失败");
return -1;
}
}
Collect.Info(0, "读入图像结束");
Collect.Info(0, "处理成灰度图开始");
for (int aIndex = 0; aIndex < barCount; aIndex++)
{
//转成灰度图
cvtColor(src[aIndex], imageGray[aIndex], COLOR_BGR2GRAY);
}
Collect.Info(0, "处理成灰度图结束");
for (int aIndex = 0; aIndex < barCount; aIndex++)
{
width[aIndex] = imageGray[aIndex].cols;
height[aIndex] = imageGray[aIndex].rows;
raw[aIndex] = (uchar *)imageGray[aIndex].data;
image[aIndex] = zbar_image_create();
const std::string& format = "Y800";
unsigned long fourcc = ((format[0] & 0xff) |
((format[1] & 0xff) << 8) |
((format[2] & 0xff) << 16) |
((format[3] & 0xff) << 24));
zbar_image_set_format(image[aIndex], fourcc);
zbar_image_set_size(image[aIndex], width[aIndex], height[aIndex]);
zbar_image_set_data(image[aIndex], raw[aIndex], width[aIndex] * height[aIndex], zbar_image_free_data);
}
thread t0(test, 0);
thread t1(test, 1);
thread t2(test, 2);
thread t3(test, 3);
t0.join();
t1.join();
t2.join();
t3.join();
for (int aIndex = 0; aIndex < barCount; aIndex++)
{
//这是释放了空指针吗?会报错
//zbar_image_destroy(image);
zbar_image_scanner_destroy(scanner[aIndex]);
}
//waitKey();
return 0;
}
我编译好的32位的glog和zbar,连同整个工程
下载地址: