C++ 二维码生成

Posted 76211822

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 二维码生成相关的知识,希望对你有一定的参考价值。

二维码生成类

// QR_Encode.h : CQR_Encode 僋儔僗愰尵偍傛傃僀儞僞乕僼僃僀僗掕媊
// Date 2006/05/17    Ver. 1.22    Psytec Inc.

#if !defined(AFX_QR_ENCODE_H__AC886DF7_C0AE_4C9F_AC7A_FCDA8CB1DD37__INCLUDED_)
#define AFX_QR_ENCODE_H__AC886DF7_C0AE_4C9F_AC7A_FCDA8CB1DD37__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// 掕悢

// 岆傝掶惓儗儀儖
#define QR_LEVEL_L    0
#define QR_LEVEL_M    1
#define QR_LEVEL_Q    2
#define QR_LEVEL_H    3

// 僨乕僞儌乕僪
#define QR_MODE_NUMERAL        0
#define QR_MODE_ALPHABET    1
#define QR_MODE_8BIT        2
#define QR_MODE_KANJI        3

// 僶乕僕儑儞(宆斣)僌儖乕僾
#define QR_VRESION_S    0 // 1 乣 9
#define QR_VRESION_M    1 // 10 乣 26
#define QR_VRESION_L    2 // 27 乣 40

#define MAX_ALLCODEWORD     3706 // 憤僐乕僪儚乕僪悢嵟戝抣
#define MAX_DATACODEWORD 2956 // 僨乕僞僐乕僪儚乕僪嵟戝抣(僶乕僕儑儞40-L)
#define MAX_CODEBLOCK      153 // 僽儘僢僋僨乕僞僐乕僪儚乕僪悢嵟戝抣(俼俽僐乕僪儚乕僪傪娷傓)
#define MAX_MODULESIZE      177 // 堦曈儌僕儏乕儖悢嵟戝抣

// 價僢僩儅僢僾昤夋帪儅乕僕儞
#define QR_MARGIN    4


/////////////////////////////////////////////////////////////////////////////
typedef struct tagRS_BLOCKINFO
{
    int ncRSBlock;        // 俼俽僽儘僢僋悢
    int ncAllCodeWord;    // 僽儘僢僋撪僐乕僪儚乕僪悢
    int ncDataCodeWord;    // 僨乕僞僐乕僪儚乕僪悢(僐乕僪儚乕僪悢 - 俼俽僐乕僪儚乕僪悢)

} RS_BLOCKINFO, *LPRS_BLOCKINFO;


/////////////////////////////////////////////////////////////////////////////
// QR僐乕僪僶乕僕儑儞(宆斣)娭楢忣曬

typedef struct tagQR_VERSIONINFO
{
    int nVersionNo;       // 僶乕僕儑儞(宆斣)斣崋(1乣40)
    int ncAllCodeWord; // 憤僐乕僪儚乕僪悢

    // 埲壓攝楍揧帤偼岆傝掶惓棪(0 = L, 1 = M, 2 = Q, 3 = H) 
    int ncDataCodeWord[4];    // 僨乕僞僐乕僪儚乕僪悢(憤僐乕僪儚乕僪悢 - 俼俽僐乕僪儚乕僪悢)

    int ncAlignPoint;    // 傾儔僀儊儞僩僷僞乕儞嵗昗悢
    int nAlignPoint[6];    // 傾儔僀儊儞僩僷僞乕儞拞怱嵗昗

    RS_BLOCKINFO RS_BlockInfo1[4]; // 俼俽僽儘僢僋忣曬(1)
    RS_BLOCKINFO RS_BlockInfo2[4]; // 俼俽僽儘僢僋忣曬(2)

} QR_VERSIONINFO, *LPQR_VERSIONINFO;


/////////////////////////////////////////////////////////////////////////////
// CQR_Encode 僋儔僗

class CQR_Encode
{
// 峔抸/徚柵
public:
    CQR_Encode();
    ~CQR_Encode();

public:
    int m_nLevel;        // 岆傝掶惓儗儀儖
    int m_nVersion;        // 僶乕僕儑儞(宆斣)
    BOOL m_bAutoExtent;    // 僶乕僕儑儞(宆斣)帺摦奼挘巜掕僼儔僌
    int m_nMaskingNo;    // 儅僗僉儞僌僷僞乕儞斣崋

public:
    int m_nSymbleSize;
    BYTE m_byModuleData[MAX_MODULESIZE][MAX_MODULESIZE]; // [x][y]
    // bit5:婡擻儌僕儏乕儖乮儅僗僉儞僌懳徾奜乯僼儔僌
    // bit4:婡擻儌僕儏乕儖昤夋僨乕僞
    // bit1:僄儞僐乕僪僨乕僞
    // bit0:儅僗僋屻僄儞僐乕僪昤夋僨乕僞
    // 20h偲偺榑棟榓偵傛傝婡擻儌僕儏乕儖敾掕丄11h偲偺榑棟榓偵傛傝昤夋乮嵟廔揑偵偼BOOL抣壔乯

private:
    int m_ncDataCodeWordBit; // 僨乕僞僐乕僪儚乕僪價僢僩挿
    BYTE m_byDataCodeWord[MAX_DATACODEWORD]; // 擖椡僨乕僞僄儞僐乕僪僄儕傾

    int m_ncDataBlock;
    BYTE m_byBlockMode[MAX_DATACODEWORD];
    int m_nBlockLength[MAX_DATACODEWORD];

    int m_ncAllCodeWord; // 憤僐乕僪儚乕僪悢(俼俽岆傝掶惓僨乕僞傪娷傓)
    BYTE m_byAllCodeWord[MAX_ALLCODEWORD]; // 憤僐乕僪儚乕僪嶼弌僄儕傾
    BYTE m_byRSWork[MAX_CODEBLOCK]; // 俼俽僐乕僪儚乕僪嶼弌儚乕僋

// 僨乕僞僄儞僐乕僪娭楢僼傽儞僋僔儑儞
public:
    BOOL EncodeData(int nLevel, int nVersion, BOOL bAutoExtent, int nMaskingNo, LPCSTR lpsSource, int ncSource = 0);

private:
    int GetEncodeVersion(int nVersion, LPCSTR lpsSource, int ncLength);
    BOOL EncodeSourceData(LPCSTR lpsSource, int ncLength, int nVerGroup);

    int GetBitLength(BYTE nMode, int ncData, int nVerGroup);

    int SetBitStream(int nIndex, WORD wData, int ncData);

    BOOL IsNumeralData(unsigned char c);
    BOOL IsAlphabetData(unsigned char c);
    BOOL IsKanjiData(unsigned char c1, unsigned char c2);

    BYTE AlphabetToBinaly(unsigned char c);
    WORD KanjiToBinaly(WORD wc);

    void GetRSCodeWord(LPBYTE lpbyRSWork, int ncDataCodeWord, int ncRSCodeWord);

// 儌僕儏乕儖攝抲娭楢僼傽儞僋僔儑儞
private:
    void FormatModule();

    void SetFunctionModule();
    void SetFinderPattern(int x, int y);
    void SetAlignmentPattern(int x, int y);
    void SetVersionPattern();
    void SetCodeWordPattern();
    void SetMaskingPattern(int nPatternNo);
    void SetFormatInfoPattern(int nPatternNo);
    int CountPenalty();
};

/////////////////////////////////////////////////////////////////////////////

#endif // !defined(AFX_QR_ENCODE_H__AC886DF7_C0AE_4C9F_AC7A_FCDA8CB1DD37__INCLUDED_)

.cpp

// QR_Encode.cpp : CQR_Encode 僋儔僗 僀儞僾儕儊儞僥乕僔儑儞 僼傽僀儖
// Date 2006/05/17    Ver. 1.22    Psytec Inc.

#include "stdafx.h"
#include "QR_Encode.h"


/////////////////////////////////////////////////////////////////////////////
// QR僐乕僪僶乕僕儑儞(宆斣)忣曬
static QR_VERSIONINFO QR_VersonInfo[] = {{0}, // (僟儈乕:Ver.0)
                                         { 1, // Ver.1
                                            26,   19,   16,   13,    9,
                                           0,   0,   0,   0,   0,   0,   0,
                                           1,  26,  19,
                                           1,  26,  16,
                                           1,  26,  13,
                                           1,  26,   9,
                                           0,   0,   0,
                                           0,   0,   0,
                                           0,   0,   0,
                                           0,   0,   0},
                                         { 2, // Ver.2
                                            44,   34,   28,   22,   16,
                                           1,  18,   0,   0,   0,   0,   0,
                                           1,  44,  34,
                                           1,  44,  28,
                                           1,  44,  22,
                                           1,  44,  16,
                                           0,   0,   0,
                                           0,   0,   0,
                                           0,   0,   0,
                                           0,   0,   0},
                                         { 3, // Ver.3
                                            70,   55,   44,   34,   26,
                                           1,  22,   0,   0,   0,   0,   0,
                                           1,  70,  55,
                                           1,  70,  44,
                                           2,  35,  17,
                                           2,  35,  13,
                                           0,   0,   0,
                                           0,   0,   0,
                                           0,   0,   0,
                                           0,   0,   0},
                                         { 4, // Ver.4
                                           100,   80,   64,   48,   36,
                                           1,  26,   0,   0,   0,   0,   0,
                                           1, 100,  80,
                                           2,  50,  32,
                                           2,  50,  24,
                                           4,  25,   9,
                                           0,   0,   0,
                                           0,   0,   0,
                                           0,   0,   0,
                                           0,   0,   0},
                                         { 5, // Ver.5
                                           134,  108,   86,   62,   46,
                                           1,  30,   0,   0,   0,   0,   0,
                                           1, 134, 108,
                                           2,  67,  43,
                                           2,  33,  15,
                                           2,  33,  11,
                                           0,   0,   0,
                                           0,   0,   0,
                                           2,  34,  16,
                                           2,  34,  12},
                                         { 6, // Ver.6
                                           172,  136,  108,   76,   60,
                                           1,  34,   0,   0,   0,   0,   0,
                                           2,  86,  68,
                                           4,  43,  27,
                                           4,  43,  19,
                                           4,  43,  15,
                                           0,   0,   0,
                                           0,   0,   0,
                                           0,   0,   0,
                                           0,   0,   0},
                                         { 7, // Ver.7
                                           196,  156,  124,   88,   66,
                                           2,  22,  38,   0,   0,   0,   0,
                                           2,  98,  78,
                                           4,  49,  31,
                                           2,  32,  14,
                                           4,  39,  13,
                                           0,   0,   0,
                                           0,   0,   0,
                                           4,  33,  15,
                                           1,  40,  14},
                                         { 8, // Ver.8
                                           242,  194,  154,  110,   86,
                                           2,  24,  42,   0,   0,   0,   0,
                                           2, 121,  97,
                                           2,  60,  38,
                                           4,  40,  18,
                                           4,  40,  14,
                                           0,   0,   0,
                                           2,  61,  39,
                                           2,  41,  19,
                                           2,  41,  15},
                                         { 9, // Ver.9
                                           292,  232,  182,  132,  100,
                                           2,  26,  46,   0,   0,   0,   0,
                                           2, 146, 116,
                                           3,  58,  36,
                                           4,  36,  16,
                                           4,  36,  12,
                                           0,   0,   0,
                                           2,  59,  37,
                                           4,  37,  17,
                                           4,  37,  13},
                                         {10, // Ver.10
                                           346,  274,  216,  154,  122,
                                           2,  28,  50,   0,   0,   0,   0,
                                           2,  86,  68,
                                           4,  69,  43,
                                           6,  43,  19,
                                           6,  43,  15,
                                           2,  87,  69,
                                           1,  70,  44,
                                           2,  44,  20,
                                           2,  44,  16},
                                         {11, // Ver.11
                                           404,  324,  254,  180,  140,
                                           2,  30,  54,   0,   0,   0,   0,
                                           4, 101,  81,
                                           1,  80,  50,
                                           4,  50,  22,
                                           3,  36,  12,
                                           0,   0,   0,
                                           4,  81,  51,
                                           4,  51,  23,
                                           8,  37,  13},
                                         {12, // Ver.12
                                           466,  370,  290,  206,  158,
                                           2,  32,  58,   0,   0,   0,   0,
                                           2, 116,  92,
                                           6,  58,  36,
                                           4,  46,  20,
                                           7,  42,  14,
                                           2, 117,  93,
                                           2,  59,  37,
                                           6,  47,  21,
                                           4,  43,  15},
                                         {13, // Ver.13
                                           532,  428,  334,  244,  180,
                                           2,  34,  62,   0,   0,   0,   0,
                                           4, 133, 107,
                                           8,  59,  37,
                                           8,  44,  20,
                                          12,  33,  11,
                                           0,   0,   0,
                                           1,  60,  38,
                                           4,  45,  21,
                                           4,  34,  12},
                                         {14, // Ver.14
                                           581,  461,  365,  261,  197,
                                           3,  26,  46,  66,   0,   0,   0,
                                           3, 145, 115,
                                           4,  64,  40,
                                          11,  36,  16,
                                          11,  36,  12,
                                           1, 146, 116,
                                           5,  65,  41,
                                           5,  37,  17,
                                           5,  37,  13},
                                         {15, // Ver.15
                                           655,  523,  415,  295,  223,
                                           3,  26,  48,  70,   0,   0,   0,
                                           5, 109,  87,
                                           5,  65,  41,
                                           5,  54,  24,
                                          11,  36,  12,
                                           1, 110,  88,
                                           5,  66,  42,
                                           7,  55,  25,
                                           7,  37,  13},
                                         {16, // Ver.16
                                           733,  589,  453,  325,  253,
                                           3,  26,  50,  74,   0,   0,   0,
                                           5, 122,  98,
                                           7,  73,  45,
                                          15,  43,  19,
                                           3,  45,  15,
                                           1, 123,  99,
                                           3,  74,  46,
                                           2,  44,  20,
                                          13,  46,  16},
                                         {17, // Ver.17
                                           815,  647,  507,  367,  283,
                                           3,  30,  54,  78,   0,   0,   0,
                                           1, 135, 107,
                                          10,  74,  46,
                                           1,  50,  22,
                                           2,  42,  14,
                                           5, 136, 108,
                                           1,  75,  47,
                                          15,  51,  23,
                                          17,  43,  15},
                                         {18, // Ver.18
                                           901,  721,  563,  397,  313,
                                           3,  30,  56,  82,   0,   0,   0,
                                           5, 150, 120,
                                           9,  69,  43,
                                          17,  50,  22,
                                           2,  42,  14,
                                           1, 151, 121,
                                           4,  70,  44,
                                           1,  51,  23,
                                          19,  43,  15},
                                         {19, // Ver.19
                                           991,  795,  627,  445,  341,
                                           3,  30,  58,  86,   0,   0,   0,
                                           3, 141, 113,
                                           3,  70,  44,
                                          17,  47,  21,
                                           9,  39,  13,
                                           4, 142, 114,
                                          11,  71,  45,
                                           4,  48,  22,
                                          16,  40,  14},
                                         {20, // Ver.20
                                          1085,  861,  669,  485,  385,
                                           3,  34,  62,  90,   0,   0,   0,
                                           3, 135, 107,
                                           3,  67,  41,
                                          15,  54,  24,
                                          15,  43,  15,
                                           5, 136, 108,
                                          13,  68,  42,
                                           5,  55,  25,
                                          10,  44,  16},
                                         {21, // Ver.21
                                          1156,  932,  714,  512,  406,
                                           4,  28,  50,  72,  94,   0,   0,
                                           4, 144, 116,
                                          17,  68,  以上是关于C++ 二维码生成的主要内容,如果未能解决你的问题,请参考以下文章

虚幻引擎二维码的生成与显示C++

虚幻引擎二维码的生成与显示C++

这些 C++ 代码片段有啥作用?

有趣的 C++ 代码片段,有啥解释吗? [复制]

以下代码片段 C++ 的说明

C++ 代码片段执行