Nadesłany przez Tomasz Lubiński, 30 października 2019 21:40
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.
Std25/Form1.cs:
//Kod kreskowy Standard 2 of 5 //(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.Std25 { public partial class Form1 : Form { private readonly static char[] code25signs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; private readonly static int[,] code25bars = { {1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0}, {1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0}, {1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0}, {1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0}, {1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0}, {1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0}, {1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0}, {1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0}, {1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0}, {1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0} }; private readonly static int[] start = {1, 1, 0, 1, 1, 0, 1, 0}; private readonly static int[] stop = {1, 1, 0, 1, 0, 1, 1, 0}; /// <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> /// Sprawdza czy podany txt mozna zakodowac Standard 2 of 5 /// </summary> /// <param name="txt">tekst do zakodowania</param> /// <returns>true jezeli mozna zakodowac, false w przeciwnym razie</returns> public bool check(string txt) { for (int i = 0; i < txt.Length; i++) { if (checkLetter(txt[i]) == false) return false; } return true; } /// <summary> /// Sprawdza czy podany znak mozna zakodowac Standard 2 of 5 /// </summary> /// <param name="ch">znak do zakodowania</param> /// <returns>true jezeli mozna zakodowac, false w przeciwnym razie</returns> private bool checkLetter(char ch) { for (int i = 0; i < code25signs.Length; i++) { if (code25signs[i] == ch) return true; } return false; } /// <summary> /// Zwraca wartosc danego znaku /// </summary> /// <param name="ch">znak</param> /// <returns>wartosc danego znaku</returns> private int getLetterValue(char ch) { for (int i = 0; i < code25signs.Length; i++) if (code25signs[i] == ch) return i; return -1; } /// <summary> /// Oblicza cyfre kontrolna dla podanego ciagu /// </summary> /// <param name="txt">Ciag dla ktorego obliczyc cyfre kontrolna</param> /// <returns>cyfre kontrolna dla podanego ciagu</returns> private int checkDigit(string txt) { int sum = 0; int w1 = 0; int w2 = 0; for (int i = 0; i < txt.Length; i++) { if (i % 2 == 0) { w1 += getLetterValue(txt[i]); } else { w2 += getLetterValue(txt[i]); } } if (txt.Length % 2 == 0) { w2 *= 3; } else { w1 *= 3; } sum = w1 + w2; sum %= 10; sum = 10 - sum; sum %= 10; return sum; } /// <summary> /// Generuj Standard 2 of 5 /// </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)); //Sprawdz czy podany ciag mozna zakodowac string code = Code.Text; if (check(code) == false) { MessageBox.Show("Nieprawidłowe znaki!", "Bład", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Wygeneruj i narysuj kod int[] bars = generateBars(code); for (int i = 0; i < bars.Length; i++) { //Sprawdz czy pasek bialy czy czarny (rysujemy tylko czarne, bo tlo jest biale) if (bars[i] == 1) { graphics.FillRectangle(blackBrush, i*2 + 10, 10, 2, 90); } } //Wypisz kodowany ciag pod kodem kreskowym for (int i = 0; i < code.Length; i++) { graphics.DrawString(code.Substring(i, 1), drawFont, blackBrush, i * 28 + 40, 102); } //Odswiez pictureBox.Refresh(); //Zwolinij zasoby whiteBrush.Dispose(); blackBrush.Dispose(); graphics.Dispose(); } /// <summary> /// Generuje kod kreskowy /// </summary> /// <param name="code">string do zakodowania kodem kreskowym</param> /// <returns>kod kreskowy - 1 pasek czarny, N - pasek bialy</returns> private int[] generateBars(string code) { int[] bars = new int[14 * (code.Length + 1) + 16]; //Dodaj znak start for (int j = 0; j < 8; j++) { bars[j] = start[j]; } //Dodaj kolejne znaki for (int i = 0; i < code.Length; i++) { int letterValue = getLetterValue(code[i]); for (int j = 0; j < 14; j++) { bars[i * 14 + 8 + j] = code25bars[letterValue, j]; } } //Dodaj sume kontrolna int checkDigitValue = checkDigit(code); for (int j = 0; j < 14; j++) { bars[code.Length * 14 + 8 + j] = code25bars[checkDigitValue, j]; } //Dodaj znak stop for (int j = 0; j < 8; j++) { bars[code.Length * 14 + 22 + j] = stop[j]; } return bars; } } }