Springen naar inhoud


* * * * * 1 stemmen

Barcode Generator Ean13 In C# 2017

EAN13 barcode c# 2017

  • Log in a.u.b. om te beantwoorden
Er zijn 2 reacties in dit onderwerp

#1 doebi

doebi

    Master Developer

  • Leden
  • PipPipPipPipPip
  • 537 berichten
    Laatst bezocht 16 aug 2018 11:23

Geplaatst op 02 januari 2018 - 18:55

Vroeger al eens een barcode generator geschreven in vb6.0

Nu een volledig nieuwe gemaakt, afgewerkt op enkele details na,
oa het plaatsen van de human readable text, maar dat doe ik wel
eens later.

Plak dit in een nieuwe class
Code:
// author	   Doubbel Danny
//			  dannydoubbel@gmail.com
// free for use
// keep in mind : no code is ever 100% error free
// no responsabilty for usage of this code !
// possible damage or loss by using this code is for your own risk
// use at your own risk !
//
// any suggestions of improvement may be sent to dannydoubbel@gmail.com

using System;
using System.Drawing;


namespace MyDatabase	 // use your own namespace here
{
	// for info about EAN13 barcode structure, thanks to :
	// https://en.wikipedia.org/wiki/International_Article_Number

	public class BarcodeImageGenerator
	{
		string[] LCodes = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" };
		string[] GCodes = { "0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111" };
		string[] RCodes = { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" };
		string[] FirstSix = { "LLLLLL", "LLGLGG", "LLGGLG", "LLGGGL", "LGLLGG", "LGGLLG", "LGGGLL", "LGLGLG", "LGLGGL", "LGGLGL" };
		string[] LastSix = { "RRRRRR", "RRRRRR", "RRRRRR", "RRRRRR", "RRRRRR", "RRRRRR", "RRRRRR", "RRRRRR", "RRRRRR", "RRRRRR" };
		string StartMarker = "101";
		string MiddleMarker = "01010";
		string EndMarker = "101";
		// digit 1 encode key for the rest of the digitis
		// digit 2-7 encoded bij first digit and can be LCodes,GCodes or LCodes
		// digit 8-13 encodesd always like RCode
		// startmarker + first six digits + middlemarker + next six digits + endmarker

		public Bitmap CreateBarcodeBitmap(string EAN13Binary, int LineThickness, int Height, int Height2 = 0, int OffsetLeft = 0)
		{
			// author	   Doubbel Danny
			//			  dannydoubbel@gmail.com
			// usage example :
			// bmpEAN13 = MyBarcode.CreateBarcodeBitmap(EAN13Binary, 3, 150, 200, 100);
			// if (bmpEAN13 != null)
			// {
			//	pictureBox1.Image = bmpEAN13;
			//	pictureBox1.Width = bmpEAN13.Width;
			//	pictureBox1.Height = bmpEAN13.Height;
			// }
			//
			//
			// todo :
			//	  1 add optional parameter human readable text
			//	  2 add optional parameters drawcolor and backgroundcolor
			if (EAN13Binary.Length == 95)
			{
				// LineThickness may not be 0 or less
				if (LineThickness < 1)
				{
					LineThickness = 1;
				}
				int CalcWidth;
				CalcWidth = OffsetLeft + ((3 + 5 + 3 + 6 * 7 + 6 * 7) * LineThickness);
				int CalcHeight = Height;
				if (Height2 > 0)
				{
					if (Height2 < Height)
					{
						// if height2 is greater then height, swop height2 & height
						Height = Height2;
						Height2 = CalcHeight;
					}
					else
					{
						CalcHeight = Height2;
					}
				}
				Bitmap bmpResult = new Bitmap(CalcWidth, CalcHeight);
				using (Graphics g = Graphics.FromImage(bmpResult))
				{
					int DrawHeight = Height;
					g.Clear(Color.White);
					for (int Lus = 0; Lus < EAN13Binary.Length; Lus++)
					{
						if (Height2 > 0)
						{
							// at first, lets asume not drawing a EAN13 marker
							bool Longer = false;
							// drawing the first EAN13 marker ?
							if (Lus < 3)
							{
								Longer = true;
							}
							else
							{
								// drawing the middle EAN13 marker ?
								if ((Lus > 3 + 6 * 7) & (Lus < 3 + 6 * 7 + 5))
								{
									Longer = true;
								}
								else
								{
									// drawing the last (right) EAN13 marker ?
									if ((Lus > 3 + 12 * 7 + 4))
									{
										Longer = true;
									}
								}
							}
							// if its a EAN13 marker (first,middle or last)
							// draw the stripes longer, else draw them shorter
							if (Longer)
							{
								DrawHeight = Height2;
							}
							else
							{
								DrawHeight = Height;
							}
						}
						else
						{
							DrawHeight = Height;
						}
						// todo, make code below in a switch .. case .. default structure
						if (EAN13Binary[Lus] == '1')
						{
							// it's a stripe ( = "1"), so draw it
							for (int LusThickness = 0; LusThickness < LineThickness; LusThickness++)
							{
								// draw the stripe x times depending on the LineThickness
								g.DrawLine(
									new Pen(Color.Black),
									new Point((Lus * LineThickness + LusThickness) + OffsetLeft, 0),
									new Point((Lus * LineThickness + LusThickness) + OffsetLeft, DrawHeight-1)
								);
							}
						}
						else
						if (EAN13Binary[Lus] == '0')
						{
							// the stripe is 0 , don't have to draw anything
						}
						else
						{
							// something is wrong, the digit we read was not '0' or '1'
							// so leave this function while returning null
							return null;
						}
					}
				}
				// return this new bitmap to this function
				return bmpResult;
			}
			else
			{
				// there was an error
				// so return null to this function
				return null;
			}

		}
		public string CreateBarcodeBinaryString(string EAN13)
		{
			// author	   Doubbel Danny
			//			  dannydoubbel@gmail.com
			// usage example :
			// BarcodeImageGenerator MyBarcode = new BarcodeImageGenerator();
			// Bitmap bmpEAN13 = new Bitmap(1, 1);
			// string EAN13Binary = MyBarcode.CreateBarcodeBinaryString("9781491988534");
			//
			// returns :
			// or a 95 char long string with '0' or '1' chars
			// or a "" (empty string) if invalid input
			//
			// todo :
			// check checksum nummer for the inputstring, if incorrect return "" (empty string)
			String strBarcode = ""; // this is the string where the boolean barcode pattern will be stored
			// test if the input EAN13 string length is conform
			if (EAN13.Length == 13)
			{
				// the length is conform, so continue
				// for starters, the first binary part is the start marker
				strBarcode = StartMarker;
				byte HelpNumber = 0;  // this is the val of the char at position DigitNumber in EAN13
				byte DigitNumber; // this is the position in the EAN13 we will evalutate
				// set the postion in the EAN13 to 0 in order to get the decodepattern
				DigitNumber = 0;
				// process the first digit, the encode pattern string
				HelpNumber = System.Convert.ToByte(EAN13[DigitNumber]); // get a byte from a char in the EAN13 string
				HelpNumber -= 48; // okay 48 is the val of char "0"
				string DecodePattern = FirstSix[HelpNumber];
				// set the position in the EAN13 to 1
				DigitNumber = 1;
				//now process the first pack of 6 digits
				for (int lus = 0; lus < 6; lus++)
				{
					// get the digit at the Position in the EAN13
					HelpNumber = System.Convert.ToByte(EAN13[lus + 1]);  // + 1 because of the leading (decoding) digit
					HelpNumber -= 48;
					switch (DecodePattern[DigitNumber - 1])
					{
						case 'L':
							{
								strBarcode = strBarcode + LCodes[HelpNumber];
								break;
							}
						case 'R':
							{
								strBarcode = strBarcode + RCodes[HelpNumber];
								break;
							}
						case 'G':
							{
								strBarcode = strBarcode + GCodes[HelpNumber];
								break;
							}
					}
					// increase the position in the EAN13 for the next digtit to process
					DigitNumber++;
				}
				// first 6 pack of digits is processed
				// add the middle marker
				strBarcode = strBarcode + MiddleMarker;
				// set the postion in the EAN13 to 0 in order to get the decodepattern
				DigitNumber = 0;
				HelpNumber = System.Convert.ToByte(EAN13[DigitNumber]);
				HelpNumber -= 48; // okay 48 is the val of char "0"
				// previos lines are not realy necessary because for the last 6 digits it's always RCode in EAN13
				// set the position in the EAN13 to 8
				DigitNumber = 8;
				DecodePattern = LastSix[HelpNumber];
				// now process the last 6 digits
				for (int lus = 0; lus < 6; lus++)
				{
					// EAN13 last 6 digits are always RCodes
					HelpNumber = System.Convert.ToByte(EAN13[lus + 1 + 6]); // + 1 because of the leading digit , + 6 because there are already 6 digits processed
					HelpNumber -= 48; // okay 48 is the val of char "0"
					strBarcode = strBarcode + RCodes[HelpNumber];
					// increase the position in the EAN13 for the next digtit to process
					DigitNumber++;
				}
				// last 6 digits are processed
				// add the end marker
				strBarcode = strBarcode + EndMarker;
			   
			}
			else
			{
				// the input EAN string length was not conform
				strBarcode = "";
			}
			return strBarcode;
		}
	}
}






en plaats dit in je main programma (hier gebruikt onder een button) en een picturebox1 is getekend op het form:

Code:
private void button1_Click_1(object sender, EventArgs e)
		{
			BarcodeImageGenerator MyBarcode = new BarcodeImageGenerator();
			Bitmap bmpEAN13 = new Bitmap(1,1);
			string EAN13Binary = MyBarcode.CreateBarcodeBinaryString("9781491988534");
			if (EAN13Binary != "")
			{
				MessageBox.Show("Lengte =" + EAN13Binary.Length);
				bmpEAN13 = MyBarcode.CreateBarcodeBitmap(EAN13Binary, 3, 150, 200, 100);
				if (bmpEAN13 != null)
				{
					pictureBox1.Image = bmpEAN13;
					pictureBox1.Width = bmpEAN13.Width;
					pictureBox1.Height = bmpEAN13.Height;
				}
			}
		}



Opmerkingen, suggesties of verbeteringen zijn welkom in reply of pm of mail


grtz

#2 RedThread

RedThread

    Beheerder VBIB

  • Beheerder
  • 3590 berichten
    Laatst bezocht 16 aug 2018 11:34
  • LocatieTongeren,Belgium.
Inzender

Geplaatst op 02 januari 2018 - 20:10

Je kunt er ook een class library van maken, dan hoef je enkel een referentie naar de dll te leggen en de dll te delen.

We goed documenteren dan ;-)

#3 Damic

Damic

    Master Developer

  • Leden
  • PipPipPipPipPip
  • 638 berichten
    Laatst bezocht 10 aug 2018 14:20
  • LocatieMechelen (BE)

Geplaatst op 12 januari 2018 - 18:56

Nu de rest nog toevoegen :Y Code 39, Code 128, QR, datamatrix (screenshot van internet geplukt)
Geplaatste afbeelding




0 gebruiker(s) lezen dit onderwerp

0 lid(leden), 0 bezoeker(s), 0 anonieme gebruikers

Inloggen


Untitled 1

Met dank aan J├╝rgen voor de jarenlange inzet van visualbasic.be (anno dec 2000)
Met dank aan Mike en Ronneke voor de jarenlange inzet van vbib.be (anno dec 2010)
Met dank aan PascalBianca voor de jarenlange inzet van vbib.be (anno dec 2016)