我正在尝试使用 ZXing.Net 在 Windows 应用商店应用程序中解析 QR 码,但是当我尝试使用他们网页上的最新版本运行它时,它在第 50 行给了ArgumentNullException
我BitmapLuminanceSource.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)