一般字元 轉 CODE128 演算法 -摘自網路

來源:互聯網
上載者:User

1、CODE128基礎知識

CODE128有三個版本:
CODE128A: 標準數字和字母, 控制符, 特殊字元
CODE128B: 標準數字和字母, 小寫字母, 特殊字元
CODE128C: [00]-[99]的數字對集合, 共100個

這裡採用CODE128B版本,系統內需要安裝Code 128字型

2、條碼編碼規則

條碼由開始位、資料、校正位、停止位組成

條碼=Convert.ToChar(204)+資料+Convert.ToChar(校正碼)+Convert.ToChar(206);

需要計算的就是校正碼了,下面直接給出轉換函式:

 

private string Get128CodeString(string inputData)
{
    string result;
    int checksum=104;
    for(int ii=0;ii<inputData.Length;ii++)
    {
        if(inputData[ii]>=32)
        {
            checksum+=(inputData[ii]-32)*(ii+1);
        }
        else        
        {
            checksum+=(inputData[ii]+64)*(ii+1);
        }            
    }
    checksum=checksum%103;        
    if(checksum<95)
    {
        checksum+=32;
    }
    else
    {
        checksum+=100;        
    }
    result=Convert.ToChar(204)+inputData.ToString()+Convert.ToChar(checksum)+Convert.ToChar(206);
    return result;
}

 

3、列印

 

public void PrintLable()
{
    PrintDocument pd = new PrintDocument();
    StandardPrintController controler = new StandardPrintController();

    try
    {    
        pd.PrintPage+=new PrintPageEventHandler(this.PrintCustomLable);
        pd.PrintController = controler;
        pd.Print();                    
        return;
    }
    catch(Exception err)
    {
        Console.WriteLine(err.Message);
        return;
    }
    finally
    {
        pd.Dispose();
    }

}
public void PrintCustomLable(Object Sender,PrintPageEventArgs av)
{
    Font ft1 = new System.Drawing.Font("Times New Roman",18,FontStyle.Regular,GraphicsUnit.World);
    Font ft2 = new System.Drawing.Font("Code 128",64,FontStyle.Regular,GraphicsUnit.World);
    Brush br = new SolidBrush(Color.Black);
    Margins margins = new Margins(50,50,50,145);
    av.PageSettings.Margins = margins;
    
    av.Graphics.DrawString(Get128CodeString(inputString),ft2,br,50,-3);
    av.Graphics.DrawString(inputString,ft1,br,110,60);
    av.HasMorePages = false;
}

 

看到評論說是不能用,今天測試了一下,發現寫錯了一個地方:

 

條碼=Convert.ToChar(204)+資料+Convert.ToChar(校正碼)+Convert.ToChar(206);

代碼:

result=Convert.ToChar(204)+checksum.ToString()+Convert.ToChar(checksum)+Convert.ToChar(206);

應該改為:
result=Convert.ToChar(204)+inputData.ToString()+Convert.ToChar(checksum)+Convert.ToChar(206);

 

簡單測試程式:

Vs2008測試程式

代碼用到列印的東西需要引用:

using System.Drawing.Printing;

 

定義全域變數:

string inputString;

在按鈕click中測試:

inputString = "0123456789";

//查看Code128碼的值:

//lblBarCode.Text = Get128CodeString(inputString);

PrintLable();

 

 

 

 

 

如果還有問題,那是系統裡沒有安裝Code 128字型 Code128字型下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.