Nadesłany przez Tomasz Lubiński, 30 października 2019 21:42
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.
MSI/Form1.cs:
//Kod kreskowy MSI //(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.MSI { public partial class Form1 : Form { private readonly static int[] mod10values = {0, 1, 4, 3, 8, 5, 3, 7, 7, 9, 2, 11, 6, 13, 10, 15}; private readonly static char[] MSIsigns = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; private readonly static int[,] MSIbars = { {1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0}, {1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0}, {1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0}, {1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0}, {1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0}, {1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0}, {1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0}, {1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0}, {1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0}, {1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0}, {1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0}, {1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0}, {1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0}, {1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0}, {1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0}, {1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0} }; private readonly static int[] start = { 1, 1, 0 }; private readonly static int[] stop = { 1, 0, 0, 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> /// Sprawdza czy podany txt mozna zakodowac MSI /// </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 MSI /// </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 < MSIsigns.Length; i++) { if (MSIsigns[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 < MSIsigns.Length; i++) if (MSIsigns[i] == ch) return i; return 0; } /// <summary> /// Zwraca sume kontrolna modulo 10 /// </summary> /// <param name="txt">kodowany ciag</param> /// <returns>suma kontrolna C</returns> private int mod10checkDigit(string code) { int sum = 0; for (int i = 0; i < code.Length; i++) { sum += mod10values[getLetterValue(code[i])]; } sum %= 10; sum = 10 - sum; sum %= 10; return sum; } /// <summary> /// Zwraca sume kontrolna modulo 11 /// </summary> /// <param name="txt">kodowany ciag</param> /// <returns>suma kontrolna C</returns> private int mod11checkDigit(string code) { int sum = 0; int w = 0; for (int i = code.Length - 1; i >= 0; i--) { sum += getLetterValue(code[i]) * (w + 2); w++; w %= 6; } sum %= 11; sum = 11 - sum; sum %= 11; return sum; } /// <summary> /// Generuj MSI /// </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 (check(code) == false) { MessageBox.Show("Nieprawidłowe znaki!", "Bład", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Wygeneruj i narysuj kod int[] bars = generateBars(ref code, checksumMethod.SelectedIndex); for (int i = 0; i < bars.Length; i++) { if (bars[i] == 1) { graphics.FillRectangle(blackBrush, i * 2 + 20, 10, 2, 90); } } //Wypisz cyfry pod kodem for (int i = 0; i < code.Length; i++) { graphics.DrawString(code.Substring(i, 1), drawFont, blackBrush, i * 24 + 30, 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, 0 - pasek bialy</returns> private int[] generateBars(ref string code, int checksumMethod) { //Sprawdz jaka cyfre kontrolna wybral uzytkownik if (checksumMethod == 0) { //Modulo 10 code += MSIsigns[mod10checkDigit(code)]; } else if (checksumMethod == 1) { //2 Modulo 10 code += MSIsigns[mod10checkDigit(code)]; code += MSIsigns[mod10checkDigit(code)]; } else if (checksumMethod == 2) { //Modulo 11 code += MSIsigns[mod11checkDigit(code)]; } else { //Modulo 11/10 code += MSIsigns[mod11checkDigit(code)]; code += MSIsigns[mod10checkDigit(code)]; } int[] bars = new int[code.Length * 12 + 7]; //Dodaj znak start for (int i = 0; i < 3; i++) { bars[i] = start[i]; } //Koduj kolejne znaki (lacznie z dodanymi sumami kontrolnymi) for (int i = 0; i < code.Length; i++) { int letterValue = getLetterValue(code[i]); for (int j = 0; j < 12; j++) { bars[i*12 + 3 + j] = MSIbars[letterValue, j]; } } //Dodaj znak stop for (int i = 0; i < 4; i++) { bars[code.Length * 12 + 3 + i] = stop[i]; } return bars; } } }