C# 條形碼 生成函式 (Code 128 標準參考:GB/T 18347-2001)
最近在做單據列印,發現客戶要求用到條形碼,在網上找了,發現只有一些條形碼的標準,但打出來發現根本不能掃,還要加某些字型.
以下程式碼經過實際掃描測試~!
原創,轉載請說明~!
[code=C#]
/// <summary>
/// 生成條形碼( 128條碼,標準參考:GB/T 18347-2001 )
/// BY JUNSON 20090508
/// </summary>
public class BarCode
{
/// <summary>
/// 條形碼生成函式
/// </summary>
/// <param name="text">條型碼字串</param>
/// <returns></returns>
public static Bitmap BuildBarCode(string text)
{
//查檢是否合條件TEXT
bool ck = CheckErrerCode(text);
if (!ck)
throw new Exception("條形碼字元不合要求,不能是漢字或全形字元");
string barstring = BuildBarString(text);
return KiCode128C(barstring, 30);
}
/// <summary>
/// 建立條碼字串
/// </summary>
/// <param name="tex">條碼內容</param>
/// <returns></returns>
private static string BuildBarString(string tex)
{
string barstart = "bbsbssbssss"; //碼頭
string barbody = ""; //碼身
string barcheck = ""; //碼檢
string barend = "bbsssbbbsbsbb"; //碼尾
int checkNum = 104;
//迴圈新增碼身,計算碼檢
for (int i = 1; i <= tex.Length; i++)
{
int index = (int)tex[i - 1] - 32;
checkNum += (index * i);
barbody += AddSimpleTag(index);//加入字元值的條碼標記
}
//碼檢值計算
barcheck = AddSimpleTag(int.Parse(Convert.ToDouble(checkNum % 103).ToString("0")));
string barstring = barstart + barbody + barcheck + barend;
return barstring;
}
//增加一個條碼標記
private static string AddSimpleTag(int CodeIndex)
{
string res = "";
/// <summary>1-4的條的字元標識 </summary>
string[] TagB ={ "", "b", "bb", "bbb", "bbbb" };
/// <summary>1-4的空的字元標識 </summary>
string[] TagS ={ "", "s", "ss", "sss", "ssss" };
string[] Code128List = new string[] {
"212222","222122","222221","121223","121322","131222","122213","122312","132212","221213",
"221312","231212","112232","122132","122231","113222","123122","123221","223211","221132",
"221231","213212","223112","312131","311222","321122","321221","312212","322112","322211",
"212123","212321","232121","111323","131123","131321","112313","132113","132311","211313",
"231113","231311","112133","112331","132131","113123","113321","133121","313121","211331",
"231131","213113","213311","213131","311123","311321","331121","312113","312311","332111",
"314111","221411","431111","111224","111422","121124","121421","141122","141221","112214",
"112412","122114","122411","142112","142211","241211","221114","413111","241112","134111",
"111242","121142","121241","114212","124112","124211","411212","421112","421211","212141",
"214121","412121","111143","111341","131141","114113","114311","411113","411311","113141",
"114131","311141","411131","211412","211214","211232" };
string tag = Code128List[CodeIndex];
for (int i = 0; i < tag.Length; i++)
{
string temp = "";
int num = int.Parse(tag[i].ToString());
if (i % 2 == 0)
{
temp = TagB[num];
}
else
{
temp = TagS[num];
}
res += temp;
}
return res;
}
/// <summary>
/// 檢查條形碼文字是否合條件(不能是漢字或全形字元)
/// </summary>
/// <param name="cktext"></param>
/// <returns></returns>
private static bool CheckErrerCode(string cktext)
{
foreach (char c in cktext)
{
byte[] tmp = System.Text.UnicodeEncoding.Default.GetBytes(c.ToString());
if (tmp.Length > 1)
return false;
}
return true;
}
/// <summary>生成條碼 </summary>
/// <param name="BarString">條碼模式字串</param> //Format32bppArgb
/// <param name="Height">生成的條碼高度</param>
/// <returns>條碼圖形</returns>
private static Bitmap KiCode128C(string BarString, int _Height)
{
Bitmap b = new Bitmap(BarString.Length, _Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//using (Graphics grp = Graphics.FromImage(b))
//{
try
{
char[] cs = BarString.ToCharArray();
for (int i = 0; i < cs.Length; i++)
{
for (int j = 0; j < _Height; j++)
{
if (cs[i] == 'b')
{
b.SetPixel(i, j, Color.Black);
}
else
{
b.SetPixel(i, j, Color.White);
}
}
}
//grp.DrawString(text, SystemFonts.CaptionFont, Brushes.Black, new PointF(leftEmpty, b.Height - botEmpty));
return b;
}
catch
{
return null;
}
//}
}
}
[/code]
相關文章
- Python 生成條形碼、二維碼 (Code 128、EAN-13、QR code等)Python
- iBarcoder for Mac條形碼生成工具Mac
- Java 條形碼生成(一維條形碼)Java
- Mac條形碼生成工具——iBarcoder for MacMac
- JavaScript 函式形參和實參JavaScript函式
- SR 提交email 標準格式 --- 供參考AI
- 軟體測試BUG參考標準
- 使用 Python 生成二維條形碼 QR CodesPython
- Ramda 函式庫參考教程函式
- php中函式的實參和形參PHP函式
- C標準庫參考指南系列譯文
- iOS 生成二維碼/條形碼iOS
- delphi函式參考手冊 (轉)函式
- python關於函式形參Python函式
- day10:函式的實參&形參函式
- js獲取指定函式的形參程式碼例項JS函式
- php生成條形碼的圖片PHP
- to_char函式格式轉換參考函式
- js 函式中形參與實參的關係JS函式
- #region(C# 參考)C#
- 找java培訓機構有哪些參考標準Java
- Delphi編碼標準——過程和函式 (轉)函式
- IOS 二維碼條形碼生成(原生程式碼)iOS
- 標準IO常用函式介面函式
- Hive函式(內建函式+自定義標準函式UDF)Hive函式
- 函式形參與實參的體會與對比函式
- Oracle分析函式參考手冊(轉)一Oracle函式
- Oracle分析函式參考手冊(轉)二Oracle函式
- 獲取javascript函式形參的數目JavaScript函式
- C#全形半形轉換函式C#函式
- 生成密碼函式密碼函式
- C語言標準函式庫C語言函式
- Oracle 9i 分析函式參考手冊Oracle函式
- 前端實現生成條形碼並下載前端
- Python的函式形參和返回值Python函式
- 列舉遊戲設計師可參考的7條有益準則遊戲設計師
- C標準庫參考指南系列譯文(11)stddef.h
- C標準庫參考指南系列譯文(10)stdarg.h