chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

【C語言應(yīng)用】如何用C代碼生成二維碼?

嵌入式物聯(lián)網(wǎng)開發(fā) ? 來源:嵌入式物聯(lián)網(wǎng)開發(fā) ? 作者:嵌入式物聯(lián)網(wǎng)開發(fā) ? 2022-08-24 19:01 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

當(dāng)下因微信和支付寶等手機(jī)應(yīng)用廣泛使用,而基于二維碼/一維條碼的移動支付,也借助手機(jī)移動端席卷全國,使得越來越多的人知道有“二維碼”這么一種東西。

對于普通用戶而來,一般只知道將自己的二維碼展示給別人,別人使用軟件識別這個二維碼即可完成一定的功能。比如,掃碼二維碼付款、掃碼二維碼加微信好友、掃碼二維碼訪問網(wǎng)頁、掃碼二維碼下載app等等。這些個功能,在日常行為中,已經(jīng)很常見了,但作為程序猿的我們,我們怎么能不知道二維碼是如何生成的呢?或者說,我要自己生成一個二維碼,除了去網(wǎng)頁上找二維碼生成工具來生成,我可以自己編碼來實(shí)現(xiàn)么?

答案,當(dāng)然是,必須可以。不然這文章不用寫了。

在介紹如何用代碼生成二維碼之前,就不得不先介紹一個開源庫叫zint。這個開源可謂牛叉的很,幾乎平時見過的“碼”,各式各樣的一維條碼、各式各樣的二維碼條碼都難不倒它,重要的是,它還是開源的,幾乎包含了所有常見“碼”的生成。以下是摘自官方用戶使用手冊的介紹片段。(筆者上一篇博文介紹zint的安裝時簡單介紹了一下zint庫,如何在linux平臺安裝zint開源庫 - 架構(gòu)師李肯 - 博客園,它的開源項(xiàng)目網(wǎng)頁為Zint Barcode Generator download | SourceForge.net)

The Zint project aims to provide a complete cross-platform open source barcode generating solution. The package currently consists of a Qt based GUI, a command line executable and a library with an API to allow developers access to the capabilities of Zint. It is hoped that Zint provides a solution which is flexible enough for professional users while at the same time takes care of as much of the processing as possible to allow easy translation from input data to barcode image.

-----------------------------------------------------華麗麗的分割線-----------------------------------------------------

言歸正傳,說回如何使用zint庫生成二維碼。主要使用到以下幾個函數(shù):可以從zint.h中得到api的聲明(主要是C語言接口)。

ZINT_EXTERN struct zint_symbol* ZBarcode_Create(void);

ZINT_EXTERN void ZBarcode_Clear(struct zint_symbol *symbol);

ZINT_EXTERN void ZBarcode_Delete(struct zint_symbol *symbol);

ZINT_EXTERN int ZBarcode_Encode_and_Print(struct zint_symbol *symbol, unsigned char *input, int length, int rotate_angle);

以下是個人封裝的生成二維碼的自定義接口函數(shù):

/****************************************************************************

Descpribe: Create Qrcode API with C Code by calling zint lib.

Input : pQrCodeData, the qrcode data buf

QrcodeLen, the len of qrcode data, but it can be 0

pQrCodeFile, the output file name of qrcode, it can be NULL

Output : pZintRet, to store the ret code from linzint.

Return : 0 is ok, and other values are fail. See the meanings in enum ZINT_RET_CODE

Notes : pQrCodeFile, Must end in .png, .eps or .svg. when isn,t NULL string.

****************************************************************************/

ZINT_RET_CODE Zint_Create_QrCode(uint8_t *pQrCodeData, int QrcodeLen, char *pQrCodeFile, int *pZintRet);

這個接口定義比較簡單,上面也簡單說了各個參數(shù)的意義,其他中特別需要注意的是,如果傳入生成二維碼圖片名字不使用默認(rèn)值時(pQrCodeFile != NULL),也務(wù)必保證pQrCodeFile必須是以.png, .eps or .svg.結(jié)尾的文件名。

以下是zint_code.c 和 zint_code.h的內(nèi)容,里面將zint中生成二維碼的幾個函數(shù)封裝在一塊了,使用者只需關(guān)注上面定義的Zint_Create_QrCode函數(shù),即可生成漂亮的二維碼圖片文件。

 ?




 1 /****************************************************************************
 2  * File       : zint_code.c
 3  * 
 4  * Copyright (c) 2011 by Li.Recan < 721317716@qq.com >
 5  * 
 6  * DESCRIPTION: Demo for creating qrcode by C code.
 7  * 
 8  * Modification history
 9  * --------------------------------------------------------------------------
10  * Date         Version  Author       History
11  * --------------------------------------------------------------------------
12  * 2016-10-15   1.0.0    Li.Recan     written
13  ***************************************************************************/
14  
15 // Standard Library
16 #include 
17 #include 
18 
19 // so Library
20 #include "zint.h"
21 
22 // Project Header
23 #include "zint_code.h"
24 
25 
26 /****************************************************************************
27 Descpribe: Create Qrcode API with C Code by calling zint lib.
28 Input    : pQrCodeData, the qrcode data buf
29            QrcodeLen, the len of qrcode data, but it can be 0
30            pQrCodeFile, the output file name of qrcode, it can be NULL           
31 Output   : pZintRet, to store the ret code from linzint. 
32 Return   : 0 is ok, and other values are fail. See the meanings in enum ZINT_RET_CODE
33 Notes    : pQrCodeFile, Must end in .png, .eps or .svg. when isn,t NULL string.
34 ****************************************************************************/
35 ZINT_RET_CODE Zint_Create_QrCode(uint8_t *pQrCodeData, int QrcodeLen, char *pQrCodeFile, int *pZintRet)
36 {
37     struct zint_symbol *pMySymbol     = NULL;
38     int RetCode                     = 0;    
39     
40     if(!pQrCodeData) //check input pointer
41     {
42         return ZINT_ERR_INV_DATA;
43     }
44 
45     if(QrcodeLen == 0)
46     {
47         QrcodeLen = strlen((char *)pQrCodeData);
48     }
49     if(QrcodeLen > QRCODE_MAX_LEN)//len is too long
50     {        
51         return ZINT_ERR_TOO_LONG;
52     }
53 
54     if(0 == ZBarcode_ValidID(BARCODE_QRCODE))
55     {
56         return ZINT_ERR_INV_CODE_ID;
57     }
58     
59     pMySymbol = ZBarcode_Create();
60     if(pMySymbol == NULL)
61     {
62         return ZINT_ERR_MEMORY;
63     }
64 
65     if(pQrCodeFile)//when it's NULL, outfile will be "out.png"
66     {
67         if(strstr(pQrCodeFile, "png") || (strstr(pQrCodeFile, "eps")) || (strstr(pQrCodeFile, "svg")))
68         {
69             strcpy(pMySymbol->outfile, pQrCodeFile);
70         }
71         else
72         {
73             ZBarcode_Clear(pMySymbol);
74             ZBarcode_Delete(pMySymbol); //release memory in zint lib
75             return ZINT_ERR_FILE_NAME;
76         }
77     }
78     pMySymbol->symbology     = BARCODE_QRCODE;  
79     pMySymbol->option_1     = 3; //ECC Level.It can be large when ECC Level is larger.(value:1-4)  
80     pMySymbol->scale         = 4; //contorl qrcode file size, default is 1, used to be 4   
81     pMySymbol->border_width = 2; //set white space width around your qrcode and 0 is for nothing 
82     
83     RetCode = ZBarcode_Encode_and_Print(pMySymbol, pQrCodeData, QrcodeLen, 0);    
84     ZBarcode_Clear(pMySymbol);
85     ZBarcode_Delete(pMySymbol); //release memory in zint lib
86 
87     if(pZintRet)
88     {
89         *pZintRet = RetCode; //save ret code from zint lib
90     }
91     
92     return ((0 == RetCode) ? (ZINT_OK) : (ZINT_ERR_LIB_RET));
93 }
View Code: zint_code.c




 1 /****************************************************************************
 2  * File       : zint_code.h
 3  * 
 4  * Copyright (c) 2011 by Li.Recan < 721317716@qq.com >
 5  * 
 6  * DESCRIPTION: API for creating qrcode by C code.
 7  * 
 8  * Modification history
 9  * --------------------------------------------------------------------------
10  * Date         Version  Author       History
11  * --------------------------------------------------------------------------
12  * 2016-10-15   1.0.0    Li.Recan     written
13  ***************************************************************************/
14  
15 #ifndef __ZINT_CODE__
16 #define __ZINT_CODE__
17 
18 #ifdef __cplusplus
19 extern "C"
20 {
21 #endif
22 
23 #include 
24 
25 #define QRCODE_MAX_LEN        500 //max string len for creating qrcode
26 
27 typedef enum 
28 {
29     ZINT_OK                 = 0,
30     ZINT_ERR_INV_DATA         = -1, //input invalid data
31     ZINT_ERR_TOO_LONG         = -2, //len for input data is too long    
32     ZINT_ERR_INV_CODE_ID     = -3,//the code type is not supported by zint
33     ZINT_ERR_MEMORY         = -4, //malloc memory error in zint lib
34     ZINT_ERR_FILE_NAME        = -5, //qrcode file isn'y end in .png, .eps or .svg.
35     ZINT_ERR_LIB_RET         = -6, //zint lib ret error, real ret code should be zint api ret code
36 }ZINT_RET_CODE;
37 
38 /****************************************************************************
39 Descpribe: Create Qrcode API with C Code by calling zint lib.
40 Input    : pQrCodeData, the qrcode data buf
41            QrcodeLen, the len of qrcode data, but it can be 0
42            pQrCodeFile, the output file name of qrcode, it can be NULL           
43 Output   : pZintRet, to store the ret code from linzint. 
44 Return   : 0 is ok, and other values are fail. See the meanings in enum ZINT_RET_CODE
45 Notes    : pQrCodeFile, Must end in .png, .eps or .svg. when isn,t NULL string.
46 ****************************************************************************/
47 ZINT_RET_CODE Zint_Create_QrCode(uint8_t *pQrCodeData, int QrcodeLen, char *pQrCodeFile, int *pZintRet);
48 
49 #define Debuging(fmt, arg...)       printf("[%20s, %4d] "fmt, __FILE__, __LINE__, ##arg)
50 
51 #ifdef __cplusplus
52 }
53 #endif
54 
55 #endif /* __ZINT_CODE__ */
?

在工程實(shí)踐中,只需要將這兩個文件添加到工程中,并讓他們參與工程編譯,即可完美使用zint生成二維碼了。

下面是一個簡單的demo,將會展示如何使用這個接口函數(shù),見qrcode_test.c

/****************************************************************************
 2  * File       : qrcode_test.c
 3  * 
 4  * Copyright (c) 2011 by Li.Recan < 721317716@qq.com >
 5  * 
 6  * DESCRIPTION: Demo for creating qrcode by C code.
 7  * 
 8  * Modification history
 9  * --------------------------------------------------------------------------
10  * Date         Version  Author       History
11  * --------------------------------------------------------------------------
12  * 2016-10-15   1.0.0    Li.Recan     written
13  ***************************************************************************/
14  
15 // Standard Library
16 #include 
17 
18 // Project Header
19 #include "zint_code.h"
20 
21 int main(int argc, char *argv[])
22 {
23     int ZintLibRet             = 0; //ret code from zint lib
24     ZINT_RET_CODE ZintRet     = 0; //ret code from zint_code api
25     char QrcodeData[]         = "I love zint lib. 測試一下gbk編碼 ...";
26     char QrcodeDataDef[]     = "This's default qrcode file name : out.png ";
27     char QrcodeFile[]         = "MyQrcode.png"; // Must end in .png, .eps or .svg. //zint lib ask !
28     
29     //test with inputing qrcode_file name
30     ZintRet = Zint_Create_QrCode((uint8_t*)QrcodeData, 0, QrcodeFile, &ZintLibRet);
31     if(ZINT_OK != ZintRet)
32     {
33         Debuging("Create qrcode err, ZintRet = %d, ZintLibRet = %d\n", ZintRet, ZintLibRet);
34     }
35     else
36     {
37         Debuging("Create qrcode OK ! View qrcode file : %s in cur path. ZintRet = %d, ZintLibRet = %d\n", QrcodeFile, ZintRet, ZintLibRet);
38     }
39     
40     //test without inputing qrcode_file name
41     ZintRet = Zint_Create_QrCode((uint8_t*)QrcodeDataDef, 0, NULL, &ZintLibRet);
42     if(ZINT_OK != ZintRet)
43     {
44         Debuging("Create qrcode err, ZintRet = %d, ZintLibRet = %d\n", ZintRet, ZintLibRet);
45     }
46     else
47     {
48         Debuging("Create qrcode OK ! View qrcode file : out.png in cur path. ZintRet = %d, ZintLibRet = %d\n", ZintRet, ZintLibRet);
49     }
50     
51     return 0;
52 }

輸入完成后,使用gcc -o qrcode_test qrcode_test.c zint_code.c –lzint 即可編譯出qrcode_test的bin文件了。

等等,如果你的linux還未安裝zint庫,sorry,你將看到

那么趕緊回到上一篇博文 如何在linux平臺安裝zint開源庫 - 架構(gòu)師李肯 - 博客園 把zint安裝起來吧。

準(zhǔn)確無誤的編譯,之后,在當(dāng)前目錄ls就可以看到qrcode_test的bin文件了。

我們使用./ qrcode_test運(yùn)行我們編譯出來的demo程序,可以看到以下的提示:

[liluchang@localhost src]$ ./qrcode_test

./qrcode_test: error while loading shared libraries: libzint.so.2.4: cannot

open shared object file: No such file or directory

又出什么問題了,原來系統(tǒng)在運(yùn)行這個demo程序時,沒有找到libzint.so來鏈接,那么我們只需要在運(yùn)行之前告訴系統(tǒng)去哪里找這個so即可。使用

export LD_LIBRARY_PATH=/usr/local/lib 這個路徑是根據(jù)情況而定的?!咀⒁膺@個export只對當(dāng)前運(yùn)行的shell生效,一旦切換一個shell,則需要重新輸入。如果需要固定告訴運(yùn)行demo的時候去哪里找so鏈接,則可以在編譯的時候告訴它。這個點(diǎn)往后再介紹?!?/p>

之后再運(yùn)行demo程序:

第一個框框里面是demo程序打印出來的調(diào)試信息,標(biāo)識連個二維碼都生成成功了。

第二個框框可以看到,在當(dāng)前目錄下,就已經(jīng)生成了這兩個png文件,并且第二個生成的使用的是系統(tǒng)默認(rèn)的名字out.png。

為了驗(yàn)證程序生成的二維碼是否正確,我們可以使用手機(jī)去掃碼一下這兩個二維碼:

為了驗(yàn)證程序生成的二維碼是否正確,我們可以使用手機(jī)去掃碼一下這兩個二維碼:

用手機(jī)掃描出來的結(jié)果如下:

poYBAGDYdXCAWkKMAAAAK8RNs4s030.png

?編輯

圖中顯示的掃描結(jié)果,正好如demo中寫的

證明這代碼是可行的。

好了,本篇介紹使用C語言調(diào)用zint庫生成二維碼的教程就介紹到這里。感興趣的童鞋可以評論留言或者自行閱讀zint用戶手冊或開源項(xiàng)目介紹網(wǎng)頁詳細(xì)內(nèi)容。

后話,下篇文章將介紹zint庫一維條碼的生成,敬請期待。屆時,zint_code.c的接口又豐富一些了。

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報(bào)投訴
  • C語言
    +關(guān)注

    關(guān)注

    183

    文章

    7643

    瀏覽量

    145479
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點(diǎn)推薦

    有哪些常見的二維碼模組類型?

    二維碼模組是集成了掃描引擎、解碼芯片、光學(xué)組件的核心模塊(也叫二維碼模塊),可直接嵌入自助終端、工業(yè)PDA、智能閘機(jī)等設(shè)備,實(shí)現(xiàn)二維碼的快速識別與數(shù)據(jù)傳輸。其常見類型可按安裝方式、掃描原理、通信接口
    的頭像 發(fā)表于 01-24 00:00 ?751次閱讀
    有哪些常見的<b class='flag-5'>二維碼</b>模組類型?

    二維影像掃描引擎在門禁二維碼刷卡梯控行業(yè)中的應(yīng)用

    門禁二維碼刷卡梯控系統(tǒng)中的應(yīng)用及其帶來的多方面優(yōu)勢。一、二維影像掃描引擎的靈活性與便捷性門禁二維碼刷卡梯控行業(yè)利用手機(jī)生成二維碼,用戶只需
    的頭像 發(fā)表于 12-17 15:42 ?326次閱讀
    <b class='flag-5'>二維</b>影像掃描引擎在門禁<b class='flag-5'>二維碼</b>刷卡梯控行業(yè)中的應(yīng)用

    二維碼模塊是什么?它和普通條碼識別設(shè)備有什么不同

    在物聯(lián)網(wǎng)技術(shù)飛速發(fā)展的今天,條碼與二維碼已成為信息傳遞的重要載體,從超市結(jié)賬到工業(yè)生產(chǎn)溯源,從移動支付到醫(yī)療耗材管理,相關(guān)識別設(shè)備無處不在。其中,二維碼模塊作為核心識別部件,正憑借其靈活適配性
    的頭像 發(fā)表于 11-06 15:54 ?483次閱讀
    <b class='flag-5'>二維碼</b>掃<b class='flag-5'>碼</b>模塊是什么?它和普通條碼識別設(shè)備有什么不同

    哪款二維碼模組適合嵌入戶外取餐柜,用于掃二維碼

    在智能取餐柜普及的當(dāng)下,二維碼模組作為核心交互組件,其性能直接影響用戶體驗(yàn)與設(shè)備穩(wěn)定性。針對戶外場景的特殊需求,深圳遠(yuǎn)景達(dá)物聯(lián)網(wǎng)推出的LV4300Pro系列二維碼模組,憑借工業(yè)級設(shè)計(jì)與場景化技術(shù)優(yōu)化
    的頭像 發(fā)表于 09-10 15:00 ?596次閱讀
    哪款<b class='flag-5'>二維碼</b>模組適合嵌入戶外取餐柜,用于掃<b class='flag-5'>二維碼</b>

    【嘉楠堪智K230開發(fā)板試用體驗(yàn)】+二維碼識別

    () 驗(yàn)證效果 二維碼通過草料二維碼網(wǎng)站隨機(jī)生成,編輯文字“用于AI測試的二維碼”,點(diǎn)擊生成二維碼
    發(fā)表于 08-22 20:16

    GM861條二維碼識別模塊用戶手冊

    GM861條二維碼識別模塊用戶手冊
    發(fā)表于 07-09 14:56 ?1次下載

    基于STM32的二維碼識別源碼+二維碼解碼庫lib

    基于STM32的二維碼識別源碼+二維碼解碼庫lib,推薦下載!
    發(fā)表于 05-28 22:04

    基于LockAI視覺識別模塊:C++二維碼識別

    二維碼識別是視覺模塊經(jīng)常使用到的功能之一。我們將演示如何使用基于瑞芯微RV1106的LockAI視覺識別模塊進(jìn)行二維碼識別。 源代碼網(wǎng)址:https://gitee.com/LockzhinerAI
    發(fā)表于 05-26 09:57

    基于LockAI視覺識別模塊:C++二維碼識別

    二維碼識別是視覺模塊經(jīng)常使用到的功能之一。我們將演示如何使用基于瑞芯微RV1106的LockAI視覺識別模塊進(jìn)行二維碼識別。
    的頭像 發(fā)表于 05-26 09:42 ?982次閱讀
    基于LockAI視覺識別模塊:<b class='flag-5'>C</b>++<b class='flag-5'>二維碼</b>識別

    基于STM32的二維碼識別源碼+二維碼解碼庫lib

    基于STM32的二維碼識別源碼+二維碼解碼庫lib項(xiàng)目實(shí)例下載! 純分享帖,需要者可點(diǎn)擊附件免費(fèi)獲取完整資料~~~【免責(zé)聲明】本文系網(wǎng)絡(luò)轉(zhuǎn)載,版權(quán)歸原作者所有。本文所用視頻、圖片、文字如涉及作品版權(quán)問題,請第一時間告知,刪除內(nèi)容!
    發(fā)表于 05-23 20:45

    基于RK3576開發(fā)板的二維碼生成

    檔介紹了如何快速上手二維碼生成,包括源碼工程下載、開發(fā)環(huán)境搭建、例程編譯與運(yùn)行。通過EASY-EAI API,用戶可輕松生成二維碼圖片,API封裝了
    的頭像 發(fā)表于 05-10 15:19 ?1102次閱讀
    基于RK3576開發(fā)板的<b class='flag-5'>二維碼</b><b class='flag-5'>生成</b>

    二維碼讀取器是干嘛的

    二維碼讀取器(用于二維碼讀取的機(jī)器),作為一種現(xiàn)代化的自動識別技術(shù)設(shè)備,正日益滲透到我們生活的方方面面。從商場購物到物流配送,從醫(yī)療管理到工業(yè)生產(chǎn)線,二維碼讀取器憑借其高效、準(zhǔn)確的讀取能力,發(fā)揮
    的頭像 發(fā)表于 03-17 15:57 ?1298次閱讀
    <b class='flag-5'>二維碼</b>讀取器是干嘛的

    一“”當(dāng)先!看二維碼模組如何重塑智能門鎖掃體驗(yàn)

    在科技日新月異的今天,智能門鎖正逐步取代傳統(tǒng)機(jī)械鎖,成為現(xiàn)代家庭安全防護(hù)的首選。在這場門鎖智能化革命中,二維碼模組以其獨(dú)特的識別技術(shù)和便捷的操作方式,成為智能門鎖的重要組成部分。本文將深入探討二維碼
    的頭像 發(fā)表于 03-12 16:17 ?1034次閱讀
    一“<b class='flag-5'>碼</b>”當(dāng)先!看<b class='flag-5'>二維碼</b>模組如何重塑智能門鎖掃<b class='flag-5'>碼</b>體驗(yàn)

    嵌入式二維碼識別引擎是什么設(shè)備?哪些場景用得到?

    在科技日新月異的今天,嵌入式技術(shù)已廣泛滲透到我們生活的方方面面,其中,嵌入式二維碼識別引擎作為一種高效、便捷的信息采集工具,正逐漸成為眾多行業(yè)智能化升級的重要推手。本文將帶您深入探索二維碼識讀引擎
    的頭像 發(fā)表于 03-10 14:57 ?800次閱讀
    嵌入式<b class='flag-5'>二維碼</b>識別引擎是什么設(shè)備?哪些場景用得到?

    快速將二維碼掃描識別模組嵌入集成到安卓一體機(jī)上使用

    在現(xiàn)代科技快速發(fā)展的今天,二維碼掃描模組的應(yīng)用已深入到各個行業(yè)領(lǐng)域。特別是在安卓一體機(jī)中,二維碼掃描模組已成為其不可或缺的一部分。本文將詳細(xì)介紹如何在安卓一體機(jī)上安裝二維碼掃描模組、連接二維碼
    的頭像 發(fā)表于 02-28 15:59 ?1077次閱讀
    快速將<b class='flag-5'>二維碼</b>掃描識別模組嵌入集成到安卓一體機(jī)上使用