Nadesłany przez Tomasz Lubiński, 30 października 2019 21:36
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
EAN13/Form1.cs:
//Kod kreskowy EAN-13 //(c) 2019 by Tomasz Lubinski //www.algorytm.org using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; namespace Barcodes.EAN13 { public partial class Form1 : Form { private readonly int[,] parityTable = { {0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 1, 1}, {0, 0, 1, 1, 0, 1}, {0, 0, 1, 1, 1, 0}, {0, 1, 0, 0, 1, 1}, {0, 1, 1, 0, 0, 1}, {0, 1, 1, 1, 0, 0}, {0, 1, 0, 1, 0, 1}, {0, 1, 0, 1, 1, 0}, {0, 1, 1, 0, 1, 0} }; private readonly int[,] right = { {1, 1, 1, 0, 0, 1, 0}, {1, 1, 0, 0, 1, 1, 0}, {1, 1, 0, 1, 1, 0, 0}, {1, 0, 0, 0, 0, 1, 0}, {1, 0, 1, 1, 1, 0, 0}, {1, 0, 0, 1, 1, 1, 0}, {1, 0, 1, 0, 0, 0, 0}, {1, 0, 0, 0, 1, 0, 0}, {1, 0, 0, 1, 0, 0, 0}, {1, 1, 1, 0, 1, 0, 0} }; private readonly int[, ,] left = { { {0, 0, 0, 1, 1, 0, 1}, {0, 0, 1, 1, 0, 0, 1}, {0, 0, 1, 0, 0, 1, 1}, {0, 1, 1, 1, 1, 0, 1}, {0, 1, 0, 0, 0, 1, 1}, {0, 1, 1, 0, 0, 0, 1}, {0, 1, 0, 1, 1, 1, 1}, {0, 1, 1, 1, 0, 1, 1}, {0, 1, 1, 0, 1, 1, 1}, {0, 0, 0, 1, 0, 1, 1} }, { {0, 1, 0, 0, 1, 1, 1}, {0, 1, 1, 0, 0, 1, 1}, {0, 0, 1, 1, 0, 1, 1}, {0, 1, 0, 0, 0, 0, 1}, {0, 0, 1, 1, 1, 0, 1}, {0, 1, 1, 1, 0, 0 ,1}, {0, 0, 0, 0, 1, 0, 1}, {0, 0, 1, 0, 0, 0 ,1}, {0, 0, 0, 1, 0, 0, 1}, {0, 0, 1, 0, 1, 1, 1} } }; /// <summary> /// Zainicjuj /// </summary> public Form1() { InitializeComponent(); pictureBox.Image = new Bitmap(pictureBox.Width, pictureBox.Height, PixelFormat.Format24bppRgb); System.Drawing.SolidBrush whiteBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White); System.Drawing.Graphics graphics = Graphics.FromImage(pictureBox.Image); graphics.FillRectangle(whiteBrush, new Rectangle(0, 0, pictureBox.Width, pictureBox.Height)); pictureBox.Refresh(); whiteBrush.Dispose(); graphics.Dispose(); } /// <summary> /// Generuj kod kreskowy EAN-13 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Generate_Click(object sender, EventArgs e) { //Zainicjuj narzedzia do rysowania System.Drawing.SolidBrush whiteBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White); System.Drawing.SolidBrush blackBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); System.Drawing.Graphics graphics = Graphics.FromImage(pictureBox.Image); Font drawFont = new Font("Arial", 10); //Wyczysc obszar rysowania graphics.FillRectangle(whiteBrush, new Rectangle(0, 0, pictureBox.Width, pictureBox.Height)); string code = Code.Text.Trim(); if (checkCode(code) == false) { MessageBox.Show("Nieprawidłowy kod EAN-13!", "Bład", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Wygeneruj i narysuj kod int[] bars = generateBars(code); int length; for (int i = 0; i < bars.Length; i++) { if ((i >= 0 && i <= 2) || (i >= 45 && i <= 49) || (i >= 92 && i <= 94)) { //Paski ochronne sa dluzsze length = 100; } else { //Paski kodujace cyfry sa krotsze length = 90; } if (bars[i] == 1) { graphics.FillRectangle(blackBrush, i * 2 + 20, 10, 2, length); } } //Wypisz cyfry pod kodem graphics.DrawString(code.Substring(0, 1), drawFont, blackBrush, 8, 100); for (int i = 1; i < 7; i++) { graphics.DrawString(code.Substring(i, 1), drawFont, blackBrush, i * 14 + 12, 102); } for (int i = 7; i < 13; i++) { graphics.DrawString(code.Substring(i, 1), drawFont, blackBrush, i * 14 + 22, 102); } //Odswiez pictureBox.Refresh(); //Zwolinij zasoby whiteBrush.Dispose(); blackBrush.Dispose(); graphics.Dispose(); } /// <summary> /// Sprawdz EAN-13 /// </summary> /// <param name="code"></param> /// <returns>true jezeli prawidlowy, false w przeciwnym razie</returns> private bool checkCode(string code) { //Sprawdz dlugosc if (code.Length != 13) { return false; } //Sprawdz czy podano tylko cyfry int[] values = new int[13]; for (int i = 0; i < 13; i++) { if (int.TryParse(code.Substring(i, 1), out values[i]) == false) return false; } //Sprawdz sume kontrolna int sum = 1 * values[0] + 3 * values[1] + 1 * values[2] + 3 * values[3] + 1 * values[4] + 3 * values[5] + 1 * values[6] + 3 * values[7] + 1 * values[8] + 3 * values[9] + 1 * values[10] + 3 * values[11]; sum %= 10; sum = 10 - sum; sum %= 10; if (sum != values[12]) { return false; } return true; } /// <summary> /// Generuje kod kreskowy /// </summary> /// <param name="code">kod EAN-13</param> /// <returns>kod kreskowy - 1 pasek czarny, 0 - pasek bialy</returns> private int[] generateBars(string code) { int[] bars = new int[95]; int[] EAN13 = new int[13]; for (int i = 0; i < 13; i++) { int.TryParse(code.Substring(i, 1), out EAN13[i]); } bars[0] = 1; bars[1] = 0; bars[2] = 1; for (int i = 1; i < 7; i++) { for (int j = 0; j < 7; j++) { bars[(i - 1) * 7 + 3 + j] = left[parityTable[EAN13[0], i - 1], EAN13[i], j]; } } bars[45] = 0; bars[46] = 1; bars[47] = 0; bars[48] = 1; bars[49] = 0; for (int i = 7; i < 13; i++) { for (int j = 0; j < 7; j++) { bars[(i - 7) * 7 + 50 + j] = right[EAN13[i], j]; } } bars[92] = 1; bars[93] = 0; bars[94] = 1; return bars; } } }