QR Code can by definition contain any binary content: bytes, strings, numbers, etc. In general strings are most frequently used. The following examples show how to generate QR Codes with different contents.

Plain text, message
using Esponce.QRCode
...
var client = new QRCodeClient();
var stream = client.Generate("Hello World"); //Simply put a message as the first argument

Hyperlink
var client = new QRCodeClient();
var stream = client.Generate("http://qrcodeapi.codeplex.com/"); //There is no special about hyperlinks either

Contact card
There are 3 main formats for contact cards:
  • VCARD - well defined standard, has lots of fields but more overhead (syntax can take more space than actual data) and thus require more capacity which results in larger QR Code
  • MECARD - standard format known as Phonebook Registration, more compact content and suitable for QR Code, usually for personal cards since it has no fields for company info
  • BIZCARD - de-facto standard, compact and similar to MECARD, suitable for business cards since it contains company info (company name, job title, website)

//Build a MECARD
var mecard = new MecardContentFormat();
mecard.FirstName = "Monkey";
mecard.LastName = "Esponce";
mecard.PhoneNumber = "+1 555 123 4567";
mecard.EmailAddress = "monkey@esponce.com";
mecard.HouseNumber = "432 Sherwood Circle";
mecard.City = "Lake Charles";
mecard.Prefecture = "LA";
mecard.ZipCode = "70601";
mecard.Country = "USA";
string raw = mecard.Build();

//raw = "MECARD:N:Esponce,Monkey;TEL:+1 555 123 4567;EMAIL:monkey@esponce.com;ADR:432 Sherwood Circle, Lake Charles, LA, 70601, USA;;"

//VCard and Bizcard are very similar, e.g. replace MecardContentFormat with VCardContentFormat

var client = new QRCodeClient();
var stream = client.Generate(raw);

Content decoding
var client = new QRCodeClient();

//QR Code sample with BIZCARD content
//raw = "BIZCARD:N:Monkey;X:Esponce;T:Code Monkey;C:Esponce;A:432 Sherwood Circle, Lake Charles, LA 70601;B:+15551234560;E:monkey@esponce.com;;"
string raw = client.Decode(File.OpenRead("bizcard-sample.png"));

//Detecting content format
VCardContentFormat vcard;
if (VCardContentFormat.TryParse(raw, out vcard))
{
  //TODO: Handle content if VCARD
  return;
}

MecardContentFormat mecard;
if (MecardContentFormat.TryParse(raw, out mecard))
{
  //TODO: Handle content if MECARD
  return;
}

BizcardContentFormat bizcard;
if (BizcardContentFormat.TryParse(raw, out bizcard))
{
  //TODO: Handle content if BIZCARD
  Console.WriteLine(bizcard.DisplayName);
  Console.WriteLine(bizcard.CompanyName);
  Console.WriteLine(bizcard.WebsiteUrl);
  return;
}

//Check if hyperlink
if (Regex.IsMatch(raw, @"^\s*https?://.+"))
{
  //TODO: Handle hyperlink
  return;
}

//Else treat content as plain text

Last edited Oct 10, 2012 at 7:32 AM by Avivo, version 1

Comments

No comments yet.