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.
UPC-A/Form1.cs:
//Kod kreskowy UPC-A
//(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.UPC_A
{
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 UPC-A
/// </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 UPC-A!", "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 <= 10) ||
(i >= 45 && i <= 49) ||
(i >= 85 && i <= 94))
{
//Paski ochronne, pierwsza i ostatnia cyfra kodowana sa dluzsze
length = 100;
}
else
{
//Paski kodujace pozostale cyfry sa krotsze
length = 90;
}
if (bars[i] == 1)
{
graphics.FillRectangle(blackBrush, i * 2 + 30, 10, 2, length);
}
}
//Wypisz cyfry pod kodem
graphics.DrawString(code.Substring(0, 1), drawFont, blackBrush, 18, 90);
for (int i = 1; i < 6; i++)
{
graphics.DrawString(code.Substring(i, 1), drawFont, blackBrush, i * 14 + 37, 102);
}
for (int i = 6; i < 11; i++)
{
graphics.DrawString(code.Substring(i, 1), drawFont, blackBrush, i * 14 + 47, 102);
}
graphics.DrawString(code.Substring(11, 1), drawFont, blackBrush, 220, 90);
//Odswiez
pictureBox.Refresh();
//Zwolinij zasoby
whiteBrush.Dispose();
blackBrush.Dispose();
graphics.Dispose();
}
/// <summary>
/// Sprawdz UPC-A
/// </summary>
/// <param name="code"></param>
/// <returns>true jezeli prawidlowy, false w przeciwnym razie</returns>
private bool checkCode(string code)
{
//Sprawdz dlugosc
if (code.Length != 12)
{
return false;
}
//Sprawdz czy podano tylko cyfry
int[] values = new int[12];
for (int i = 0; i < 12; 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] +
1 * values[7] +
3 * values[8] +
1 * values[9] +
3 * values[10];
sum %= 10;
sum = 10 - sum;
sum %= 10;
if (sum != values[11])
{
return false;
}
return true;
}
/// <summary>
/// Generuje kod kreskowy
/// </summary>
/// <param name="code">kod UPC-A</param>
/// <returns>kod kreskowy - 1 pasek czarny, 0 - pasek bialy</returns>
private int[] generateBars(string code)
{
int[] bars = new int[95];
int[] UPCA = new int[12];
for (int i = 0; i < 12; i++)
{
int.TryParse(code.Substring(i, 1), out UPCA[i]);
}
bars[0] = 1;
bars[1] = 0;
bars[2] = 1;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
bars[i * 7 + 3 + j] = left[UPCA[i], j];
}
}
bars[45] = 0;
bars[46] = 1;
bars[47] = 0;
bars[48] = 1;
bars[49] = 0;
for (int i = 6; i < 12; i++)
{
for (int j = 0; j < 7; j++)
{
bars[i * 7 + 8 + j] = right[UPCA[i], j];
}
}
bars[92] = 1;
bars[93] = 0;
bars[94] = 1;
return bars;
}
}
}

