簡體   English   中英

有沒有辦法從 cpp 中的文本生成條形碼圖像?

[英]Is there a way to generate barcode image from text in cpp?

我有一個在 CPP 中生成條形碼編號的代碼,為了在收據中顯示它,我需要將該編號轉換為 Image(.bmp)。

是否有現有的庫或實現上述結果的方法?

這是我用於生成 Interleaved 2 of 5 條碼(最簡單的一維條碼)的一些代碼片段。 它只設置為數字而不是字母,但我相信你會明白的。

這決定了數字 0 到 9 的行間距

const char I25_Code[10][6] = { {"NNWWN"}, {"WNNNW"}, {"NWNNW"}, {"WWNNN"}, {"NNWNW"}, {"WNWNN"}, {"NWWNN"}, {"NNNWW"}, {"WNNWN"}, {"NWNWN"} };

這會生成條形碼:

void GenerateBarcodeI2of5(unsigned int startX, unsigned int startY, unsigned int width, unsigned int height, char *value)
{
    if(value==NULL)return;

    if(strlen(stringbuffer)==0) return;

    int minBarThickness = MIN_BARCODE_BAR_WIDTH;
    int barRatio = BARCODE_BAR_RATIO_3_TO_1 ? 3:2;

    bool bars = true;
    int sl = strlen(stringbuffer);
    int pixelX=0;
    pixelX=startX;
    for(int i=0;i<sl;i++)
    {
        char c = stringbuffer[i];
        if(bars)
        {
            if(c=='N' || c=='n')
            {
                GenerateLine(pixelX, startY, minBarThickness, height, false);
                pixelX+=minBarThickness;
            }
            else
            {
                GenerateLine(pixelX, startY, minBarThickness*barRatio, height, false);
                pixelX+=minBarThickness*barRatio;

            }
        }
        else
        {
            if(c=='N' || c=='n')
            {
                GenerateLine(pixelX, startY, minBarThickness, height, true);
                pixelX+=minBarThickness;
            }
            else
            {
                GenerateLine(pixelX, startY, minBarThickness*barRatio, height, true);
                pixelX+=minBarThickness*barRatio;                       
            }
        }
        bars=!bars;
    }
}

這里的前提是有一個字節數組表示位圖,子函數設置該字節數組中的位,例如 SetPixel。

void GenerateLine(unsigned int startX, unsigned int startY, unsigned int width, unsigned int height, bool inverse)
{
    int bit = inverse?0:1;
    for(int x = startX; x<(width+startX); x++)
    {
        for(int y=startY; y<(startY+height); y++)
        {
            if(x>=IMAGE_COLUMNS) continue;
            if(y>=IMAGE_ROWS) continue;
            SetPixel(x,y,ScreenImageData,IMAGE_ARRAY_COLUMN_DATA_BYTES, bit);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM