0

我正在尝试使用 ZXing.Net 在 Windows 应用商店应用程序中解析 QR 码,但是当我尝试使用他们网页上的最新版本运行它时,它在第 50 行给了ArgumentNullExceptionBitmapLuminanceSource.Silverlight.cs一个

这条线看起来像这样

var data = System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(writeableBitmap.PixelBuffer, 0, (int)writeableBitmap.PixelBuffer.Length);

is not null ,WriteableBitmap所以我不什么,什么是null。

有谁能够帮助我?

就是从这个方法

public BitmapLuminanceSource(WriteableBitmap writeableBitmap)
   : base(writeableBitmap.PixelWidth, writeableBitmap.PixelHeight)
{
   var height = writeableBitmap.PixelHeight;
   var width = writeableBitmap.PixelWidth;
   var stride = width * 4;
   luminances = new byte[width * height];
   Color c;

#if NETFX_CORE
   var data = System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(writeableBitmap.PixelBuffer, 0, (int)writeableBitmap.PixelBuffer.Length);
   for (int y = 0; y < height; y++)
   {
      int offset = y * stride;
      for (int x = 0, xl = 0; x < stride; x += 4, xl++)
      {
         c = Color.FromArgb(
            data[x + offset], 
            data[x + offset + 1], 
            data[x + offset + 2], 
            data[x + offset + 3]);
         luminances[y * width + xl] = (byte)(0.3 * c.R + 0.59 * c.G + 0.11 * c.B + 0.01);
      }
   }
#else
   var pixels = writeableBitmap.Pixels;
   for (int y = 0; y < height; y++)
   {
      int offset = y * width;
      for (int x = 0; x < width; x++)
      {
         int srcPixel = pixels[x + offset];
         c = Color.FromArgb((byte)((srcPixel >> 0x18) & 0xff),
            (byte)((srcPixel >> 0x10) & 0xff),
            (byte)((srcPixel >> 8) & 0xff),
            (byte)(srcPixel & 0xff));
         luminances[offset + x] = (byte)(0.3 * c.R + 0.59 * c.G + 0.11 * c.B + 0.01);
      }
   }
#endif
}

更新

WriteableBitmat 是使用此代码创建的

// Get the File
var File = await FilePick.PickSingleFileAsync();

// Convert the File to a Bitmap
var Stream = await File.OpenAsync(FileAccessMode.Read);
var Bmp = new BitmapImage();
Bmp.SetSource(Stream);
var WBmp = new WriteableBitmap(Bmp.PixelWidth, Bmp.PixelHeight);
WBmp.SetSource(Stream);

通过使用 Damir Arh 的回答,错误被移到了下面的代码中

c = Color.FromArgb(
    data[x + offset], 
    data[x + offset + 1], 
    data[x + offset + 2], 
    data[x + offset + 3]);

IndexOutOfRangeException我在哪里得到一个

x = 580
xl = 145
offset = 31104
y = 36
height = 216
width = 216
stride = 864
data = {byte[31684]}

我当然可以看到它为什么超出范围,但我不知道如何修复它

使用 Damir Arh 的更新答案修复了它Stream.Seek(0)

4

1 回答 1

1

我快速移植了项目主页上提供的 Silverlight 示例,它确实有效:

var dlg = new FileOpenPicker();
dlg.FileTypeFilter.Add(".png");
var file = await dlg.PickSingleFileAsync();
if (file != null)
{
    currentBarcode = new WriteableBitmap(89, 89);
    using (var stream = await file.OpenReadAsync())
    {
        currentBarcode.SetSource(stream);
    }
    imgDecoderBarcode.Source = currentBarcode;
    var result = reader.Decode(currentBarcode);
    if (result != null)
    {
        txtDecoderType.Text = result.BarcodeFormat.ToString();
        txtDecoderContent.Text = result.Text;
    }
    else
    {
        txtDecoderType.Text = String.Empty;
        txtDecoderContent.Text = "No barcode found.";
    }
}

我还尝试从您发布的代码中调用违规行,并且没有引发异常:

var dlg = new FileOpenPicker();
dlg.FileTypeFilter.Add(".png");
var file = await dlg.PickSingleFileAsync();
if (file != null)
{
    currentBarcode = new WriteableBitmap(89, 89);
    using (var stream = await file.OpenReadAsync())
    {
        currentBarcode.SetSource(stream);
        var data = System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(currentBarcode.PixelBuffer, 0, (int)currentBarcode.PixelBuffer.Length);
    }
}

你的问题是你如何创建WriteableBitmap传递给这个方法的。因为您首先从同一个流中加载 a Bitmap,所以当您将其设置为WritableBitmap. 您需要将自己重新定位到流的开头,以便可以再次加载数据:

// Convert the File to a Bitmap
var Stream = await File.OpenAsync(FileAccessMode.Read);
var Bmp = new BitmapImage();
Bmp.SetSource(Stream);
Stream.Seek(0);
var WBmp = new WriteableBitmap(Bmp.PixelWidth, Bmp.PixelHeight);
WBmp.SetSource(Stream);
于 2013-01-08T06:15:27.623 回答