0

我想使用 xaml C# 读取 Windows 8 应用程序中的 QR 码/条形码,有什么指示吗?

我尝试使用 ZXing.Net

    var openPicker = new FileOpenPicker { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.PicturesLibrary };
    openPicker.FileTypeFilter.Add(".jpg");
    openPicker.FileTypeFilter.Add(".jpeg");
    openPicker.FileTypeFilter.Add(".png");

    StorageFile file = await openPicker.PickSingleFileAsync();
    if (file != null)
    {
        // Application now has read/write access to the picked file

        BitmapImage bmp = new BitmapImage();
        IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
        bmp.SetSource(stream);
        BarCodeImage.Source = bmp;

        IBarcodeReader reader = new BarcodeReader();
        WriteableBitmap barcodeBitmap = new WriteableBitmap(1,1);
        barcodeBitmap.SetSource(stream);
        var result = reader.Decode(barcodeBitmap);
    }

但是在加载结果时,我收到“值不能为空”的异常。请帮助它应该是什么代码

4

1 回答 1

2

我终于得到了解决方案,希望它将来可以帮助其他人

    private async void DecodeStaticResource(StorageFile file)
    {
        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);

        var result = ScanBitmap(writeableBmp);
        if (result != null)
        {
            ScanResult.Text += result.Text;
        }
    }

    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;
    }
于 2013-05-09T20:58:17.583 回答