Но для универсального приложения для Windows ничего не работает
Тестируя на моей стороне образец , который вы связали, работает.
Поскольку вы не предоставили свой фрагмент кода, который декодирует штрих-код, я предоставляю весь проект, который может хорошо работать на моей стороне, чтобы вы могли протестировать его и попытаться выяснить, что не так с вашим фрагментом кода.
Сначала установите пакет nuget ZXing.uwp
и выполните тестирование со следующим фрагментом кода.
XAML
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Image x:Name="imgshow" Height="150" Width="150"></Image>
<TextBox x:Name="txtDecoderType" Height="50" Width="250"></TextBox>
<TextBox x:Name="txtDecoderContent" Height="50" Width="250"></TextBox>
<Button x:Name="btnscan" Click="btnscan_Click" Content=" scan"></Button>
</StackPanel>
Код позади
private async void btnscan_Click(object sender, RoutedEventArgs e)
{
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(size.Width, size.Height);
StorageFile file = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
//QR code conversion from jepg and return string.
WriteableBitmap writeableBitmap;
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
writeableBitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
writeableBitmap.SetSource(fileStream);
imgshow.Source = writeableBitmap;
}
// create a barcode reader instance
IBarcodeReader reader = new BarcodeReader();
// detect and decode the barcode inside the writeableBitmap
var barcodeReader = new BarcodeReader
{
AutoRotate = true,
Options = { TryHarder = true }
};
Result result = reader.Decode(writeableBitmap);
// do something with the result
if (result != null)
{
txtDecoderType.Text = result.BarcodeFormat.ToString();
txtDecoderContent.Text = result.Text;
}
}
}
Кроме того, с 1803 года UWP также предоставляет API для сканера штрих-кода , подробности см. , это руководство и официальный образец .