Mobile Development: Analyze QRcode content on Windows Mobile

QRreaderWH6

You possibly know these QRcodes that make your phone open a web site or add a contact. Unfortunately there is no such function on Windows Mobile and I just wrote a demo of such a functionality:
A demo app to show how these nifty QR barcodes are encoded to hold URLs, business cards, appointments or other data. Further on, the application shows how to use QR code reading and import or launch the appropiate application for the QRcode content. The demo uses ZXing.Net to encode text into a QRcode barcode image but ZXing or ThoughtWorks code to decode QRcode images as the latter was easier to use. Then there is a list of sample encodings to show you how to specify a vcard or SMS or others to be encoded into a readable QRcode.

After start of the app and then simply click in the type list at the top and select for example “vcard”. In the sample field you can see how a vcard is to be encoded:

BEGIN:VCARD
N:Heinz-Josef Gode
ORG:ACME GmbH
TITLE:Systems Engineer
TEL:0123456789
URL:www.hjgode.de/wp
EMAIL:heinz-josef.gode@somewhere.com
ADR:Musterstrasse 1
NOTE:Support Engineer
END:VCARD

Now click [decode&encode] and you get the QRcode image and the cleaned content:

  

The ‘decoded’ cleaned content is:

Heinz-Josef Gode
Systems Engineer
ACME GmbH
Musterstrasse 1
0123456789
heinz-josef.gode@somewhere.com
www.hjgode.de/wp
Support Engineer

If you click [process] instead, the information is transferred to the right app (if any) on the device. For a vcard, this is Pocket Outlook or say Contacts. You can check the information has arrived in Contacts:

     

Easy! Just using zxing client code and some mobile code. Here is a calendar entry example:

  

You also have the option to save the QRcode image or to load QRcode images. You even can take a photo of a QRcode print. See the File menu with Save Image, Load Image and the Extras menu with Camera.

The code to parse a QRcode text is:

            if (comboBox1.SelectedIndex == -1)
                return;
            testQRcodes = new TestQRcodes();
            TestQRcodes.TestType tt = (TestQRcodes.TestType)comboBox1.SelectedItem;
            string sTest = tt.content;
            QRdecodeClass qr = new QRdecodeClass(sTest);
            txtResultType.Text = qr.resultTypeString;
            txtResultContent.Text = qr.sResult;
            encode2QRcode(txtResultContent.Text);

whereas encode2QRcode encodes the data into a QRcode image:

        void encode2QRcode(string s)
        {
            try
            {
                var writer = new BarcodeWriter();
                writer.Format = BarcodeFormat.QR_CODE;
                writer.Options.Height = 240;
                writer.Options.Width = 240;
                pictureBox1.Image = writer.Write(s);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
            }
        }

The code to decode text from a QRcode image is that:

        private void mnuLoadImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string FileName = ofd.FileName;
                try
                {
                    pictureBox1.Image = new Bitmap(FileName);
                    QRCodeDecoder decoder = new QRCodeDecoder();
                    String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(pictureBox1.Image)));

                    txtResultContent.Text = decodedString;

                    processQRresult.processResult(txtContentSample.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Sorry, could not load image. Exception: " + ex.Message);
                    txtResultContent.Text = ex.Message;
                }
            }
        }

You see the code is simple.

A list of possible standard texts to encode into a barcode: http://archive.is/20120530/http://code.google.com/p/zxing/wiki/BarcodeContents

Warning: the menu extra offers to try to capture an image to be decoded. Most QRCode decoders I have tested fail in decoding these camera pictures. As I am working with and developing for industrial devices, this is no problem for me. Most of these devices have a barcode reader intergrated and decode QRcode images the fastest.

Download source of QRreaderWH6 incl. ZXing.net binary runtimes

[Download not found]

Leave a Reply