/*
2018-10-10 10:01:15
编译Zint 在VS2017的环境下
*/
参考:https://blog.csdn.net/weiwei9363/article/details/81196646
补充:
在编译过程中 出了上述文章所没有提及的问题
还会出现 zlib.h文件无法找到的问题;解决办法:将文件夹zlib-1.2.8中的h文件拷贝到lPng中
关于使用zint进行条码的生成:(API的使用)
http://www.zint.org.uk/Manual.aspx?type=p&page=5
详细的介绍了zint的使用方法
zint_symbol结构体 包含了生成条码图片的详细信息,结构体定义如下
struct zint_symbol
{
int symbology; //条码类型 默认:BARCODE_CODE128
int height; //条码高度 默认:50
int whitespace_width; //空白宽度 默认:0
int border_width; //边框宽度 默认:0
int output_options; //设置输出方式(输出条码图片的增加元素) 默认:0
char fgcolour[10]; //前景色 颜色为RGB十六进制字符串必须是6个字符 "000000" 黑色
char bgcolour[10]; //背景色 "FFFFFF" 白色
char outfile[256]; //输出条码的名称,包括后缀名;后缀名可选(.png,.bmp,.gif,.eps,.pcx,.svg或.txt)
float scale; //调整图像大小比例 默认:1.0
int option_1;
int option_2;
int option_3;
int show_hrt; //设置为1 显示文本在条码图片下面 设置为0 则不显示
int fontsize;
int input_mode; //设置输入数据的编码模式 默认:UNICODE_MODE
int eci; //扩展通道解释模式 默认:3
unsigned char text[128]; //可读的文本 默认:NULL
int rows; //输出的行数
int width; //输出的宽
char primary[128]; //复杂条码的主要数据 默认:NULL
unsigned char encoded_data[200][143]; //输出的编码数据
int row_height[200]; //输出一行的高度
char errtxt[100]; //发生错误时 输出的错误信息
char *bitmap; //输出指向位图的指针
int bitmap_width; //输出位图的宽
int bitmap_height; //输出位图的高
unsigned int bitmap_byte_length;
float dot_size; //使用点输出模式下 点的大小
struct zint_render *rendered;
int debug;
};
关于使用Zint支持中文的问题,解决方案
转换成UTF8支持即可
包含几个互相转换的函数
UnicodeToAscii
AsciiToUnicode
Unicode转换成UTF8
std::string UnicodeToUTF8(const std::wstring& str)
{
char *pElementText;
int iTextLen = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
pElementText = new char[iTextLen + 1];
memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1));
::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);
std::string strText = pElementText;
delete[] pElementText;
return strText;
}
ASCII码直接转换成UTF8
std::string ASCToUTF8(const std::string& str)
{
int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[unicodeLen + 1];
memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
std::wstring wrt = (wchar_t*)pUnicode;
delete pUnicode;
int iTextLen = WideCharToMultiByte(CP_UTF8, 0, wrt.c_str(), -1, NULL, 0, NULL, NULL);
char *pElementText = new char[iTextLen + 1];
memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1));
::WideCharToMultiByte(CP_UTF8, 0, wrt.c_str(), -1, pElementText, iTextLen, NULL, NULL);
std::string strText = pElementText;
delete pElementText;
return strText;
}
实现的代码片段
#include "pch.h"
#include <iostream>
#include "HeadFile/zint.h"
#include <string>
#include <atlconv.h>
std::string UnicodeToUTF8(const std::wstring& str); //
std::string ASCIIToUTF8(const std::string& str);
int main()
{
struct zint_symbol *my_symbol = ZBarcode_Create();
my_symbol->symbology = BARCODE_QRCODE; //设置条码类型
my_symbol->height = 100;
my_symbol->border_width = 2;
my_symbol->input_mode = DATA_MODE; //如果需要支持中文 输入数据的模式最好设置为DATA_MODE
strcpy(my_symbol->outfile, "mypng.png");
if (NULL != my_symbol)
{
printf("符号成功创建!\n");
}
//std::string StrSource = UnicodeToUTF8(L"我可以支持中文啦");
std::string StrSource = ASCIIToUTF8("我可以支持中文啦");
int error = ZBarcode_Encode(my_symbol, (const unsigned char*)StrSource.c_str(), StrSource.length());
if (0 != error)
{
printf("%s\n", my_symbol->errtxt);
}
ZBarcode_Print(my_symbol, 0);
ZBarcode_Delete(my_symbol);
system("pause");
return EXIT_SUCCESS;
}
std::string ASCIIToUTF8(const std::string& str)
{
int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[unicodeLen + 1];
memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
std::wstring wrt = (wchar_t*)pUnicode;
delete pUnicode;
int iTextLen = WideCharToMultiByte(CP_UTF8, 0, wrt.c_str(), -1, NULL, 0, NULL, NULL);
char *pElementText = new char[iTextLen + 1];
memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1));
::WideCharToMultiByte(CP_UTF8, 0, wrt.c_str(), -1, pElementText, iTextLen, NULL, NULL);
std::string strText = pElementText;
delete pElementText;
return strText;
}
std::string UnicodeToUTF8(const std::wstring& str)
{
char *pElementText;
int iTextLen = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
pElementText = new char[iTextLen + 1];
memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1));
::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);
std::string strText = pElementText;
delete[] pElementText;
return strText;
}
最后实现效果:
最后附上 编译好的库和例子的完整工程
链接:https://pan.baidu.com/s/1JYqzHBJLvN2lV1Z-mC1XHw
提取码:s927.
有问题 请留言