地铁应用程序的条形码扫描仪
本文关键字:扫描仪 条形码 应用程序 地铁 | 更新日期: 2024-10-22 20:52:31
我想开发一个用于扫描条形码的metro应用程序。是否存在用于条形码扫描的默认库?请向我提供指南和示例应用程序
查看CodePlex和NuGet上的ZXing.Net。CodePlex源代码有WinRT应用程序的示例。
主页.xaml
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Canvas Width="640" Height="360">
<CaptureElement x:Name="VideoCapture" Width="640" Height="360" />
<Image x:Name="CaptureImage" Width="640" Height="360" Visibility="Collapsed" />
</Canvas>
<TextBlock x:Name="Error" VerticalAlignment="Bottom" FontSize="32" Width="640" TextAlignment="Center" Margin="363,0,363,37" />
<TextBlock x:Name="ScanResult" VerticalAlignment="Bottom" TextAlignment="Center" FontSize="32" Width="640"/>
</Grid>
主页.xaml.cs
public sealed partial class MainPage : Page
{
private readonly MediaCapture _mediaCapture = new MediaCapture();
private Result _result;
public MainPage()
{
InitializeComponent();
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
try
{
var cameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
if (cameras.Count < 1)
{
Error.Text = "No camera found, decoding static image";
await DecodeStaticResource();
return;
}
MediaCaptureInitializationSettings settings;
if (cameras.Count == 1)
{
settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras[0].Id }; // 0 => front, 1 => back
}
else
{
settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras[1].Id }; // 0 => front, 1 => back
}
await _mediaCapture.InitializeAsync(settings);
VideoCapture.Source = _mediaCapture;
await _mediaCapture.StartPreviewAsync();
while (_result == null)
{
var photoStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync("scan.jpg", CreationCollisionOption.GenerateUniqueName);
await _mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), photoStorageFile);
var stream = await photoStorageFile.OpenReadAsync();
// initialize with 1,1 to get the current size of the image
var writeableBmp = new WriteableBitmap(1, 1);
writeableBmp.SetSource(stream);
// and create it again because otherwise the WB isn't fully initialized and decoding
// results in a IndexOutOfRange
writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight);
stream.Seek(0);
writeableBmp.SetSource(stream);
_result = ScanBitmap(writeableBmp);
await photoStorageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
await _mediaCapture.StopPreviewAsync();
VideoCapture.Visibility = Visibility.Collapsed;
CaptureImage.Visibility = Visibility.Visible;
ScanResult.Text = _result.Text;
}
catch (Exception ex)
{
Error.Text = ex.Message;
}
}
private async System.Threading.Tasks.Task DecodeStaticResource()
{
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets'1.jpg");
var stream = await file.OpenReadAsync();
// initialize with 1,1 to get the current size of the image
var writeableBmp = new WriteableBitmap(1, 1);
writeableBmp.SetSource(stream);
// and create it again because otherwise the WB isn't fully initialized and decoding
// results in a IndexOutOfRange
writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight);
stream.Seek(0);
writeableBmp.SetSource(stream);
CaptureImage.Source = writeableBmp;
VideoCapture.Visibility = Visibility.Collapsed;
CaptureImage.Visibility = Visibility.Visible;
_result = ScanBitmap(writeableBmp);
if (_result != null)
{
ScanResult.Text += _result.Text;
}
return;
}
private Result ScanBitmap(WriteableBitmap writeableBmp)
{
var barcodeReader = new BarcodeReader
{
TryHarder = true,
AutoRotate = true
};
var result = barcodeReader.Decode(writeableBmp);
if (result != null)
{
CaptureImage.Source = writeableBmp;
}
return result;
}
protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
await _mediaCapture.StopPreviewAsync();
}
}