2014-10-20 14 views
0

我是Windows Phone 8.1開發人員中的新成員。 我在做一個簡單的應用程序,使用ZXing.Net和Windows Phone 8.1 SDK掃描條碼。ZXing.Net中的「索引超出數組範圍」解碼方法

當我打電話給解碼方法,我得到的文本例外「索引數組的範圍之外」

下面是部分代碼:

InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); 
await Camera.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); 

stream.Seek(0); 

var properties = Camera.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); 
var videoEncodingProperties = properties as VideoEncodingProperties; 

WriteableBitmap writableImg = new WriteableBitmap((int)videoEncodingProperties.Width, (int)videoEncodingProperties.Height); 

await writableImg.SetSourceAsync(stream); 

result = barcode.Decode(writableImg); // The exception is here 

if (result != null) 
{ 
    debug.Text = result.Text; 
} 
else 
{ 
    debug.Text = "No results"; 
} 

我認爲這個問題是與WritableImage的大小,因爲當我在模擬器上運行應用程序(肯定沒有條形碼),解碼器解碼並返回沒有值(這沒關係),但是當我在WP8.1上運行它時設備,我得到一個文本異常: 「索引超出了陣列的範圍」

我試圖調整可寫圖像的大小,但沒有結果!但也許我正在調整一個不好的方式或價值觀。 有什麼幫助嗎?謝謝。

回答

0

您需要將效仿這一動作:與PixelWidth實際圖像上發現的,然後再做一次

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

先拉出來的圖像流的。

+0

感謝@Lyle,作爲一個qr代碼閱讀器,此代碼是否執行可接受的性能和資源行爲(例如高內存使用率)? – Hashem 2014-10-21 22:52:44

+0

我的項目專注於我的業務code39,我還沒有得到zxing讀取code39條形碼 – Lyle 2014-10-22 14:14:56

+0

我試過這種策略,但writeableBmp.PixelWidth,writeableBmp.PixelHeight總是1,所以我總是得到索引外部錯誤... – pomarc 2014-11-03 12:22:11