首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在UWP中用ZXing.Net.Mobile生成图像源

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

Stack Overflow用户
提问于 2016-10-06 15:09:04
回答 1查看 2.8K关注 0票数 0

问题

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

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

然后我找到了这个直接的代码

代码语言:javascript
运行
复制
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不熟悉。我会感谢每一个小费。

更新

好的,在这个时刻,情况如下

代码语言:javascript
运行
复制
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);中,一个异常是抛出"Exception from HRESULT: 0x88982F50“。Google说这是因为流没有被设置为0。但我早做了一句。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-07 10:21:42

根据您的问题,您将使用ZXing.Net.Mobile创建条形码图像。然而,您发现的样本正在使用ZXing.Net。他们不是同一个图书馆。

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

代码语言:javascript
运行
复制
Install-Package ZXing.Net.Mobile

然后使用以下代码:

代码语言:javascript
运行
复制
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;

此外,我们还可以通过安装ZXing.Net来使用ZXing.Net来自NuGet

代码语言:javascript
运行
复制
Install-Package ZXing.Net

并使用示例中的代码:

代码语言:javascript
运行
复制
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;

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39899616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档