Generate QR Codes for MeCard Data in F#
Today I learned about MeCard file formats for representing contact information while reading the post Sharing contacts using QR Code without requiring a network connection by Andre Garzia.
When I saw the MeCard format, I immediately liked how simple it was so I decided to see how difficult it'd be to write some code to generate a QR code for my own contact data.
What is MeCard?
According to Wikipedia, MeCard is "...a data file similar to vCard...It is largely compatible with most QR-readers for smartphones. It is an easy way to share a contact with the most used fields. Usually, devices can recognize it and treat it like a contact ready to import."
An example might look like:
Encoding text as a QR code using F#
The snippet is a F# Interactive script which uses Net.Codecrete.QrCodeGenerator NuGet package to encode text as a QR code and saves it to an SVG file.
The core logic consists of the following code:
let createQrCode (savePath:string) (target:string) = let qr = QrCode.EncodeText(target,QrCode.Ecc.High) let svgString = qr.ToSvgString(4) File.WriteAllText(savePath,svgString, Encoding.UTF8)
In this case
target is the text I want to encode and
savePath is the path of the SVG file I'm saving the generated QR code in.
Given MeCard with the following content:
The QR Code generated might look as follows:
Scanning the QR Code with a smartphone would then immediately recognize it as a contact and prompt you to save the contact information to your device.
Since I already had the code snippet to do this available, creating a MeCard was straightforward. I've since added my MeCard alongside by vCard. You can check out the corresponding QR code in my contact page.