Nadesłany przez Tomasz Lubiński, 30 października 2019 21:39
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.
Code93Ext/Form1.cs:
//Kod kreskowy Extended Code 93 //(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.Code93Ext { public partial class Form1 : Form { private readonly static char special_1 = '\u0080'; // ($) private readonly static char special_2 = '\u0081'; // (%) private readonly static char special_3 = '\u0082'; // (/) private readonly static char special_4 = '\u0083'; // (+) private readonly static char[] code93signs = { '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', '-', '.', ' ', '$', '/', '+', '%', special_1, special_2, special_3, special_4 }; private readonly static int[,] code93bars = { {1, 0, 0, 0, 1, 0, 1, 0, 0}, {1, 0, 1, 0, 0, 1, 0, 0, 0}, {1, 0, 1, 0, 0, 0, 1, 0, 0}, {1, 0, 1, 0, 0, 0, 0, 1, 0}, {1, 0, 0, 1, 0, 1, 0, 0, 0}, {1, 0, 0, 1, 0, 0, 1, 0, 0}, {1, 0, 0, 1, 0, 0, 0, 1, 0}, {1, 0, 1, 0, 1, 0, 0, 0, 0}, {1, 0, 0, 0, 1, 0, 0, 1, 0}, {1, 0, 0, 0, 0, 1, 0, 1, 0}, {1, 1, 0, 1, 0, 1, 0, 0, 0}, {1, 1, 0, 1, 0, 0, 1, 0, 0}, {1, 1, 0, 1, 0, 0, 0, 1, 0}, {1, 1, 0, 0, 1, 0, 1, 0, 0}, {1, 1, 0, 0, 1, 0, 0, 1, 0}, {1, 1, 0, 0, 0, 1, 0, 1, 0}, {1, 0, 1, 1, 0, 1, 0, 0, 0}, {1, 0, 1, 1, 0, 0, 1, 0, 0}, {1, 0, 1, 1, 0, 0, 0, 1, 0}, {1, 0, 0, 1, 1, 0, 1, 0, 0}, {1, 0, 0, 0, 1, 1, 0, 1, 0}, {1, 0, 1, 0, 1, 1, 0, 0, 0}, {1, 0, 1, 0, 0, 1, 1, 0, 0}, {1, 0, 1, 0, 0, 0, 1, 1, 0}, {1, 0, 0, 1, 0, 1, 1, 0, 0}, {1, 0, 0, 0, 1, 0, 1, 1, 0}, {1, 1, 0, 1, 1, 0, 1, 0, 0}, {1, 1, 0, 1, 1, 0, 0, 1, 0}, {1, 1, 0, 1, 0, 1, 1, 0, 0}, {1, 1, 0, 1, 0, 0, 1, 1, 0}, {1, 1, 0, 0, 1, 0, 1, 1, 0}, {1, 1, 0, 0, 1, 1, 0, 1, 0}, {1, 0, 1, 1, 0, 1, 1, 0, 0}, {1, 0, 1, 1, 0, 0, 1, 1, 0}, {1, 0, 0, 1, 1, 0, 1, 1, 0}, {1, 0, 0, 1, 1, 1, 0, 1, 0}, {1, 0, 0, 1, 0, 1, 1, 1, 0}, {1, 1, 1, 0, 1, 0, 1, 0, 0}, {1, 1, 1, 0, 1, 0, 0, 1, 0}, {1, 1, 1, 0, 0, 1, 0, 1, 0}, {1, 0, 1, 1, 0, 1, 1, 1, 0}, {1, 0, 1, 1, 1, 0, 1, 1, 0}, {1, 1, 0, 1, 0, 1, 1, 1, 0}, {1, 0, 0, 1, 0, 0, 1, 1, 0}, {1, 1, 1, 0, 1, 1, 0, 1, 0}, {1, 1, 1, 0, 1, 0, 1, 1, 0}, {1, 0, 0, 1, 1, 0, 0, 1, 0} }; private readonly static int[] start_stop = { 1, 0, 1, 0, 1, 1, 1, 1, 0 }; private readonly static string[] extendedCode93Replace = { "\u0081U", "\u0080A", "\u0080B", "\u0080C", "\u0080D", "\u0080E", "\u0080F", "\u0080G", "\u0080H", "\u0080I", "\u0080J", "\u0080K", "\u0080L", "\u0080M", "\u0080N", "\u0080O", "\u0080P", "\u0080Q", "\u0080R", "\u0080S", "\u0080T", "\u0080U", "\u0080V", "\u0080W", "\u0080X", "\u0080Y", "\u0080Z", "\u0081A", "\u0081B", "\u0081C", "\u0081D", "\u0081E", " ", "\u0082A", "\u0082B", "\u0082C", "\u0082D", "\u0082E", "\u0082F", "\u0082G", "\u0082H", "\u0082I", "\u0082J", "\u0082K", "\u0082L", "-", ".", "\u0082O", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "\u0082Z", "\u0081F", "\u0081G", "\u0081H", "\u0081I", "\u0081J", "\u0081V", "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", "\u0081K", "\u0081L", "\u0081M", "\u0081N", "\u0081O", "\u0081W", "\u0083A", "\u0083B", "\u0083C", "\u0083D", "\u0083E", "\u0083F", "\u0083G", "\u0083H", "\u0083I", "\u0083J", "\u0083K", "\u0083L", "\u0083M", "\u0083N", "\u0083O", "\u0083P", "\u0083Q", "\u0083R", "\u0083S", "\u0083T", "\u0083U", "\u0083V", "\u0083W", "\u0083X", "\u0083Y", "\u0083Z", "\u0081P", "\u0081Q", "\u0081R", "\u0081S", "\u0081T" }; /// <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> /// 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 < code93signs.Length; i++) if (code93signs[i] == ch) return i; return -1; } /// <summary> /// Oblicza cyfre kontrolna C /// </summary> /// <param name="code93">Ciag do zakodowania</param> /// <returns>cyfra kontrolna C</returns> private int checkDigitC(string code93) { int sum = 0; int w = 0; for (int i=code93.Length-1; i>=0; i--) { sum += (getLetterValue(code93[i]) * ((w % 20) + 1)); w++; } return (sum % 47); } /// <summary> /// Oblicza cyfre kontrolna C /// </summary> /// <param name="code93">Ciag do zakodowania</param> /// <returns>cyfra kontrolna C</returns> private int checkDigitK(string code93) { int sum = checkDigitC(code93); int w = 1; for (int i=code93.Length-1; i>=0; i--) { sum += (getLetterValue(code93[i]) * ((w % 15) + 1)); w++; } return (sum % 47); } /// <summary> /// Konwertuje podany ciag do zbioru Code 93 /// </summary> /// <param name="code93ext">Ciag do zakodowania</param> /// <returns>ciag skonwertowany do zbioru Code 93</returns> private string convertToCode93(string code93ext) { string result = ""; for (int i=0; i<code93ext.Length; i++) { if ((code93ext[i] >= 0) && (code93ext[i] <= 127)) result += extendedCode93Replace[code93ext[i]]; else return null; } return result; } /// <summary> /// Generuj Extended Code 93 /// </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)); //Przkonwertuj dany ciag do zbioru Code 93 i sprawdz czy mozna zakodowac string code = convertToCode93(Code.Text); if (code == null) { 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, 10+2*i, 10, 2, 90); } } //Wypisz kodowany ciag pod kodem kreskowym StringFormat center = new StringFormat(); center.Alignment = StringAlignment.Center; graphics.DrawString(Code.Text, drawFont, blackBrush, bars.Length + 10, 102, center); //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(string code) { int[] bars = new int[9 * (code.Length+4) + 1]; //Dodaj znak start/stop for (int j = 0; j < 9; j++) { bars[j] = start_stop[j]; } //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 * 9 + 9 + j] = code93bars[letterValue, j]; } } //Dodaj cyfre kontrolna C int checkDigit = checkDigitC(code); for (int j = 0; j < 9; j++) { bars[(code.Length + 1) * 9 + j] = code93bars[checkDigit, j]; } //Dodaj cyfre kontrolna K checkDigit = checkDigitK(code); for (int j = 0; j < 9; j++) { bars[(code.Length + 2) * 9 + j] = code93bars[checkDigit, j]; } //Dodaj znak start/stop for (int j = 0; j < 9; j++) { bars[(code.Length + 3) * 9 + j] = start_stop[j]; } //Dodaj koncowa kreske bars[(code.Length + 4) * 9] = 1; return bars; } } }