zryou 的 條碼產生代碼(Code39碼)
『Code 39條碼介紹』
code39條碼能表示字母、數字和其它一些符號共43個字元:A -Z, 0-9, -.$/ + %, space等,其主要特點如下:
◆條碼的長度是可變化的
◆通常用“*”號作為起始、終止符
◆校正碼不用
◆代碼密度介於3 - 9.4個字元/每英寸
◆空白區是窄條的10倍
◆用於工業、圖書、以及票證自動化管理上
三九碼是Intermec公司於1975年推出的一種條碼,它可表示數字、英文字母以及“-”、“.”、“/”、“ ”、“%”、“$”、“ ”(空格)和“*”共44個符號,其中“*”僅作為啟始符和終止符。
三九碼且有編碼規則簡單、誤碼率低、所能表示字元個數多等特點,因此在各個領域有著極為廣泛的應用。我國也制定了相應的國家標準(GB12908-91)。
三九碼僅有兩種單元寬度——分別為寬單元和窄單元。寬單元這寬度為窄單元的1到3倍,一般多選用2倍、2.5倍或3倍。三九碼的每一個條碼字元由九個單元組成,其中有三個寬單元,其餘是窄單元,因此稱為三九碼。
我國有的圖書館使用的39碼使用了特殊的校正字元,條碼軟體都可列印適用於圖書館的39碼。
代碼原創: LovlyPuppy
實現該功能分兩個類,第一個類 DrawImageBord.cs
using System.Drawing;
using System.Drawing.Imaging;
public abstract class DrawImageBord
...{
protected virtual string BordRuleName
...{
get ...{ return string.Empty; }
}
protected virtual System.Collections.Hashtable Roles
...{
get ...{ return new System.Collections.Hashtable(); }
}
string drawString;
int width = 800; //畫布的寬度(可計算)
int height = 36;//1CM
int unitWidth = 1; //
int currentLocation = 0;
public DrawImageBord(string s)
...{
drawString = s;
}
public virtual void Draw(System.IO.Stream target)
...{
Bitmap bm = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bm);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
//畫布和邊的設定
g.Clear(Color.White);
g.DrawRectangle(Pens.White, 0, 0, width, height);
for(int i = 0; i < drawString.Length; i++)
...{
this.DrawString(drawString[i].ToString(), g);
}
bm.Save(target, ImageFormat.Jpeg);
}
protected virtual void DrawString(string s, Graphics g)
...{
System.Collections.Hashtable hash = this.Roles;
object o = hash[s];
if (o == null) return;
char[] chars = o.ToString().ToCharArray();
if (chars.Length > 9) return;
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush witeBrush = new SolidBrush(Color.White);
for(int i = 0; i < 5; i++)
...{
//畫第一個 0 黑條
if (chars[i] == '0')
...{
Rectangle re1 = new Rectangle(currentLocation, 0, unitWidth, height);
g.FillRectangle(blackBrush, re1);
currentLocation += unitWidth;
}
else
...{
Rectangle re1 = new Rectangle(currentLocation, 0, 3 * unitWidth, height);
g.FillRectangle(blackBrush, re1);
currentLocation += 3 * unitWidth;
}
//畫第6個 5 白條
if ((i + 5) < 9)
...{
if (chars[i+5] == '0')
...{
Rectangle re1 = new Rectangle(currentLocation, 0, unitWidth, height);
g.FillRectangle(witeBrush, re1);
currentLocation += unitWidth;
}
else
...{
Rectangle re1 = new Rectangle(currentLocation, 0, 3 * unitWidth, height);
g.FillRectangle(witeBrush, re1);
currentLocation += 3 * unitWidth;
}
}
}
Rectangle re2 = new Rectangle(currentLocation, 0, unitWidth, height);
g.FillRectangle(witeBrush, re2);
currentLocation += unitWidth;
}
}
第二個類CODE39DrawImageBord.cs 繼承DrawImageBord.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/**//**//**//// <summary>
/// CODE39DrawImageBord 的摘要說明
/// </summary>
public class CODE39DrawImageBord : DrawImageBord
......{
private System.Collections.Hashtable hash = new System.Collections.Hashtable();
protected override string BordRuleName
......{
get ......{ return "CODE39"; }
}
public CODE39DrawImageBord(string s)
: base(s)
......{
}
protected override System.Collections.Hashtable Roles
......{
get
......{
if (hash.Count > 0) return hash;
hash.Add("0", "001100100");
hash.Add("1", "100010100");
hash.Add("2", "010010100");
hash.Add("3", "110000100");
hash.Add("4", "001010100");
hash.Add("5", "101000100");
hash.Add("6", "011000100");
hash.Add("7", "000110100");
hash.Add("8", "100100100");
hash.Add("9", "010100100");
hash.Add("A", "100010010");
hash.Add("B", "010010010");
hash.Add("C", "110000010");
hash.Add("D", "001010010");
hash.Add("E", "101000010");
hash.Add("F", "011000010");
hash.Add("G", "000110010");
hash.Add("H", "100100010");
hash.Add("I", "010100010");
hash.Add("J", "001100010");
hash.Add("K", "100010001");
hash.Add("L", "010010001");
hash.Add("M", "110000001");
hash.Add("N", "001010001");
hash.Add("O", "101000001");
hash.Add("P", "011000001");
hash.Add("Q", "000110001");
hash.Add("R", "100100001");
hash.Add("S", "010100001");
hash.Add("T", "001100001");
hash.Add("U", "100011000");
hash.Add("V", "010011000");
hash.Add("W", "110001000");
hash.Add("X", "001011000");
hash.Add("Y", "101001000");
hash.Add("Z", "011001000");
hash.Add("-", "000111000");
hash.Add("%", "100101000");
hash.Add("$", "010101000");
hash.Add("*", "001101000");
return hash;
}
}
}
調用的方法:
protected void Page_Load(object sender, EventArgs e)
...{
CODE39DrawImageBord dr = new CODE39DrawImageBord("*3949178*");
dr.Draw(Response.OutputStream);
}
--顯示結果:
--顯示結果:
補充說明:
產生的這個條碼,並沒有用掃描槍去識別,
以後我會用Symbol DS6608數字掃描器 來識別它的準確性.