Практическое занятие № 19
Тема: Создание штрихкода в WPF
Цель работы: приобретение навыков работы в создании штрихкода в WPF.
Приобретаемые умения и навыки: умение создавать штрихкод в WPF.
Норма времени: 2 часа.
Оборудование: Компьютер с установленным программным обеспечением и подключенный к Internet.
Методические указания по выполнению практической работы
Чтобы создать штрихкод формата EAN-13 в WPF, нужно сначала объявить словари с шириной черных и белых штрихов. Это нужно, потому что от ширины штрихов зависит то, какая цифра закодирована. А так же объявить словарь с первой группой цифр в штрихкоде.
Dictionary dictA = new Dictionary(10);
Dictionary dictB = new Dictionary(10);
Dictionary dictC = new Dictionary(10);
Dictionary numGroup = new Dictionary(10);
Листинг 1 – Объявление словарей
Следующим этапом будет написание метода с заполнением словарей значениями.
public void FillDict()
{
dictA.Add(0, new List { 3, 2, 1, 1 });
dictA.Add(1, new List { 2, 2, 2, 1 });
dictA.Add(2, new List { 2, 1, 2, 2 });
dictA.Add(3, new List { 1, 4, 1, 1 });
dictA.Add(4, new List { 1, 1, 3, 2 });
dictA.Add(5, new List { 1, 2, 3, 1 });
dictA.Add(6, new List { 1, 1, 1, 4 });
dictA.Add(7, new List { 1, 3, 1, 2 });
dictA.Add(8, new List { 1, 2, 1, 3 });
dictA.Add(9, new List { 3, 1, 1, 2 });
dictB.Add(0, new List { 1, 1, 2, 3 });
dictB.Add(1, new List { 1, 2, 2, 2 });
dictB.Add(2, new List { 2, 2, 1, 2 });
dictB.Add(3, new List { 1, 1, 4, 1 });
dictB.Add(4, new List { 2, 3, 1, 1 });
dictB.Add(5, new List { 1, 3, 2, 1 });
dictB.Add(6, new List { 4, 1, 1, 1 });
dictB.Add(7, new List { 2, 1, 3, 1 });
dictB.Add(8, new List { 3, 1, 2, 1 });
dictB.Add(9, new List { 2, 1, 1, 3 });
dictC.Add(0, new List { 3, 2, 1, 1 });
dictC.Add(1, new List { 2, 2, 2, 1 });
dictC.Add(2, new List { 2, 1, 2, 2 });
dictC.Add(3, new List { 1, 4, 1, 1 });
dictC.Add(4, new List { 1, 1, 3, 2 });
dictC.Add(5, new List { 1, 2, 3, 1 });
dictC.Add(6, new List { 1, 1, 1, 4 });
dictC.Add(7, new List { 1, 3, 1, 2 });
dictC.Add(8, new List { 1, 2, 1, 3 });
dictC.Add(9, new List { 3, 1, 1, 2 });
numGroup.Add(0, new List { "A", "A", "A", "A", "A", "A"});
numGroup.Add(1, new List { "A", "A", "B", "A", "B", "B" });
numGroup.Add(2, new List { "A", "A", "B", "B", "A", "B" });
numGroup.Add(3, new List { "A", "A", "B", "B", "B", "A" });
numGroup.Add(4, new List { "A", "B", "A", "A", "B", "B" });
numGroup.Add(5, new List { "A", "B", "B", "A", "A", "B" });
numGroup.Add(6, new List { "A", "B", "B", "B", "A", "A" });
numGroup.Add(7, new List { "A", "B", "A", "B", "A", "B" });
numGroup.Add(8, new List { "A", "B", "A", "B", "B", "A" });
numGroup.Add(9, new List { "A", "B", "B", "A", "B", "A" });
}
Листинг 2 – Заполнение словарей
Теперь напишем метод для заполнения штрихкода числами по умолчанию.
public void Load(string num)
{
barcodeCan.Children.RemoveRange(0, barcodeCan.Children.Count-1);
char[] nums = num.Replace(" ", "").Replace(",", "").ToArray();
List numGroupNum = numGroup[Convert.ToInt32(nums[0])-48];
Rectangle rect = new Rectangle()
{
Height = 222.0,
Width = 4.0,
Margin = new Thickness(30, 10, 0, 0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
Rectangle rect2 = new Rectangle()
{
Height = 222.0,
Width = 2.0,
Margin = new Thickness(33, 10, 0, 0),
Fill = System.Windows.Media.Brushes.White,
SnapsToDevicePixels = true
};
Rectangle rect1_2 = new Rectangle()
{
Height = 222.0,
Width = 4.0,
Margin = new Thickness(36, 10, 0, 0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
Label lb1 = new Label()
{
Content = nums[0],
FontSize = 24,
FontWeight = FontWeights.Bold,
Margin = new Thickness(8, 207, 0, 0),
};
barcodeCan.Children.Add(lb1);
barcodeCan.Children.Add(rect);
barcodeCan.Children.Add(rect2);
barcodeCan.Children.Add(rect1_2);
Rectangle localRec = rect1_2;
for (int i = 0; i
{
Rectangle rec1 = new Rectangle()
{
Height = 207.0,
Width = (numGroupNum[i] == "A") ? 3.0 * dictA[Convert.ToInt32(nums[i + 1]) - 48][0] : 3.0 * dictB[Convert.ToInt32(nums[i + 1]) - 48][0],
Margin = new Thickness(localRec.Margin.Left + localRec.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.White,
SnapsToDevicePixels = true
};
Rectangle rec2 = new Rectangle()
{
Height = 207.0,
Width = (numGroupNum[i] == "A") ? 3.0 * dictA[Convert.ToInt32(nums[i + 1]) - 48][1] : 3.0 * dictB[Convert.ToInt32(nums[i + 1]) - 48][1],
Margin = new Thickness(rec1.Margin.Left + rec1.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
Rectangle rec3 = new Rectangle()
{
Height = 207.0,
Width = (numGroupNum[i] == "A") ? 3.0 * dictA[Convert.ToInt32(nums[i + 1]) - 48][2] : 3.0 * dictB[Convert.ToInt32(nums[i + 1]) - 48][2],
Margin = new Thickness(rec2.Margin.Left + rec2.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.White,
SnapsToDevicePixels = true
};
Rectangle rec4 = new Rectangle()
{
Height = 207.0,
Width = (numGroupNum[i] == "A") ? 3.0 * dictA[Convert.ToInt32(nums[i + 1]) - 48][3] : 3.0 * dictB[Convert.ToInt32(nums[i + 1]) - 48][3],
Margin = new Thickness(rec3.Margin.Left + rec3.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
Label lbl = new Label()
{
Content = nums[i+1],
FontSize = 24,
FontWeight = FontWeights.Bold,
Margin = new Thickness(rec1.Margin.Left, 207, 0, 0),
};
localRec = rec4;
barcodeCan.Children.Add(lbl);
barcodeCan.Children.Add(rec1);
barcodeCan.Children.Add(rec2);
barcodeCan.Children.Add(rec3);
barcodeCan.Children.Add(rec4);
}
Rectangle rect3 = copyRec(localRec);
rect3.Margin = new Thickness(rect3.Margin.Left + 6.0, 10, 0, 0);
rect3.Width = 4.0;
rect3.Height = 222.0;
Rectangle rect4 = copyRec(rect3);
rect4.Margin = new Thickness(rect4.Margin.Left + 3.0, 10, 0, 0);
rect4.Width = 4.0;
barcodeCan.Children.Add(rect3);
barcodeCan.Children.Add(rect4);
localRec = copyRec(rect4);
for (int i = 7; i
{
Rectangle rec1 = new Rectangle()
{
Height = 207.0,
Width = 3.0 * dictC[Convert.ToInt32(nums[i]) - 48][0],
Margin = new Thickness(localRec.Margin.Left + localRec.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
Rectangle rec2 = new Rectangle()
{
Height = 207.0,
Width = 3.0 * dictC[Convert.ToInt32(nums[i]) - 48][1],
Margin = new Thickness(rec1.Margin.Left + rec1.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.White,
SnapsToDevicePixels = true
};
Rectangle rec3 = new Rectangle()
{
Height = 207.0,
Width = 3.0 * dictC[Convert.ToInt32(nums[i]) - 48][2],
Margin = new Thickness(rec2.Margin.Left + rec2.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
Rectangle rec4 = new Rectangle()
{
Height = 207.0,
Width = 3.0 * dictC[Convert.ToInt32(nums[i]) - 48][3],
Margin = new Thickness(rec3.Margin.Left + rec3.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.White,
SnapsToDevicePixels = true
};
Label lbl = new Label()
{
Content = nums[i],
FontSize = 24,
FontWeight = FontWeights.Bold,
Margin = new Thickness(rec1.Margin.Left, 207, 0, 0),
};
localRec = rec4;
barcodeCan.Children.Add(lbl);
barcodeCan.Children.Add(rec1);
barcodeCan.Children.Add(rec2);
barcodeCan.Children.Add(rec3);
barcodeCan.Children.Add(rec4);
}
Rectangle rect5 = copyRec(localRec);
rect5.Width = 4.0;
rect5.Height = 222.0;
Rectangle rect6 = copyRec(rect5);
rect6.Margin = new Thickness(rect6.Margin.Left + 3.0, 10, 0, 0);
rect6.Width = 4.0;
barcodeCan.Children.Add(rect5);
barcodeCan.Children.Add(rect6);
}
Листинг 3 – Заполнение штрихкода по умолчанию
Также нам нужно написать метод для копирования значений другого прямоугольника, который является одним их штрихов.
private Rectangle copyRec(Rectangle rec)
{
return new Rectangle() {
Height = Math.Floor(rec.Height),
Width = Math.Floor(rec.Width),
Margin = new Thickness(rec.Margin.Left + rec.Width, 10,0,0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
}
Листинг 4 – Метод для копирования значений
Код программы целиком представлен ниже
public partial class MainWindow : Window
{
Dictionary dictA = new Dictionary(10);
Dictionary dictB = new Dictionary(10);
Dictionary dictC = new Dictionary(10);
Dictionary numGroup = new Dictionary(10);
barcodeDBEntities db = new barcodeDBEntities();
public MainWindow()
{
InitializeComponent();
FillDict();
Load("0000000000000");
}
private Rectangle copyRec(Rectangle rec)
{
return new Rectangle() {
Height = Math.Floor(rec.Height),
Width = Math.Floor(rec.Width),
Margin = new Thickness(rec.Margin.Left + rec.Width, 10,0,0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
}
public void FillDict()
{
dictA.Add(0, new List { 3, 2, 1, 1 });
dictA.Add(1, new List { 2, 2, 2, 1 });
dictA.Add(2, new List { 2, 1, 2, 2 });
dictA.Add(3, new List { 1, 4, 1, 1 });
dictA.Add(4, new List { 1, 1, 3, 2 });
dictA.Add(5, new List { 1, 2, 3, 1 });
dictA.Add(6, new List { 1, 1, 1, 4 });
dictA.Add(7, new List { 1, 3, 1, 2 });
dictA.Add(8, new List { 1, 2, 1, 3 });
dictA.Add(9, new List { 3, 1, 1, 2 });
dictB.Add(0, new List { 1, 1, 2, 3 });
dictB.Add(1, new List { 1, 2, 2, 2 });
dictB.Add(2, new List { 2, 2, 1, 2 });
dictB.Add(3, new List { 1, 1, 4, 1 });
dictB.Add(4, new List { 2, 3, 1, 1 });
dictB.Add(5, new List { 1, 3, 2, 1 });
dictB.Add(6, new List { 4, 1, 1, 1 });
dictB.Add(7, new List { 2, 1, 3, 1 });
dictB.Add(8, new List { 3, 1, 2, 1 });
dictB.Add(9, new List { 2, 1, 1, 3 });
dictC.Add(0, new List { 3, 2, 1, 1 });
dictC.Add(1, new List { 2, 2, 2, 1 });
dictC.Add(2, new List { 2, 1, 2, 2 });
dictC.Add(3, new List { 1, 4, 1, 1 });
dictC.Add(4, new List { 1, 1, 3, 2 });
dictC.Add(5, new List { 1, 2, 3, 1 });
dictC.Add(6, new List { 1, 1, 1, 4 });
dictC.Add(7, new List { 1, 3, 1, 2 });
dictC.Add(8, new List { 1, 2, 1, 3 });
dictC.Add(9, new List { 3, 1, 1, 2 });
numGroup.Add(0, new List { "A", "A", "A", "A", "A", "A"});
numGroup.Add(1, new List { "A", "A", "B", "A", "B", "B" });
numGroup.Add(2, new List { "A", "A", "B", "B", "A", "B" });
numGroup.Add(3, new List { "A", "A", "B", "B", "B", "A" });
numGroup.Add(4, new List { "A", "B", "A", "A", "B", "B" });
numGroup.Add(5, new List { "A", "B", "B", "A", "A", "B" });
numGroup.Add(6, new List { "A", "B", "B", "B", "A", "A" });
numGroup.Add(7, new List { "A", "B", "A", "B", "A", "B" });
numGroup.Add(8, new List { "A", "B", "A", "B", "B", "A" });
numGroup.Add(9, new List { "A", "B", "B", "A", "B", "A" });
}
public void Load(string num)
{
barcodeCan.Children.RemoveRange(0, barcodeCan.Children.Count-1);
char[] nums = num.Replace(" ", "").Replace(",", "").ToArray();
List numGroupNum = numGroup[Convert.ToInt32(nums[0])-48];
Rectangle rect = new Rectangle()
{
Height = 222.0,
Width = 4.0,
Margin = new Thickness(30, 10, 0, 0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
Rectangle rect2 = new Rectangle()
{
Height = 222.0,
Width = 2.0,
Margin = new Thickness(33, 10, 0, 0),
Fill = System.Windows.Media.Brushes.White,
SnapsToDevicePixels = true
};
Rectangle rect1_2 = new Rectangle()
{
Height = 222.0,
Width = 4.0,
Margin = new Thickness(36, 10, 0, 0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
Label lb1 = new Label()
{
Content = nums[0],
FontSize = 24,
FontWeight = FontWeights.Bold,
Margin = new Thickness(8, 207, 0, 0),
};
barcodeCan.Children.Add(lb1);
barcodeCan.Children.Add(rect);
barcodeCan.Children.Add(rect2);
barcodeCan.Children.Add(rect1_2);
Rectangle localRec = rect1_2;
for (int i = 0; i
{
Rectangle rec1 = new Rectangle()
{
Height = 207.0,
Width = (numGroupNum[i] == "A") ? 3.0 * dictA[Convert.ToInt32(nums[i + 1]) - 48][0] : 3.0 * dictB[Convert.ToInt32(nums[i + 1]) - 48][0],
Margin = new Thickness(localRec.Margin.Left + localRec.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.White,
SnapsToDevicePixels = true
};
Rectangle rec2 = new Rectangle()
{
Height = 207.0,
Width = (numGroupNum[i] == "A") ? 3.0 * dictA[Convert.ToInt32(nums[i + 1]) - 48][1] : 3.0 * dictB[Convert.ToInt32(nums[i + 1]) - 48][1],
Margin = new Thickness(rec1.Margin.Left + rec1.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
Rectangle rec3 = new Rectangle()
{
Height = 207.0,
Width = (numGroupNum[i] == "A") ? 3.0 * dictA[Convert.ToInt32(nums[i + 1]) - 48][2] : 3.0 * dictB[Convert.ToInt32(nums[i + 1]) - 48][2],
Margin = new Thickness(rec2.Margin.Left + rec2.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.White,
SnapsToDevicePixels = true
};
Rectangle rec4 = new Rectangle()
{
Height = 207.0,
Width = (numGroupNum[i] == "A") ? 3.0 * dictA[Convert.ToInt32(nums[i + 1]) - 48][3] : 3.0 * dictB[Convert.ToInt32(nums[i + 1]) - 48][3],
Margin = new Thickness(rec3.Margin.Left + rec3.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
Label lbl = new Label()
{
Content = nums[i+1],
FontSize = 24,
FontWeight = FontWeights.Bold,
Margin = new Thickness(rec1.Margin.Left, 207, 0, 0),
};
localRec = rec4;
barcodeCan.Children.Add(lbl);
barcodeCan.Children.Add(rec1);
barcodeCan.Children.Add(rec2);
barcodeCan.Children.Add(rec3);
barcodeCan.Children.Add(rec4);
}
Rectangle rect3 = copyRec(localRec);
rect3.Margin = new Thickness(rect3.Margin.Left + 6.0, 10, 0, 0);
rect3.Width = 4.0;
rect3.Height = 222.0;
Rectangle rect4 = copyRec(rect3);
rect4.Margin = new Thickness(rect4.Margin.Left + 3.0, 10, 0, 0);
rect4.Width = 4.0;
barcodeCan.Children.Add(rect3);
barcodeCan.Children.Add(rect4);
localRec = copyRec(rect4);
for (int i = 7; i
{
Rectangle rec1 = new Rectangle()
{
Height = 207.0,
Width = 3.0 * dictC[Convert.ToInt32(nums[i]) - 48][0],
Margin = new Thickness(localRec.Margin.Left + localRec.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
Rectangle rec2 = new Rectangle()
{
Height = 207.0,
Width = 3.0 * dictC[Convert.ToInt32(nums[i]) - 48][1],
Margin = new Thickness(rec1.Margin.Left + rec1.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.White,
SnapsToDevicePixels = true
};
Rectangle rec3 = new Rectangle()
{
Height = 207.0,
Width = 3.0 * dictC[Convert.ToInt32(nums[i]) - 48][2],
Margin = new Thickness(rec2.Margin.Left + rec2.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.Black,
SnapsToDevicePixels = true
};
Rectangle rec4 = new Rectangle()
{
Height = 207.0,
Width = 3.0 * dictC[Convert.ToInt32(nums[i]) - 48][3],
Margin = new Thickness(rec3.Margin.Left + rec3.Width, 10, 0, 0),
Fill = System.Windows.Media.Brushes.White,
SnapsToDevicePixels = true
};
Label lbl = new Label()
{
Content = nums[i],
FontSize = 24,
FontWeight = FontWeights.Bold,
Margin = new Thickness(rec1.Margin.Left, 207, 0, 0),
};
localRec = rec4;
barcodeCan.Children.Add(lbl);
barcodeCan.Children.Add(rec1);
barcodeCan.Children.Add(rec2);
barcodeCan.Children.Add(rec3);
barcodeCan.Children.Add(rec4);
}
Rectangle rect5 = copyRec(localRec);
rect5.Width = 4.0;
rect5.Height = 222.0;
Rectangle rect6 = copyRec(rect5);
rect6.Margin = new Thickness(rect6.Margin.Left + 3.0, 10, 0, 0);
rect6.Width = 4.0;
barcodeCan.Children.Add(rect5);
barcodeCan.Children.Add(rect6);
}
private void TxtBox_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
Load(txtBox.Text);
}
catch
{
}
}
Листинг 5 – Код программы целиком
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BarcodeWPF"
mc:Ignorable="d"
Title="EAN-13" Height="500" Width="400" WindowStartupLocation="CenterScreen" ResizeMode="NoResize"
Листинг 6 – Код разметки
Рис. 1 – Программа
Контрольные вопросы:
Что такое штрихкод?
Для чего используются штрихкоды?