简体   繁体   English

在Xamarin.Forms应用程序中使用ZXing.Net.Mobile进行二维码渲染

[英]QR code rendering with ZXing.Net.Mobile in Xamarin.Forms app

I'm trying to use ZXing.Net.Mobile to display a QR code as an image. 我正在尝试使用ZXing.Net.Mobile将QR码显示为图像。

I'm having difficulty getting the byte[] returned from BarcodeWriter.Writer(...) into an image for display. 我很难将从BarcodeWriter.Writer(...)返回的byte[]转换为图像进行显示。

ie this does not display an image 即,这不显示图像

public class BarcodePage : ContentPage
{
    public BarcodePage()
    {
        Image image = new Image();

        image.Source = ImageSource.FromStream(() => 
        {
            var writer = new BarcodeWriter 
            {
                Format = BarcodeFormat.QR_CODE,
                Options = new EncodingOptions 
                {
                    Height = 200,
                    Width = 600
                }
            };

            var bitmapBytes = writer.Write ("Some text");
            return new MemoryStream(bitmapBytes);
        });

        Content = image;
    }
}

The bitmapBytes byte array looks like it has reasonable values in the debugger, how can I turn that into an image for display? bitmapBytes字节数组看起来在调试器中具有合理的值,如何将其转换为图像以供显示?

If I change the Image.Source assignment to: 如果我将Image.Source分配更改为:

image.Source = ImageSource.FromUri(new Uri("http://i.imgur.com/q0aIYvC.png"));

then it does correctly display the image, so I know the problem isn't with the Forms layout, etc. 然后它确实正确显示图像,所以我知道问题不在于Forms布局等。

Workaround 解决方法

At the moment, I'm doing conversion to a MemoryStream in the native, eg in Android I have: 目前,我正在转换为本机中的MemoryStream,例如我在Android中:

public class BuildQrCodes_Android : IBuildQrCodes
{
    public async Task<MemoryStream> ImageStreamForAsync(string text)
    {
        var writer = new BarcodeWriter 
            {
                Format = ZXing.BarcodeFormat.QR_CODE,
                Options = new EncodingOptions 
                    {
                        Height = 600,
                        Width = 600
                    }
            };

        var bitmap = writer.Write(text);

        var stream = new MemoryStream();
        await bitmap.CompressAsync(Bitmap.CompressFormat.Png, 100, stream);
        stream.Position = 0;

        return stream;
    }
}

and then over in the Xamarin Forms side, I have 然后在Xamarin Forms方面,我有

IBuildQrCodes qrBuilder = /* Service lookup */
var stream = await qrBuilder.ImageStreamForAsync("the text to encode");
return ImageSource.FromStream(() => new MemoryStream(stream.ToArray()));

which is doing what I want, so it's an acceptable workaround. 这是我想要的,所以这是一个可接受的解决方法。

I am not sure what widget "Content" in your code represents, but assigning your "image" to a Form View's Image Class that is a member of your ContentPage should do the trick. 我不确定代码中的“内容”小部件是什么,但是将“图像”分配给作为ContentPage成员的表单视图的图像类应该可以解决问题。

In XAML, you would create an Image within your ContentPage, ie like in one of the Form's samples . 在XAML中,您将在ContentPage中创建一个Image,即在Form的一个示例中

Otherwise, dynamically create an Image class widget, add it your layout via AddView for your ContentPage . 否则,动态创建一个Image类小部件,通过AddView为您的ContentPage添加您的布局。

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

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