SimpleCameraReader – easy to use wrapper class for ZXing 2.0 for WP7

In the previous post I showed you how to use ZXing 2.0 with Windows Phone 7 for barcode scanning. Now I’m going to show how you can simplify that process a bit using the SimpleCameraReader class I wrote that wraps the Camera- and ZXing methods.

How do I use it?

First we have to start up Visual Studio and create a new Windows Phone 7 Application and reference SimpleCameraReader.dll and zxingwp7.dll. Follow the download links below to download the necessary files or build the latest source from Github to continue.

For the design we will re-use the design from last post with minor changes to the foreground and background color to match the theme on my blog.

MainPage.xaml

This sets up a full-screen view of the camera feed using a Rectangle and a VideoBrush as a fill. We overlay a Listbox that will show the codes we scan on top of the feed. VideoBrush lets you paint any XAML element using a video source, which comes in handy when we want to display the camera feed. Then we apply a CompositeTransform that lets us rotate the VideoBrush-element to match the cameras orientation depending on how we’re holding the phone. In essence, this means that you can turn your phone around and it will always end up showing correctly. That’s all there is to the design, now let’s go deeper and implement some functionality. Next up we’ll initialize the SimpleCameraReader and get to grips on how to use it. So go into the code-behind file for MainPage (MainPage.xaml.cs)

MainPage.xaml.cs

 

First we instantiate a ObservableCollection to hold our codes and the other is our SimpleCameraReader instance which is (obviously) essential for this tutorial. In the constructor we set up the ObservableCollection and bind it to our ListBox’s ItemsSource. Are you unfamiliar with ObservableCollection? Read my “ZXing 2.0 – Windows Phone 7 Library” post and get a general idea why we use it.

We override the OnNavigatedTo method (event) which will fire when the page is navigated to and this is where we’ll initialize our SimpleCameraReader instance. There are four simple steps to get it up and running. First create a new instance of SimpleCameraReader, then set the source of VideoBrush to the Camera property of our reader-instance. Next up we’ll hook up some events; when the reader successfully decodes a code and when the camera is finished initializing. We CAN’T start our scanning process before the camera is initialized so that’s what we’re going to do next:

MainPage.xaml.cs

When the camera has initialized we set the rotation of our preview to the CameraOrientation so the preview displays correctly. Then we set (this is optional) the FlashMode to FlashMode.On to indicate that when focusing use the flash. Finally we start the scanning process.

That’s all there is to start the process, now we just need to stop it when we’re navigating away from the page and or course handle our results.

MainPage.xaml.cs

That’s all there is to create an application for scanning barcodes, qr codes etc

UPDATE!

Thanks to Tomas and Payam for discovering that the application will crash if have multiple pages and navigate back and forth between the camerapage. The solution that was posted in the comments is as follows

Mainpage.xaml.cs

So, where to go from here?

This is a simple app and you might have noticed that we haven’t set any specific barcode-formats yet. That’s because if we don’t specify any formats when we’re creating a new instance of SimpleCameraReader it will include all formats that are available. To set it to only scan for QR codes for instance we can do that:

That’s all we can do with SimpleCameraReader right now, but it’s actively being developed on so watch the project on Github or come back here and look for updates!

 

C# Geographic/UTM Coordinate Converter Class

Been a while since my last post so I thought I would like to share a C# / .NET class that I converted from a javascript source. This one has proven to be quite helpful when dealing with GPS in one of my Windows Phone 7 projects. I’m currently working on an application for checking real-time data, travel routes etc from Trafikanten which provides data from the subway, train, bus and so on. Official applications already exist for Android and iPhone so I thought I’d give it a go for WP7.

This class allows for converting between Geographic (Latitude and Longitude) to UTM (X and Y positions) and vice-versa. Using the GPS on WP7 returns Latitude and Longitude coordinates while the JSON API provided by Trafikanten utilizes X and Y positions.

Using the class is very simple; first you have to import the namespace

[box]using trafikappen.Utils;[/box]

and to convert to UTM you can type

[box]GeoUTMConverter gutmConverter = new GeoUTMConverter();

gutmConverter.ToUTM(Latitude, Longitude);[/box]

or the other way around

[box]gutmConverter.ToLatLon(X, Y, Zone, Hemisphere.Northern|Hemisphere.Southern)[/box]

That’s all there is to it.

Download source: GeoUTMConverter.cs

Sources:

[box icon=”info”]Copyright 1997-1998 by Charles L. Taylor

http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html

Javascript converted from http://cambertx.com/utm/[/box]

MPC Remote v1.0

Hi everybody!

The site has been down for a couple of days now due to us transferring the domain over to a new provider and thus hasn’t been pointing to the right location. Happy to see that everything’s fixed now though!

I can announce that I’ve completed the work on my first working version of MPC Remote which I’ve set to version 1.0 because it is stable and fulfills the features and goals I’ve set. This version will be submitted to the marketplace soon (as soon as Microsoft can fix a bug in the submission page) but you can download it today from my blog here if you happen to own an unlocked phone.

Shows how the main screen looksFeatures

  • Clean new interface (not very exciting in terms of flashy ‘yet’)
  • Buttons to control playback, volume and seeking (currently implements the five-second seek back/forward)
  • Main page provides the information you need
  • Settings page allows for setting the host and port of the machine running MPC

Head on over to the project page for download

Installing Windows 7 Professional x64 on Acer Aspire 5943G: Thank you Linux!

After waiting a couple of weeks now and watching my lovely laptop deteriorate everytime I turned it off and on again I finally burned Windows 7 Professional x64 edition on to a DVD download legally from MSDNAA (Thank you school!). A short visit to acer.com showed me I didn’t have to download any drivers so I went ahead with the install. Everything seemed right! Or so I thought…

Minutes later a fresh Windows 7 desktop was staring at me, but something was off. There was no network connection, a low resolution and generally things were missing. A quick look in the device manager revealed that I was missing all of the drivers I was told were going to be in Windows 7 by default. I panicked a little bit, because how on God’s earth would I be able to get my system up and running in time for the evening? And why was Acer lying to me about the driver?

My first attempt was to use my Samsung Galaxy S phone to download the necessary LAN driver but with no luck. Turns out the browser won’t allow me to download files that aren’t supported/associated by Android and the Acer support site doesn’t work properly on the Android device. I realized later that I’m a Linux fanboy/enthusiast and that I always keep a couple of distros laying around. My savior? Linux Mint. Booting up the Live CD allowed me to browse the net and download the LAN driver for Windows. That’s what I love about Linux distributions, they’ve got almost every driver in place for use. Of course there’s some devices that aren’t supported or need to download restricted drivers but yeah.. My savior was Linux Mint.

Turns out that Acer wasn’t lying to me. I just didn’t see the drop-down box showing me what version of Windows 7 I wanted at first, so it defaults to one of the  32-bit Home versions. So if you’re going to reinstall your operating system on an Acer laptop, be sure to check that you have the necessary drivers or have a Live CD with a Linux distribution at hand.