我正在尝试使用ZXing.Net解析Windows应用商店应用中的二维码,但是当我尝试使用其网页上的最新版本运行它时,它在ArgumentNullException
中为我提供BitmapLuminanceSource.Silverlight.cs
在第50行
这条线看起来像这样
var data = System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(writeableBitmap.PixelBuffer, 0, (int)writeableBitmap.PixelBuffer.Length);
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)
答案 0 :(得分:1)
我快速创建了一个可以从project homepage获得的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
。因为您首先从同一个流中加载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);