我想使用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);
}
但在加载结果时,我得到“Value不能为null”的异常。请帮助应该为它编写代码
答案 0 :(得分: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;
}