Nadesłany przez Tomasz Lubiński, 30 października 2019 21:36
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.
EAN8/Form1.cs:
//Kod kreskowy EAN-8
//(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.EAN8
{
public partial class Form1 : Form
{
private readonly int[,] right = {
{1, 1, 1, 0, 0, 1, 0},
{1, 1, 0, 0, 1, 1, 0},
{1, 1, 0, 1, 1, 0, 0},
{1, 0, 0, 0, 0, 1, 0},
{1, 0, 1, 1, 1, 0, 0},
{1, 0, 0, 1, 1, 1, 0},
{1, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0, 0},
{1, 1, 1, 0, 1, 0, 0}
};
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}
};
/// <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-8
/// </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-8!", "Bład", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//Wygeneruj i narysuj kod
int[] bars = generateBars(code);
int length;
for (int i = 0; i < bars.Length; i++)
{
if ((i >= 0 && i <= 2) ||
(i >= 31 && i <= 35) ||
(i >= 64 && i <= 66))
{
//Paski ochronne sa dluzsze
length = 100;
}
else
{
//Paski kodujace cyfry sa krotsze
length = 90;
}
if (bars[i] == 1)
{
graphics.FillRectangle(blackBrush, i * 2 + 40, 10, 2, length);
}
}
//Wypisz cyfry pod kodem
for (int i = 0; i < 4; i++)
{
graphics.DrawString(code.Substring(i, 1), drawFont, blackBrush, i * 14 + 47, 102);
}
for (int i = 4; i < 8; i++)
{
graphics.DrawString(code.Substring(i, 1), drawFont, blackBrush, i * 14 + 57, 102);
}
//Odswiez
pictureBox.Refresh();
//Zwolinij zasoby
whiteBrush.Dispose();
blackBrush.Dispose();
graphics.Dispose();
}
/// <summary>
/// Sprawdz EAN-8
/// </summary>
/// <param name="code"></param>
/// <returns>true jezeli prawidlowy, false w przeciwnym razie</returns>
private bool checkCode(string code)
{
//Sprawdz dlugosc
if (code.Length != 8)
{
return false;
}
//Sprawdz czy podano tylko cyfry
int[] values = new int[8];
for (int i = 0; i < 8; i++)
{
if (int.TryParse(code.Substring(i, 1), out values[i]) == false)
return false;
}
//Sprawdz sume kontrolna
int sum = 3 * values[0] +
1 * values[1] +
3 * values[2] +
1 * values[3] +
3 * values[4] +
1 * values[5] +
3 * values[6];
sum %= 10;
sum = 10 - sum;
sum %= 10;
if (sum != values[7])
{
return false;
}
return true;
}
/// <summary>
/// Generuje kod kreskowy
/// </summary>
/// <param name="code">kod EAN-8</param>
/// <returns>kod kreskowy - 1 pasek czarny, 0 - pasek bialy</returns>
private int[] generateBars(string code)
{
int[] bars = new int[67];
int[] EAN8 = new int[8];
for (int i = 0; i < 8; i++)
{
int.TryParse(code.Substring(i, 1), out EAN8[i]);
}
bars[0] = 1;
bars[1] = 0;
bars[2] = 1;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 7; j++)
{
bars[i * 7 + 3 + j] = left[EAN8[i], j];
}
}
bars[31] = 0;
bars[32] = 1;
bars[33] = 0;
bars[34] = 1;
bars[35] = 0;
for (int i = 4; i < 8; i++)
{
for (int j = 0; j < 7; j++)
{
bars[(i - 4) * 7 + 36 + j] = right[EAN8[i], j];
}
}
bars[64] = 1;
bars[65] = 0;
bars[66] = 1;
return bars;
}
}
}

