Nadesłany przez Tomasz Lubiński, 30 października 2019 21:38
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.
Code39Mod43/Form1.cs:
//Kod kreskowy Code 39 Mod 43 //(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.Code39Mod43 { public partial class Form1 : Form { private readonly char[] code39signs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%' }; private readonly char[,] code39bars = { {'N', 'N', 'N', 'W', 'W', 'N', 'W', 'N', 'N'}, {'W', 'N', 'N', 'W', 'N', 'N', 'N', 'N', 'W'}, {'N', 'N', 'W', 'W', 'N', 'N', 'N', 'N', 'W'}, {'W', 'N', 'W', 'W', 'N', 'N', 'N', 'N', 'N'}, {'N', 'N', 'N', 'W', 'W', 'N', 'N', 'N', 'W'}, {'W', 'N', 'N', 'W', 'W', 'N', 'N', 'N', 'N'}, {'N', 'N', 'W', 'W', 'W', 'N', 'N', 'N', 'N'}, {'N', 'N', 'N', 'W', 'N', 'N', 'W', 'N', 'W'}, {'W', 'N', 'N', 'W', 'N', 'N', 'W', 'N', 'N'}, {'N', 'N', 'W', 'W', 'N', 'N', 'W', 'N', 'N'}, {'W', 'N', 'N', 'N', 'N', 'W', 'N', 'N', 'W'}, {'N', 'N', 'W', 'N', 'N', 'W', 'N', 'N', 'W'}, {'W', 'N', 'W', 'N', 'N', 'W', 'N', 'N', 'N'}, {'N', 'N', 'N', 'N', 'W', 'W', 'N', 'N', 'W'}, {'W', 'N', 'N', 'N', 'W', 'W', 'N', 'N', 'N'}, {'N', 'N', 'W', 'N', 'W', 'W', 'N', 'N', 'N'}, {'N', 'N', 'N', 'N', 'N', 'W', 'W', 'N', 'W'}, {'W', 'N', 'N', 'N', 'N', 'W', 'W', 'N', 'N'}, {'N', 'N', 'W', 'N', 'N', 'W', 'W', 'N', 'N'}, {'N', 'N', 'N', 'N', 'W', 'W', 'W', 'N', 'N'}, {'W', 'N', 'N', 'N', 'N', 'N', 'N', 'W', 'W'}, {'N', 'N', 'W', 'N', 'N', 'N', 'N', 'W', 'W'}, {'W', 'N', 'W', 'N', 'N', 'N', 'N', 'W', 'N'}, {'N', 'N', 'N', 'N', 'W', 'N', 'N', 'W', 'W'}, {'W', 'N', 'N', 'N', 'W', 'N', 'N', 'W', 'N'}, {'N', 'N', 'W', 'N', 'W', 'N', 'N', 'W', 'N'}, {'N', 'N', 'N', 'N', 'N', 'N', 'W', 'W', 'W'}, {'W', 'N', 'N', 'N', 'N', 'N', 'W', 'W', 'N'}, {'N', 'N', 'W', 'N', 'N', 'N', 'W', 'W', 'N'}, {'N', 'N', 'N', 'N', 'W', 'N', 'W', 'W', 'N'}, {'W', 'W', 'N', 'N', 'N', 'N', 'N', 'N', 'W'}, {'N', 'W', 'W', 'N', 'N', 'N', 'N', 'N', 'W'}, {'W', 'W', 'W', 'N', 'N', 'N', 'N', 'N', 'N'}, {'N', 'W', 'N', 'N', 'W', 'N', 'N', 'N', 'W'}, {'W', 'W', 'N', 'N', 'W', 'N', 'N', 'N', 'N'}, {'N', 'W', 'W', 'N', 'W', 'N', 'N', 'N', 'N'}, {'N', 'W', 'N', 'N', 'N', 'N', 'W', 'N', 'W'}, {'W', 'W', 'N', 'N', 'N', 'N', 'W', 'N', 'N'}, {'N', 'W', 'W', 'N', 'N', 'N', 'W', 'N', 'N'}, {'N', 'W', 'N', 'W', 'N', 'W', 'N', 'N', 'N'}, {'N', 'W', 'N', 'W', 'N', 'N', 'N', 'W', 'N'}, {'N', 'W', 'N', 'N', 'N', 'W', 'N', 'W', 'N'}, {'N', 'N', 'N', 'W', 'N', 'W', 'N', 'W', 'N'} }; private readonly char[] start_stop = { 'N', 'W', 'N', 'N', 'W', 'N', 'W', 'N', 'N' }; /// <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 Code 39 /// </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 Code 39 /// </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 < code39signs.Length; i++) { if (code39signs[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 < code39signs.Length; i++) if (code39signs[i] == ch) return i; return -1; } /// <summary> /// Zwraca cyfre kontrolna dla danego ciagu /// </summary> /// <param name="?"></param> /// <returns></returns> private int checkDigit(string code) { int sum = 0; for (int i=0; i<code.Length; i++) sum += getLetterValue(code[i]); return (sum % 43); } /// <summary> /// Generuj Code 39 Mod 43 /// </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 char[] bars = generateBars(code); int pos = 10; int width = 0; for (int i = 0; i < bars.Length; i++) { //Sprawdz czy teraz bedzie pasek szeroki czy waski if (bars[i] == 'N') { width = 2; } else { width = 4; } //Sprawdz czy pasek bialy czy czarny (rysujemy tylko czarne, bo tlo jest biale) if ((i % 2) == 0) { graphics.FillRectangle(blackBrush, pos, 10, width, 90); } pos += width; } //Wypisz kodowany ciag pod kodem kreskowym for (int i = 0; i < code.Length; i++) { graphics.DrawString(code.Substring(i, 1), drawFont, blackBrush, i * 26 + 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 - W pasek szeroki, N - pasek waski</returns> private char[] generateBars(string code) { char[] bars = new char[10 * (code.Length+3)]; //Dodaj znak start/stop for (int j = 0; j < 9; j++) { bars[j] = start_stop[j]; } bars[9] = 'N'; //Dodaj odstep //Dodaj kolejne znaki for (int i = 0; i < code.Length; i++) { int letterValue = getLetterValue(code[i]); for (int j = 0; j < 9; j++) { bars[i * 10 + 10 + j] = code39bars[letterValue, j]; } bars[i * 10 + 19] = 'N'; //Dodaj odstep } //Dodaj cyfre kontrolna int checkDigitValue = checkDigit(code); for (int j = 0; j < 9; j++) { bars[code.Length * 10 + 10 + j] = code39bars[checkDigitValue, j]; } bars[code.Length * 10 + 10 + 9] = 'N'; //Dodaj odstep //Dodaj znak start/stop for (int j = 0; j < 9; j++) { bars[code.Length * 10 + 20 + j] = start_stop[j]; } bars[code.Length * 10 + 20 + 9] = 'N'; //Dodaj odstep return bars; } } }