1

I need to create a qrreader with windows phone.

Xzing examples only print to video the qr string captured, I need an example of how to understand if this string is a vcard and, consequently, save it in contact, or if it is a link and open it in the browser.

private void ScanPreviewBuffer()
    {

            try
            {
                _photoCamera.GetPreviewBufferY(_luminance.PreviewBufferY);
                var binarizer = new HybridBinarizer(_luminance);
                var binBitmap = new BinaryBitmap(binarizer);
                var result = _reader.decode(binBitmap);
                Dispatcher.BeginInvoke(() => CheckQr(result.Text));
            }
            catch {  }
     }

    private void CheckQr(string qrString)
    {

        VibrateController vibrate = VibrateController.Default;
        vibrate.Start(TimeSpan.FromMilliseconds(500));

        MessageBox.Show(qrString);
        /* CONTROLS HERE */
    }
Lin-Art
  • 4,813
  • 2
  • 23
  • 28
mikiamomik
  • 43
  • 2
  • 8

2 Answers2

0

Obviously you have to start by parsing the qrString content to get what you want, i think we'll both agree on that point ;)

So the main issues are :

  • Determining formats (url or vcard)
  • Parsing them (if needed)
  • Using them to trigger wanted actions

1. About vCard

To determine if you qrString holds a vCard, maybe you could just try to match (with string.Contains or string.StartsWith methods) the vCard header which is BEGIN:VCARD and always seems to be the same from one version to another (see wikipedia).

For Windows Phone 7, there's no builtin features to parse vCards, so you have to do it by yourself or you could try to use the vCard library For Windows Phone. It would be used this way :

   byte[] byteArray = Encoding.UTF8.GetBytes(qrString);
   using (StreamReader reader = new StreamReader(new MemoryStream(byteArray)))
   {
      vCard card = new vCard(reader);
      // access here card.PropertyFromvCard to get the information you need
   }

There's not so much documentation about it, but sources are available on codeplex, so you'll probably find all the property names and samples you need.

For Windows Phone 8, the builtin ContactInformation.ParseVcardAsync method could help you to parse your qrString content (here is an official tutorial)

Then you need to finally create your contact :

If you're developping your App on Windows Phone 7, there's no way to create a contact directly from your application. You need to use the "save contact task" and pre-populate the fields you need. Here's an example :

SaveContactTask saveContactTask = new SaveContactTask();
saveContactTask.Completed += new EventHandler<SaveContactResult>(saveContactTask_Completed);
saveContactTask.FirstName = "John"; // card.PropertyFromvCard and so on...
saveContactTask.LastName = "Doe";
saveContactTask.MobilePhone = "2065550123";
saveContactTask.Show();

If you're developping on Windows Phone 8 (and it doesn't seem to be the case given your question tags), you can create a Custom contact store and write directly into it

2. About URLs

To know if you're dealing with an URL or not, i would advice you to follow suggestions coming with this SO answer. To make a long story short, here's the code you could use or at least something similar :

static bool IsValidUrl(string qrString)
{
    Uri uri;
    return Uri.TryCreate(urlString, UriKind.Absolute, out uri)
        && (uri.Scheme == Uri.UriSchemeHttp
         || uri.Scheme == Uri.UriSchemeHttps
         || uri.Scheme == Uri.UriSchemeFtp
         || uri.Scheme == Uri.UriSchemeMailto
            /*...*/);
}

And finally to open your URL into a web browser (if it is a valid one of course), you could use the WebBrowser task or embed a true WebBrowser into your application with the WebBrowser control and make good use of it.

Community
  • 1
  • 1
AirL
  • 1,857
  • 2
  • 17
  • 17
0

ZXing has a class called ResultParser with a static method parseResult. The ResultParser supports some common content formats like vCard, vEvent, URL, etc. It gives you as a result an instance of AddressBookParsedResult for vCard content back.

ParsedResult parsedResult = ResultParser.parseResult(result);
Michael
  • 1,966
  • 1
  • 13
  • 13