为什么要先编译库而不像 其它语言拿来即用?因为c++有好多种模式。
总结:
先什么也不动编译库与示例。再把示例按我的工程类型一步一步靠近改。
单独建工程是正常的。
放到 工程不行,是因为C++头文件冲突了,同样的名字,别的头文件也定义了。解决:单独放一个类里 。
移植:
1.包含的目录
D:\test\zxing-cpp-master3\core\src;D:\test\zxing-cpp-master3\build\core;D:\test\zxing-cpp-master3\build\_deps\stb-src;C:\Program Files\MySQL\MySQL Server 5.5\include;.\openssl;.\utility;%(AdditionalIncludeDirectories)
2.头文件及zxing.lib库(下面)
3.调用
#pragma once
#include <windows.h>
#include <wingdi.h>
#include "myBase.h"
int GenBarCodeZXing(HDC hDC2, int left, int top, int bottom, const char* barcodeText, int iStyle, int iMap, int width);
#include "myBase.h"
//ZXing库
#include "BitMatrix.h"
#include "MultiFormatWriter.h"
using namespace ZXing;
#pragma comment(lib,"D:\\test\\zxing-cpp-master3\\build\\core\\Release\\ZXing.lib") //#pragma comment 是以当前工程所在路径为当前目录
/**********************************************************************
* 有白边
* 参 数:
* hDC:画布句柄
* left:左上角 X坐标left
* top:左上角 Y坐标top
* bottom:右下角 Y从标bottom
* barcodeText:条码内容
* iStyle:无效 条码格式 2 Code 128
* iMap:无效
* width:宽度width
* 返回值:无
* 描 述:画条码到指定HDC
**********************************************************************/
int GenBarCodeZXing(HDC hDC2, int left, int top, int bottom,const char* barcodeText, int iStyle, int iMap, int width)
{
int height = bottom - top;
int margin = 0;
int eccLevel = -1;
BarcodeFormat format = BarcodeFormat::Code128;
CharacterSet encoding = CharacterSet::Unknown;
try {
auto writer = MultiFormatWriter(format).setMargin(margin).setEccLevel(eccLevel).setEncoding(encoding);
BitMatrix bitMatrix = writer.encode(barcodeText, width, height);
auto bitmap = ToMatrix<uint8_t>(bitMatrix);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
//bool b = true;
bool b= bitMatrix.get(x, y); //反
COLORREF color = (b ? RGB(0, 0, 0) : RGB(255, 255, 255));
SetPixel(hDC2, left+x, top+y, color);
//pixels[y * width + x] = bitMatrix.get(x, height - 1 - y) ? 0 : 1; //反
}
}
}
catch (const std::exception& e) {
//std::cerr << e.what() << std::endl;
return -1; //参数不正确
}
return 0;
}
把include减少点。以下为必要:
下面可不用看--------------------------------------------------------------------------------------------------
1.生成运行库的类型要一样。
为什么有些lib在各种类型下一个库都能兼容?
VC编译静态库/MT, /MTd和动态链接库/MD, /MDd的理解
带小写d为debug调试版本,不带d的为release版
MT:把库与程序一起打包,就像iphone把电池不可更换。好处:就一个文件。
MD:库与主程序分开,可以更换库,而不更新主程序。就像电池可自己替换。好处:可替换dll。
输出配置类型:
静态库.lib
动态库.dll
静态库是一种包含函数原型和函数声明的库文件,可以直接在应用程序中使用,不需要进行编译。静态库的编译过程与普通的源代码文件相同,只是在编译时需要指定一些特殊的选项,例如链接器选项和库文件名等。
动态库是一种包含函数指针和函数声明的库文件,可以在运行时动态加载和卸载。动态库的编译过程与普通的源代码文件相同,但是在编译时需要使用不同的选项,例如链接器选项和库文件名等。动态库的加载速度比静态库快,但是不能被多个应用程序共享。
很多SDK的动态链接库在vs的debug模式和release是通用的,这是怎么编译出来?
debug库和release库可不可以混用呢?可以,但是会报crt冲突错误,忽略掉相应的库就可以编译链接了。可以看出冲突主要是因为crt的debug库和release库的同名函数链接冲突了。我们自己编译生产一个库,不使用系统库。这样在release和debug下就都可以用了。release库和debug库本质上一样的。主要区别是否有调试代码信息。
2.加入.lib和头文件
先看原来头文件的位置
D:\test\zxing-cpp-master3\core\src;D:\test\zxing-cpp-master3\build\core;D:\test\zxing-cpp-master3\build\_deps\stb-src;%(AdditionalIncludeDirectories)
..\core\Debug\ZXing.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib
怎么把默认库全加进去了。想简单一些,方便后期可以删,先加一个ZXing.lib试试。
因为我的工程需要使用win32编译。lib也要用win32编译。新建win32,使用win64的配置编译报错:
fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突_模块计算机类型x86与目标计算机类型_EmbededCoder的博客-CSDN博客
配置好后又报错。
单独能生成zxing.lib查看64位的输入, 需要把32位的改成和64位的一样路径。把我的工程改下C++2017。
还是先把示例运行成功吧。
单个生成成功。开始测试:先生成条型码和二维码,再识别内容。成功。
ZXingWriter.exe Code128 45789ABC chenhao.jpg
ZXingWriter.exe QRCode 45789ABC chenhaoQRCode.jpg
ZXingReader.exe -format Code128 chenhao.jpg
ZXingReader.exe -format QRCode chenhaoQRCode.jpg
pause
编译debug时又提示下面,再3个地方设置一遍。
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK1112 模块计算机类型“x86”与目标计算机类型“x64”冲突 ZXing D:\test\zxing-cpp-master3\build\core\Debug\BarcodeFormat.obj 1
错误 LNK1112 模块计算机类型“x86”与目标计算机类型“x64”冲突 ZXingWriter D:\test\zxing-cpp-master3\build\example\Debug\ZXingWriter.obj 1
错误 LNK1112 模块计算机类型“x86”与目标计算机类型“x64”冲突 ZXingReader D:\test\zxing-cpp-master3\build\example\Debug\ZXingReader.obj 1
出现无法解析的外部符号,要先生成zxing.lib,再生成示例。
zxing有点特别,它在文档管理程序。
是x86,不要写成x32了。