Декодировать штрих-код из изображения в универсальном приложении для Windows - PullRequest
0 голосов
/ 09 мая 2018

Я понимаю, универсальное приложение для Windows на Windows Tablet (Windows 10). У этого планшета есть камера, и я хочу сделать снимок qr-кода и расшифровать его.

Итак, я пробовал с Zxing.net, но не работает на универсальном приложении для Windows. Есть метод для декодирования qr-кода внутри фотографии.

 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.
                }

Я посетил несколько ссылок ... например: https://github.com/Redth/ZXing.Net.Mobile а также https://archive.codeplex.com/?p=zxingnet#trunk/Clients/WindowsRTDemo/Package.appxmanifest

Но для универсального приложения для Windows ничего не работает. Решения?

1 Ответ

0 голосов
/ 11 мая 2018

Но для универсального приложения для 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 для сканера штрих-кода , подробности см. , это руководство и официальный образец .

...