简体   繁体   English

使用Xamarin.Forms和Zxing生成QR码

[英]Generate QR code with Xamarin.Forms and Zxing

I've seen alot about this online (old posts) but nothing seems to work for me. 我在网上看到了很多这样的帖子(旧帖子),但似乎没有什么对我有用。 I'm trying to generate a QR code out of a string and display it in the app. 我正在尝试从字符串中生成QR码并将其显示在应用程序中。

Here's what i had in the beginning 这就是我在开始时所拥有的

qrCode = new ZXingBarcodeImageView
{
    BarcodeFormat = BarcodeFormat.QR_CODE,
    BarcodeOptions = new QrCodeEncodingOptions
    {
        Height  = 50,
        Width   = 50
    },
    BarcodeValue = codeValue,
    VerticalOptions = LayoutOptions.CenterAndExpand,
    HorizontalOptions = LayoutOptions.CenterAndExpand
};

That works fine for Android but on IOS devices its not rendered at all. 这适用于Android,但在IOS设备上它根本没有渲染。 So after researching i tried to do it like this: 所以在研究之后我试着这样做:

Image qrCode;

if (Device.OS == TargetPlatform.iOS)
{
    var writer = new BarcodeWriter
    {
        Format = BarcodeFormat.QR_CODE,
        Options = new ZXing.Common.EncodingOptions
        {
            Width = 50,
            Height = 50
        }
    };

    var b = writer.Write(codeValue);

    qrCode = new Image
    {
        Aspect = Aspect.AspectFill,
        VerticalOptions = LayoutOptions.CenterAndExpand,
        HorizontalOptions = LayoutOptions.CenterAndExpand,
        Source = ImageSource.FromStream(() =>
        {
            MemoryStream ms = new MemoryStream(b);
            ms.Position = 0;
            return ms;
        })
    };

}else{
    qrCode = new ZXingBarcodeImageView
    {
        BarcodeFormat = BarcodeFormat.QR_CODE,
        BarcodeOptions = new QrCodeEncodingOptions
        {
            Height  = 50,
            Width   = 50
        },
        BarcodeValue = codeValue,
        VerticalOptions = LayoutOptions.CenterAndExpand,
        HorizontalOptions = LayoutOptions.CenterAndExpand
    };
}

Content = new StackLayout
{
    Children = {
        header, lblExplenationText, qrCode
    },
    BackgroundColor = Color.White
};

But there is still nothing rendered at all. 但是仍然没有任何渲染。

ZXing.Mobile.Forms NuGet Package Version: 2.1.47 (newest) ZXing.Mobile.Forms NuGet包版本:2.1.47(最新)

It seems to be a known issue . 这似乎是一个众所周知的问题
Luckily there is a workaround, to set a HeightRequest & WidthRequest , here is a working code example: 幸运的是,有一个解决方法,设置HeightRequestWidthRequest ,这是一个工作代码示例:

ZXingBarcodeImageView GenerateQR(string codeValue)
{
    var qrCode = new ZXingBarcodeImageView
    {
        BarcodeFormat = BarcodeFormat.QR_CODE,
        BarcodeOptions = new QrCodeEncodingOptions
        {
            Height = 350,
            Width = 350
        },
        BarcodeValue = codeValue,
        VerticalOptions = LayoutOptions.CenterAndExpand,
        HorizontalOptions = LayoutOptions.CenterAndExpand
    };
    // Workaround for iOS
    qrCode.WidthRequest = 350;
    qrCode.HeightRequest = 350;
    return qrCode;
}

Add in the app delegate this line ZXing.Net.Mobile.Forms.iOS.Platform.Init(); 在app中添加委托此行ZXing.Net.Mobile.Forms.iOS.Platform.Init();

before LoadApplication(new App()); 在LoadApplication之前(new App());

and ready ... 准备好......

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

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