在C#Windows phone 8.1中生成.vcf卡/名片的二维码

时间:2016-04-11 16:54:54

标签: c# windows-phone-8.1 windows-store-apps qr-code vcard

我创建了一张VCard /名片。我需要从该Vcard创建QR码。当用户扫描然后QR码然后显示选项以将该vcard保存在联系人列表中。我能够从字符串生成QR码,但我不知道如何生成StorageFile / Vcard的QR码。

这是我的代码。

 protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        vcard = e.Parameter as StorageFile;



        IBarcodeWriter writer = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new ZXing.Common.EncodingOptions
            {
                Height = 300,
                Width = 300
            }
        };


        var result2 = vcard;

     //  Result2 contain vcard. but writer.Write()   need string formate.

       // below commented line display error. because result2 is not string 
       // var result = writer.Write(result2);


        var result = writer.Write(result2.ToString());


        var wb = result.ToBitmap() as WriteableBitmap;

        //add to image component
        bar_image.Source = wb;
  }

enter image description here

1 个答案:

答案 0 :(得分:0)

请在GitHub上查看此项目:QrCoder

根据项目的Wiki,可以为vCard和其他一些要求生成QR码。对于vCard,QrCoder仅需要大约6行代码:

-从Wiki逐字复制:

ContactData generator = new ContactData(ContactData.ContactOutputType.VCard3, "John", "Doe");
string payload = generator.ToString();


QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
var qrCodeAsBitmap = qrCode.GetGraphic(20);