2013-05-09 43 views
0

我想在Windows 8应用程序中使用xaml C#读取QR码/条码,任何指针?在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); 
    } 

但在加载结果我得到异常“值不能为空”。请帮助应该是什么它

+1

尝试制作和精力首先,有大量的信息,并了解这应该是能够帮助你。另外,将问题标记为C#而不是放在标题中。 FAQ http://stackoverflow.com/faq可能会派上用场...... – Kobunite 2013-05-09 09:11:44

回答

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