Recently looking at some barcode related information, and if just look, the effect does not seem very good, so decided to do some demo, to enhance the memory of the relevant knowledge.
Here's an example I've written using WPF to generate Code 39, and Code 39 is very simple to encode, so it's the first time you try it first.
Standard Code 39 supports only 43 characters, 0~9,a~z,-,.,$,/, +,%, and spaces. In addition, * for starting and terminating symbols. By using an extension of two encodings, all acsii code characters can be supported. Relevant knowledge can be found on Wikipedia.
Because the Wpf,demo is divided into two files, the XAML file contains code related to the interface layout:
<window x:class= "Code39demo.mainwindow"
xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "
title=" MainWindow "height=" "width=" 525 " loaded= "window_loaded" >
<Grid>
<Grid.RowDefinitions>
<rowdefinition></ rowdefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
< TextBox name= "Textbox1" verticalcontentalignment= "Center" textchanged= "textbox1_textchanged" ></TextBox>
<canvas name= "CANVAS1" grid.row= "1" ></Canvas>
</Grid>
</Window>
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/
Using System.Collections.Generic;
Using System.Text;
Using System.Text.RegularExpressions;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Media;
Using System.Windows.Shapes;
namespace Code39demo {///<summary>///interaction logic for MainWindow.xaml </summary> public partial class Mainwindow:window {public Main
Window () {InitializeComponent ();
} private void Window_Loaded (object sender, RoutedEventArgs e) {
Textbox1.focus ();
} private void textBox1_TextChanged (object sender, Textchangedeventargs e) {
Canvas1.Children.Clear (); TextBox textbox = E.source asTextBox;
if (TextBox!= null) {string content = TextBox.Text; if (string.
IsNullOrEmpty (content)) {Textbox.borderbrush = null;
Return String code = String.
Empty;
BOOL result = Tryconvertcode (content, out code);
if (!result) {Textbox.borderbrush = new SolidColorBrush (colors.red);
textbox.borderthickness = new Thickness (2);
Return
else {textbox.borderbrush = null;
int thinwidth = 3;
int thickwidth = 3 * thinwidth; int currentpos = 10; for (int i = 0; i < code. Length;
i++) {Rectangle r = new Rectangle ();
R.height = 150;
Canvas.setleft (R, Currentpos);
Switch (Code[i]) {case ' N ':
{R.fill = new SolidColorBrush (colors.black);
Currentpos + = Thinwidth;
R.width = Thinwidth;
Break
Case ' n ': {
R.fill = new SolidColorBrush (colors.white); Currentpos+ + thinwidth;
R.width = Thinwidth;
Break
Case ' W ': {
R.fill = new SolidColorBrush (colors.black);
Currentpos + = Thickwidth;
R.width = Thickwidth;
Break
Case ' W ': {
R.fill = new SolidColorBrush (colors.white);
Currentpos + = Thickwidth;
R.width = Thickwidth;
Break }} Canvas1.childrEn.
ADD (R); }} private bool Tryconvertcode (string content, out St Ring code) {Code = string.
Empty;
String c = "*" + content + "*";
StringBuilder sb = new StringBuilder (); for (int i = 0; i < c.length; i++) {String value = String.
Empty;
BOOL result = _code39map.trygetvalue (C[i], out value);
if (!result) {return false; } sb.
Append (value); Sb.
Append (' n '); The code = SB. Remove (sb.) Length-1, 1).
ToString ();
return true; } Private StAtic Dictionary<char, string> _code39map = new Dictionary<char, string> ();
Static MainWindow () {_code39map[' 0 '] = "NNNWWNWNN";
_code39map[' 1 '] = "WNNWNNNNW";
_code39map[' 2 '] = "NNWWNNNNW";
_code39map[' 3 '] = "wnwwnnnnn";
_code39map[' 4 '] = "NNNWWNNNW";
_code39map[' 5 '] = "wnnwwnnnn";
_code39map[' 6 '] = "nnwwwnnnn";
_code39map[' 7 '] = "NNNWNNWNW";
_code39map[' 8 '] = "WNNWNNWNN";
_code39map[' 9 '] = "NNWWNNWNN";
_code39map[' A '] = "WNNNNWNNW";
_code39map[' B '] = "NNWNNWNNW";
_code39map[' C '] = "wnwnnwnnn";
_code39map[' D '] = "NNNNWWNNW";
_code39map[' E '] = "wnnnwwnnn"; _code39map[' F '] = "nnwnwwnnn";
_code39map[' G '] = "NNNNNWWNW";
_code39map[' H '] = "WNNNNWWNN";
_code39map[' I '] = "NNWNNWWNN";
_code39map[' J '] = "NNNNWWWNN";
_code39map[' K '] = "WNNNNNNWW";
_code39map[' L '] = "NNWNNNNWW";
_code39map[' M '] = "WNWNNNNWN";
_code39map[' N '] = "NNNNWNNWW";
_code39map[' O '] = "WNNNWNNWN";
_code39map[' P '] = "NNWNWNNWN";
_code39map[' Q '] = "nnnnnnwww";
_code39map[' R '] = "WNNNNNWWN";
_code39map[' S '] = "NNWNNNWWN";
_code39map[' T '] = "NNNNWNWWN";
_code39map[' U '] = "WWNNNNNNW";
_code39map[' V '] = "NWWNNNNNW";
_code39map[' W '] = "wwwnnnnnn";
_code39map[' X '] = "NWNNWNNNW";
_code39map[' Y '] = "wwnnwnnnn"; _code39map[' Z '] = "nwwnwnnnn";
_code39map['-'] = "NWNNNNWNW";
_code39map['. '] = "NWNNNNWNW";
_code39map['] = "NWWNNNWNN";
_code39map[' $ '] = "nwnwnwnnn";
_code39map['/'] = "NWNWNNNWN";
_code39map[' + '] = "NWNNNWNWN";
_code39map['% '] = "NNNWNWNWN";
_code39map[' * '] = "NWNNWNWNN"; }
}
}
For each character corresponding to the code, W is the black width bar, W is the white width bar, n is the black narrow bar, n is the white narrow bar. Note that a white narrow bar is needed between the adjacent characters of Code 39 for separating.
This demo also lacks code 39 commander of the Code to deal with (usually do not need), if necessary, can be supplemented on this basis, I believe it is not difficult work.