2013-05-09 46 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; 
    }