繁体   English   中英

在UWP中使用ZXing.Net.Mobile生成图像源

[英]Generate Imagesource with ZXing.Net.Mobile in UWP

问题

我正在尝试使用ZXing.Net.Mobile在UWP项目中创建条形码图像。 我找到了这种方法

但是在var wb = result.ToBitmap() as WriteableBitmap; 我得到的结果为byte [],没有方法ToBitmap()

然后我找到了这个简单的代码

var writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
var wb = writer.Write("12345678");
BarcodeImg.Source = wb;

但最后一个通道引发错误byte[] cannot be converted to ImageSource

在我幼稚的头脑中,我认为“确定应该很容易”。 哈! 我到处都可以找到类似的答案

BitmapImage没有方法BeginInitEndInit

如何将byte []转换为ImageSource或使用ZXing创建一个?

我被卡住了,再次我对UPW不熟悉。 我会为每一个技巧而感激。

更新资料

好吧,此时此刻情景看起来像这样

private async void updateBarcodeImg(string code) {
    var writer = new BarcodeWriter();
    writer.Format = BarcodeFormat.QR_CODE;
    var wb = writer.Write(code) as Byte[];
    try {
        BarcodeImg.Source = await ImageFromBytes(wb);
    } catch (Exception e) {
        Debug.WriteLine(e.Message);
    }
}

public async static Task<BitmapImage> ImageFromBytes(Byte[] bytes) {
    BitmapImage image = new BitmapImage();
    using (IRandomAccessStream stream = bytes.AsBuffer().AsStream().AsRandomAccessStream()) {
        stream.Seek(0);
        await image.SetSourceAsync(stream);
    }
    return image;
}

更新资料

然后排队await image.SetSourceAsync(stream); 异常引发“ HRESULT异常:0x88982F50”。 Google表示这是因为流未设置为位置0。但是我之前做了一行。

根据您的问题,您将使用ZXing.Net.Mobile创建条形码图像。 但是您发现的示例正在使用ZXing.Net 它们不是同一个库。

要使用ZXing.Net.Mobile ,我们需要从NuGet安装ZXing.Net.Mobile

Install-Package ZXing.Net.Mobile

然后使用以下代码:

var writer = new ZXing.Mobile.BarcodeWriter
{
    Format = ZXing.BarcodeFormat.QR_CODE,
    Options = new ZXing.Common.EncodingOptions
    {
        Height = 300,
        Width = 300
    },
    Renderer = new ZXing.Mobile.WriteableBitmapRenderer() { Foreground = Windows.UI.Colors.Black }
};
var writeableBitmap = writer.Write("https://developer.microsoft.com/en-us/windows/windows-apps");

QrCodeImg.Source = writeableBitmap;

我们也可以通过从NuGet安装ZXing.Net来使用ZXing.Net

Install-Package ZXing.Net

并使用示例中的代码:

ZXing.IBarcodeWriter writer = new ZXing.BarcodeWriter
{
    Format = ZXing.BarcodeFormat.QR_CODE,//Mentioning type of bar code generation
    Options = new ZXing.Common.EncodingOptions
    {
        Height = 300,
        Width = 300
    },
    Renderer = new ZXing.Rendering.PixelDataRenderer() { Foreground = Windows.UI.Colors.Black }//Adding color QR code
};
var result = writer.Write("http://www.bsubramanyamraju.blogspot.com ");
var wb = result.ToBitmap() as Windows.UI.Xaml.Media.Imaging.WriteableBitmap;
//Displaying QRCode Image
QrCodeImg.Source = wb;

请注意,我们不能 同时使用这两个库。 请确保您使用的是与所选库匹配的正确代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM