ZInt支援中文例子
阿新 • • 發佈:2018-11-17
/*
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.
有問題 請留言