Professional Documents
Culture Documents
using
using
using
using
using
System;
System.Collections.Generic;
System.Text;
System.Drawing;
System.Drawing.Drawing2D;
System.Drawing.Imaging;
namespace BarCodeEan13
{
class Ean13
{
#region Static initialization
static Dictionary<int, Pattern> codes = new Dictionary<int, Pattern>();
static Dictionary<int, Parity> parity = new Dictionary<int, Parity>();
static Ean13()
{
//
#
AddCode(0,
AddCode(1,
AddCode(2,
AddCode(3,
AddCode(4,
AddCode(5,
AddCode(6,
AddCode(7,
AddCode(8,
AddCode(9,
LEFT ODD
"0001101",
"0011001",
"0010011",
"0111101",
"0100011",
"0110001",
"0101111",
"0111011",
"0110111",
"0001011",
AddParity(0,
AddParity(1,
AddParity(2,
AddParity(3,
AddParity(4,
AddParity(5,
AddParity(6,
AddParity(7,
AddParity(8,
AddParity(9,
LEFT EVEN
"0100111",
"0110011",
"0011011",
"0100001",
"0011101",
"0111001",
"0000101",
"0010001",
"0001001",
"0010111",
RIGHT
"1110010");
"1100110");
"1101100");
"1000010");
"1011100");
"1001110");
"1010000");
"1000100");
"1001000");
"1110100");
"ooooo");
"oeoee");
"oeeoe");
"oeeeo");
"eooee");
"eeooe");
"eeeoo");
"eoeoe");
"eoeeo");
"eeoeo");
}
static void AddCode(int digit, string lhOdd, string lhEven, string rh)
{
Pattern p = new Pattern();
p.LhOdd = lhOdd; p.LhEven = lhEven; p.Rh = rh;
codes.Add(digit, p);
}
static void AddParity(int digit, string par)
{
parity.Add(digit, new Parity(par));
}
#endregion
private Ean13Settings settings;
private string code;
private string title;
if (c == '1')
g.FillRectangle(brush, left, top, settings.BarWidth, h);
left += settings.BarWidth;
}
return left;
}
private int DrawLeftGuard(Ean13Settings settings, Graphics g, string dig
it, int left)
{
int h = (int)(settings.BarCodeHeight * 0.8);
g.DrawString(digit, settings.Font, brush, left, h + top);
left += (int)g.MeasureString(digit, settings.Font).Width;
//TITLE
if (title != null)
g.DrawString(title, settings.Font, brush, left, settings.TopMarg
in);
g.FillRectangle(brush, left, top, settings.BarWidth, settings.BarCod
eHeight); //1
left += settings.BarWidth;
left += settings.BarWidth;
//0
g.FillRectangle(brush, left, top, settings.BarWidth, settings.BarCod
eHeight); //1
left += settings.BarWidth;
return left;
}
private int DrawRightGuard(Ean13Settings settings, Graphics g, int left)
{
g.FillRectangle(brush, left, top, settings.BarWidth, settings.BarCod
eHeight); //1
left += settings.BarWidth;
left += settings.BarWidth;
//0
g.FillRectangle(brush, left, top, settings.BarWidth, settings.BarCod
eHeight); //1
left += settings.BarWidth;
return left;
}
private int DrawCenterGuard(Ean13Settings settings, Graphics g, int left
)
{
left += settings.BarWidth;
//0
g.FillRectangle(brush, left, top, settings.BarWidth, settings.BarCod
eHeight); //1
left += settings.BarWidth;
left += settings.BarWidth;
//0
g.FillRectangle(brush, left, top, settings.BarWidth, settings.BarCod
eHeight); //1
left += settings.BarWidth;
left += settings.BarWidth;
//0
return left;
}
class Pattern
{
private string lhOdd;
public string LhOdd
{
get { return lhOdd; }
set { lhOdd = value; }
}
private string lhEven;
public string LhEven
{
get { return lhEven; }
set { lhEven = value; }
}
private string rh;
public string Rh
{
get { return rh; }
set { rh = value; }
}
}
class Parity
{
private string par;
internal Parity(string par)
{
this.par = par;
}
internal bool IsOdd(int i)
{
return par[i] == 'o';
}
internal bool IsEven(int i)
{
return par[i] == 'e';
}
}
}
class Ean13Settings
{
private int height = 120;
public int BarCodeHeight
{
get { return height; }
set { height = value; }
}
private int leftMargin = 10;
public int LeftMargin
{
get { return leftMargin; }
set { leftMargin = value; }
}
private int rightMargin = 10;
public int RightMargin
{
get { return rightMargin; }
set { rightMargin = value; }
}
private int topMargin = 10;
public int TopMargin
{
get { return topMargin; }
set { topMargin = value; }
}
private int bottomMargin = 10;
public int BottomMargin
{
get { return bottomMargin; }
set { bottomMargin = value; }
}
private int barWidth = 2;
public int BarWidth
{
get { return barWidth; }
set { barWidth = value; }
}
private Font font = new Font(FontFamily.GenericSansSerif, 10);
internal Font Font
{
get { return font; }
}
}
class Program
{
static void Main(string[] args)
{
/*
//string isbn = "0-7356-1917-4";
string isbn = "90 261 1455 9";
bool valid = CheckIsbn(isbn);
if (!valid)
throw new Exception();
string code = IsbnToEan13(isbn);
Ean13 barcode = new Ean13(code, "ISBN " + isbn);
*/
//Ean13 barcode = new Ean13("5028421973326", null);
Ean13 barcode = new Ean13("4009900409537", null);
barcode.Paint().Save("c:\\temp\\test.png", ImageFormat.Png);
}
static bool CheckIsbn(string isbn)
{
if (isbn == null)
return false;
isbn = NormalizeIsbn(isbn);
if (isbn.Length != 10)
return false;
int res;
for (int i = 0; i < 9; i++)
if (!int.TryParse(isbn[i].ToString(), out res))
return false;
int sum = 0;
for (int i = 0; i < 9; i++)
sum += (i + 1) * int.Parse(isbn[i].ToString());
int r = sum % 11;
if (r == 10)
return isbn[9] == 'X';
else
return isbn[9] == (char)('0' + r);
}
static string IsbnToEan13(string isbn)
{
isbn = NormalizeIsbn(isbn);
string code = "978" + isbn.Substring(0, 9);
code += (char)('0' + Ean13.CalculateChecksum(code));
return code;
}