在 Windows 通用应用中生成条形码和二维码





5.00/5 (5投票s)
在本文中,我们将学习如何在 Windows 通用应用中生成条形码和二维码。
引言
在移动应用中,我们经常需要创建/扫描条形码和二维码。因此,我们将探讨如何在 Windows 通用应用中生成条形码/二维码。为此,我们将使用 ZXing 条形码扫描库。它在 Nuget 上可供 .Net 开发者使用。
描述
创建一个空白的通用应用。它将包含三种类型的项目:Windows Store 应用、Windows Phone 应用和共享项目。
现在从 Nuget 安装 ZXing 条形码扫描库 .Net
要从包管理器控制台安装 ZXing 条形码扫描库 .Net 端口,请在 包管理器控制台中运行以下命令
PM> Install-Package ZXing
您需要在 Windows Store 和 Windows Phone 应用中安装此库。
现在在共享项目中添加 MainPage 类,因为我们将共享代码。
//MainPage class in shared project
public sealed partial class MainPage : Page
{
}
在 XAML 中创建布局以显示条形码和二维码图像。两种应用的布局将分开。
//MainPage.xaml in windows store and windows phone project
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" VerticalAlignment="Center" Orientation="Horizontal">
<TextBlock Text="Barcode" Foreground="White" FontSize="25" VerticalAlignment="Center" Margin="10 0 10 0"></TextBlock>
<TextBox VerticalAlignment="Center" Width="150" Foreground="Black" FontSize="25" Margin="0 0 10 0" Name="txtBarcode"></TextBox>
<Button Content="Ok" Foreground="White" FontSize="25" Name="btnBarcode" Click="btnBarcode_Click"></Button>
</StackPanel>
<StackPanel Grid.Row="1" Grid.RowSpan="2" Background="White">
<Image Name="imgBarcode" Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
</StackPanel>
<StackPanel Grid.Row="4" VerticalAlignment="Center" Orientation="Horizontal">
<TextBlock Text="QRcode" Foreground="White" FontSize="25" VerticalAlignment="Center" Margin="10 0 10 0"></TextBlock>
<TextBox VerticalAlignment="Center" Width="150" Foreground="Black" FontSize="25" Margin="0 0 10 0" Name="txtQRcode"></TextBox>
<Button Content="Ok" Foreground="White" FontSize="25" Name="btnQRcode" Click="btnQRcode_Click"></Button>
</StackPanel>
<StackPanel Grid.Row="5" Grid.RowSpan="2" Background="White">
<Image Name="imgQRCode" Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
</StackPanel>
</Grid>
创建条形码和二维码按钮点击事件处理程序并生成图像。此库中提供了多种编码选项,您可以根据需要使用任何一种。
//MainPage class in shared project
private void btnBarcode_Click(object sender, RoutedEventArgs e)
{
IBarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.CODE_128,
Options = new ZXing.Common.EncodingOptions
{
Height = 100,
Width = 450
}
};
var result = writer.Write(txtBarcode.Text);
var wb = result.ToBitmap() as WriteableBitmap;
imgBarcode.Source = wb;
}
private void btnQRcode_Click(object sender, RoutedEventArgs e)
{
IBarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new ZXing.Common.EncodingOptions
{
Height = 160,
Width = 160
}
};
var result = writer.Write(txtQRcode.Text);
var wb = result.ToBitmap() as WriteableBitmap;
//add to image component
imgQRCode.Source = wb;
}