Nadesłany przez Tomasz Lubiński, 30 października 2019 21:37
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.
EAN5/Form1.cs:
//Kod kreskowy EAN-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.EAN5
{
public partial class Form1 : Form
{
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}
}
};
private readonly int [,] checksumTable =
{
{1, 1, 0, 0, 0},
{1, 0, 1, 0, 0},
{1, 0, 0, 1, 0},
{1, 0, 0, 0, 1},
{0, 1, 1, 0, 0},
{0, 0, 1, 1, 0},
{0, 0, 0, 1, 1},
{0, 1, 0, 1, 0},
{0, 1, 0, 0, 1},
{0, 0, 1, 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>
/// Generuj kod kreskowy EAN-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));
string code = Code.Text.Trim();
if (checkCode(code) == false)
{
MessageBox.Show("Nieprawidłowy kod EAN-5!", "Bład", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//Wygeneruj i narysuj kod
int[] bars = generateBars(code);
for (int i = 0; i < bars.Length; i++)
{
if (bars[i] == 1)
{
graphics.FillRectangle(blackBrush, i * 2 + 70, 40, 2, 60);
}
}
//Wypisz cyfry nad kodem
for (int i = 0; i < 5; i++)
{
graphics.DrawString(code.Substring(i, 1), drawFont, blackBrush, i * 17 + 80, 20);
}
//Odswiez
pictureBox.Refresh();
//Zwolinij zasoby
whiteBrush.Dispose();
blackBrush.Dispose();
graphics.Dispose();
}
/// <summary>
/// Sprawdz EAN-5
/// </summary>
/// <param name="code"></param>
/// <returns>true jezeli prawidlowy, false w przeciwnym razie</returns>
private bool checkCode(string code)
{
//Sprawdz dlugosc
if (code.Length != 5)
{
return false;
}
//Sprawdz czy podano tylko cyfry
int[] values = new int[5];
for (int i = 0; i < 5; i++)
{
if (int.TryParse(code.Substring(i, 1), out values[i]) == false)
return false;
}
return true;
}
/// <summary>
/// Generuje kod kreskowy
/// </summary>
/// <param name="code">kod EAN-5</param>
/// <returns>kod kreskowy - 1 pasek czarny, 0 - pasek bialy</returns>
private int[] generateBars(string code)
{
int[] bars = new int[47];
int[] EAN5 = new int[5];
for (int i = 0; i < 5; i++)
{
int.TryParse(code.Substring(i, 1), out EAN5[i]);
}
//oblicz sume kontrolna
int checkSum = 3 * EAN5[0] + 9 * EAN5[1] + 3 * EAN5[2] + 9 * EAN5[3] + 3 * EAN5[4];
checkSum %= 10;
bars[0] = 1;
bars[1] = 0;
bars[2] = 1;
bars[3] = 1;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 7; j++)
{
bars[i * 9 + 4 + j] = left[checksumTable[checkSum, i], EAN5[i], j];
}
if (i < 4)
{
bars[i * 9 + 11] = 0;
bars[i * 9 + 12] = 1;
}
}
return bars;
}
}
}

