Wpf barcode generator

What is the best method for generating barcode in different types in wpf.

+3
source share
3 answers

Barcode fonts are an easy way to do this, but they can be difficult to deploy because the client may need a font installed on every machine that runs your application. This can be impractical, especially for XBAP, Silverlight, and WinPhone7 deployments.

It is very easy to generate barcodes in WPF. Although this can be done in pure XAML, I find it simplest using a mixture of XAML and code.

Start with a converter that takes a character and returns brushes and strip width:

public class BarcodeConverter : IValueConverter
{
  public static readonly BarcodeConverter Instance = new BarcodeConverter();

  Dictionary<char, string> _codes = new Dictionary<char, string>
  {
    { '0', "nnnwwnwnn" },
    { '1', "wnnwnnnnw" },
    { '2', "nnwwnnnnw" },
    // etc
    { 'A', "wnnnnwnnw" },
    { 'B', "nnwnnwnnw" },
    // etc
  };

  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    string code;
    if(!_codes.TryGetValue((char)value, out code)) return null;

    return
      from i in Enumerable.Range(0, code.Length)
      select new
      {
        color = i%2==0 ? Brushes.Black : Brushes.Transparent,
        width = code[i]=='n' ? 5 : 10,
      };
  }
  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
    throw new NotImplementedException();
  }
}

Now XAML for building bars is trivial:

<ItemsPanelTemplate x:Key="HorizontalPanel">
  <StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>

<DataTemplate x:Key="SingleBarTemplate">
  <Rectangle Fill="{Binding color}" Width="{Binding width}" />
</DataTemplate>

<DataTemplate x:Key="SingleCodeTemplate">
  <DockPanel>
    <TextBlock DockPanel.Dock="Bottom" Text="{Binding}" />  <!-- Display character at bottom of code -->
    <ItemsControl ItemsSource="{Binding Converter={x:Static my:BarcodeConverter.Instance}}"
                  ItemsPanel="{StaticResource HorizontalPanel}"
                  ItemTemplate="{StaticResource SingleBarTemplate}" />
  </DockPanel>
</DataTemplate>

<DataTemplate x:Key="BarcodeTemplate">
  <ItemsControl ItemsSource="{Binding}"
                ItemsPanel="{StaticResource HorizontalPanel}"
                ItemTemplate="{StaticResource SingleBarTemplate}"
                Height="60" />
</DataTemplate>

BarcodeTemplate ContentPresenter , :

<ContentPresenter Content="123456789"
                  ContentTemplate="{StaticResource BarcodeTemplate}" />

<ContentPresenter Content="{Binding UPCCode}"
                  ContentTemplate="{StaticResource BarcodeTemplate}" />

, " " - WPF, :

  • ,
  • , ,
  • WPF Silverlight.
+6

, - -. :

<Window x:Class="BarcodeTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Bar Code Test" Height="209" Width="426">
    <Grid>
        <TextBlock FontFamily="IDAutomationHC39M" FontSize="30" 
                   SnapsToDevicePixels="True"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center">*0123456789*</TextBlock>
    </Grid>
</Window>

"IDAutomationHC39M". - :

alt text

- - -: http://www.adams1.com/fonts.html.

+3

:

<Window x:Class="ScratchClient.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="655.85" Width="687"
    xmlns:local="clr-namespace:ScratchClient">
<Window.Resources>
    <local:BarcodeConverter x:Key="barcodeConverter"/>
    <ItemsPanelTemplate x:Key="HorizontalPanel">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/>
    </ItemsPanelTemplate>
    <DataTemplate x:Key="SingleBarTemplate">
        <Rectangle Fill="{Binding color}" Width="{Binding width}" />
    </DataTemplate>
    <DataTemplate x:Key="SingleCodeTemplate">
        <DockPanel>
            <TextBlock DockPanel.Dock="Bottom" Text="{Binding}" />
            <ItemsControl ItemsSource="{Binding Converter={StaticResource barcodeConverter}}"
              ItemsPanel="{StaticResource HorizontalPanel}"
              ItemTemplate="{StaticResource SingleBarTemplate}" />
        </DockPanel>
    </DataTemplate>
    <DataTemplate x:Key="BarcodeTemplate">
        <ItemsControl ItemsSource="{Binding}"
            ItemsPanel="{StaticResource HorizontalPanel}"
            ItemTemplate="{StaticResource SingleCodeTemplate}"
            Height="100" />
    </DataTemplate>
</Window.Resources>
<Grid>
    <ContentPresenter Name="barcode" Content="*WIKIPEDIA*" ContentTemplate="{StaticResource BarcodeTemplate}">
        <ContentPresenter.RenderTransform>
            <ScaleTransform ScaleX="2" ScaleY="2"></ScaleTransform>
        </ContentPresenter.RenderTransform>
    </ContentPresenter>
</Grid>

public class BarcodeConverter : IValueConverter
{
    //W Wide - Black
    //N Narrow - Black
    //w Wide - White
    //n Narrow - White
    #region code details
    Dictionary<char, string> _codes = new Dictionary<char, string>
    {
        {'0',"NnNwWnWnN"},
        {'1',"WnNwNnNnW"},
        {'2',"NnWwNnNnW"},
        {'3',"WnWwNnNnN"},
        {'4',"NnNwWnNnW"},
        {'5',"WnNwWnNnN"},
        {'6',"NnWwWnNnN"},
        {'7',"NnNwNnWnW"},
        {'8',"WnNwNnWnN"},
        {'9',"NnWwNnWnN"},
        {'A',"WnNnNwNnW"},
        {'B',"NnWnNwNnW"},
        {'C',"WnWnNwNnN"},
        {'D',"NnNnWwNnW"},
        {'E',"WnNnWwNnN"},
        {'F',"NnWnWwNnN"},
        {'G',"NnNnNwWnW"},
        {'H',"WnNnNwWnN"},
        {'I',"NnWnNwWnN"},
        {'J',"NnNnWwWnN"},
        {'K',"WnNnNnNwW"},
        {'L',"NnWnNnNwW"},
        {'M',"WnWnNnNwN"},
        {'N',"NnNnWnNwW"},
        {'O',"WnNnWnNwN"},
        {'P',"NnWnWnNwN"},
        {'Q',"NnNnNnWwW"},
        {'R',"WnNnNnWwN"},
        {'S',"NnWnNnWwN"},
        {'T',"NnNnWnWwN"},
        {'U',"WwNnNnNnW"},
        {'V',"NwWnNnNnW"},
        {'W',"WwWnNnNnN"},
        {'X',"NwNnWnNnW"},
        {'Y',"WwNnWnNnN"},
        {'Z',"NwWnWnNnN"},
        {'-',"NwNnNnWnW"},
        {'.',"WwNnNnWnN"},
        {' ',"NwWnNnWnN"},
        {'$',"NwNwNwNnN"},
        {'/',"NwNwNnNwN"},
        {'+',"NwNnNwNwN"},
        {'%',"NnNwNwNwN"},
        {'*',"NwNnWnWnN"},
    };
    #endregion

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string code;
        int narrow = 1, wide = 3;
        if (!_codes.TryGetValue((char)value, out code)) return null;

        code += 'n';
        var result =  from i in Enumerable.Range(0, code.Length)
          select new
          {
              color = (code[i] == 'W' || code[i] == 'N') ? Brushes.Black : Brushes.Transparent,
              width = (code[i] == 'n' || code[i] == 'N') ? narrow : wide
          };
        return result;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

github: https://github.com/KevinPan/wpf-barcode, , .

, .

+3

Source: https://habr.com/ru/post/1774502/


All Articles