Ruby on Rails: Running specific tests aka “I just want to test my current module”

There comes a time when your project has a reasonable amount of tests, be it unit tests, integration tests and what not. When working on a specific component, let’s say a payment module for an online shop you could probably do without running the whole test suite every single time you’re creating, changing or deleting a test for the payment module.

Now, if you just want to run a single test you can do so easily from the command-line using:

You could probably extend that one liner to include a list of files and create a shell script that does it for you, but why do that when we can create a rake test task that does it for us? If we open up our Rakefile and add a new TestTask under the test namespace like this:

This will allow us to run

from the command-line and it will run through the tests we’ve defined in the file list. You could probably be smart about the list using wildcards (or expressions?), but for now we only have the two files. This requires that the tasks be maintained, but in return you don’t have to run all the other tests each time you want to see if something failed or passed in the payment module.

I realize there might be other ways and perhaps some cool gems that can help us out even further so if you have tips, suggestions or the absolute truth then please feel free to comment and I’ll do my best to update the post!

 

 

 

 

Want to learn C# and Windows (Phone) development?

The best inspiration for writing blog posts is absolutely the questions I receive from people via Facebook, Twitter, E-mail and more. For this particular post the question at hand was if I had any good resources for learning to code and develop applications in C#.

How one learn is different for each person. Someone needs to have hands-on tutoring, visual/audio-based material and of course reading. However; the only way to truly learn is by doing and practicing. If you are the one who needs hands-on tutoring then I might suggest you look up courses near you, as I can’t help you there. In this post I have tried to come up with some good video walkthroughs and reading material for each category, so continue reading to get to the good stuff.

How to debug Windows Store applications on Windows RT tablets

After receiving my Asus VivoTab RT tablet in the mail I’ve been enjoying it quite a bit these past days. Today, I wanted to try developing some small applications and running it on the tablet which proved to be a bit more difficult than on a Windows Phone. You can’t just plug it in the USB and click run, we have to set up Remote Debugging!

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!

 

ZXing 2.0 – Windows Phone 7 library

This weekend I’ve spent some good hours into porting the ZXing 2.0 library to a Windows Phone 7 library as well as a simple library called SimpleCameraReader that makes scanning for codes with the in-built camera an ease! Look forward to the next post for how to use the SimpleCameraReader.

So what is ZXing? Taken from the official homepage for the library:

[box icon=”info”]ZXing (pronounced “zebra crossing”) is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. Our focus is on using the built-in camera on mobile phones to scan and decode barcodes on the device, without communicating with a server. However the project can be used to encode and decode barcodes on desktops and servers as well. We currently support these formats:

  • UPC-A and UPC-E
  • EAN-8 and EAN-13
  • Code 39
  • Code 93
  • Code 128
  • ITF
  • Codabar
  • RSS-14 (all variants)
  • QR Code
  • Data Matrix
  • Aztec (‘beta’ quality)
  • PDF 417 (‘alpha’ quality)

[/box]

There’s a C# version for ZXing 2.0 which I used as a base (I’m hoping to rewrite the whole thing) in which I had to fix around 400-500 errors before it could compile as a Windows Phone 7 class library. The C# version also seems to be ported using a Java to C# tool so the codebase was a bit messy. I’ve fixed parts of it, trying to change and use newer techniques where I had the opportunity. That said, I haven’t gone through all the files and made changes so there’s plenty of room for improving!

View the zxingwp7 library on Github!

Or download the binaries!

How do I use it?

When I first looked up how to use zxing in Windows Phone 7 I stumbled a cross a really nice guide showing you all the steps you need (the steps all apply with my port too!) to get your app scanning for codes using an older version of the ZXing library. You can check it out here. It’s wrriten by Jonas Follesø and is an excellent example of how to use both the old library and mine.

The first thing you want to do is create a new Windows Phone 7 application and reference the zxingwp7 library which you can download here.

Next up is editing the MainPage.xaml and replace the LayoutRoot grid with the following code (Sorry Jonas, I’m copying your code!)

[box]

MainPage.xaml

[/box]

What’s happening here is that we’re creating a Rectangle element that will hold our camera feed and specify that our rectangle’s fill should be a new element, VideoBrush. 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.

Next step is to implement some functionality; initiating our camera and connect the camera to our VideoBrush element!

[box]

MainPage.xaml.cs

[/box]

In this snippet we instantiate a ObservableCollection to hold our results, a timer that will on a reguler basis scan the camera feed for codes, our PhotoCamera object that talks directly to the Camera on WP7 and in this instance a QRCodeReader which can detect and decode QR codes from a image.

The nice thing about ObservableCollection in case you haven’t used it before is that when you add or remove items from it, it will notify the component that is bound to the ObservableCollection that it needs to update. That means when we add an item to our _matches object it will automatically popup in the Listbox without us having to tell our Listbox that it needs to update. Handy little thingie.

In our constructor we set up our ObservableCollection (_matches) and databind it to our ListBox (_matchesList.ItemsSource = _matches) as well as setting up our timer with interval of 250 milliseconds. The tick event fires off every 250 milliseconds and will then call a method called ScanPreviewBuffer() which we will take a look at in a bit. Notice that the timer hasn’t started yet; we have to call _timer.Start(); before it will start ticking. You will see how this is done in a bit.

The next method is an overriden event-method inherited from the PhoneApplicationPage class. This method gets called when this page gets navigated to and it’s here we’ll setup our Camera and bind it to our VideoBrush element. We create a new instance of PhotoCamera and bind to the Initialized event that gets fired when the camera is ready to go.

[box]

[/box]

Here’s the code that tells our VideoBrush to use our PhotoCamera as it’s video source.

[box]

[/box]

This piece of code tells that when you half-press the camera button it should start the auto-focus process.

The next step is to set up our QRCodeReader and start the timer when the Camera has initialized:

[box]

MainPage.xaml.cs

[/box]

When the camera has initialized we create a new instance of PhotoCameraLuminanceSource with the width and height of the preview resolution. The class inherits from LuminanceSource and is responsible for extracting luminance data from an image. Next up we create an instance of QRCodeReader which is responsible for detecting and decoding the image to scan for a QR code. I’ll show you later on how to read other formats as well! When it’s all set up we use the Dispatcher to invoke the next methods (get our code to run on the UI-thread instead of the thread that the Camera runs on). We set the rotation of the preview and start our timer.

[box]

MainPage.xaml.cs

[/box]

The ScanPreviewBuffer is the method where all the magic happens. To best explain the process I’m going to copy the description from Jonas Follesø’s guide:

[box icon=”info”]

The GetPreviewBufferY method takes a byte array as its parameter and will populate this byte array with the luminance data from the preview buffer. We pass in the PreviewBufferY property from the PhotoCameraLuminanceSource. Once we have captured the luminance data we create a HybridBinarizer and a BinaryBitmap. They are part of the ZXing library. I haven’t worked enough with ZXing yet to fully understand the architecture, but these classes are steps the luminance data pass through before being passed to the decode method of the QRCodeReader. If the decode is successful the decoded text is added to the ObservableCollection, which in turn will update the ListBox. The QRCodeReader decode method will throw an exception if it is not able to decode the image, so we need to wrap the code in a try-catch block.

[/box]

And that’s all there is for a simple QR code scanning app. Try it!

You can download the tutorials source code here.

So, where to go from here?

I’ve only shown how to scan QR codes, but the library has a ton of other formats as shown in the start of this post. To get the individual readers for each format just replace QRCodeReader with EAN13Reader or EAN8Reader for European product barcodes. If you want to create an app that can scan for multiple codes at the same time you can use the MultiFormatReader. This one requires a little bit of extra effort of adding what formats to scan for but it’s really simple.

[box]

[/box]

For the MultiFormatReader we need to create a list of type BarcodeFormat of the formats we want to include in the scan, and then create a Dictionary of type DecodeHintType as the key and Object as value. We add our formats to the hint dictionary with DecodeHintType of POSSIBLE_FORMATS. We could also add another hint for the Reader to try harder:

[box]

[/box]

That’s all there is to set that up, and replace _reader.decode(binBitmap) with multiReader.decodeWithState(binBitmap); — The decodeWithState tells the MultiFormatReader to use our hints.

If you want to include all the formats then I’ve added an extra touch to the ZXing library; just replace var formats = … with:

[box]

[/box]

That’s pretty much there is for a basic barcode scanning app. There’s a bit of code to get it done but if you check my blog in a couple of days I’ll have a tutorial up explaining how to use my SimpleCameraReader class for simplifying the process a bit. If you’re eager you can check it out at Github already! Just click here

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]

Ja-ja-java!

My second (serious) day at school is over and what a joy it’s been so far! Yesterday we had TK110; a subject covering how computers work, the history, binary/hex etc, you get the picture 🙂 Anyways, it was real fun and the tests/assignments was also enjoyable. It’s been way way too long ago since I had anything IT related school work to do.

Today we had Java class. Even though I had to get up at 6 AM to get on school on time it was a interesting lecture and I’m looking forward to the next ones and the assignments we’ll get 🙂 Programming is a subject that has always intrigued me and I’ve been involved in smallish coding sessions since I was a young kid with PHP, Visual Basic, VB.NET and to this day I use C# and WPF often.

I’ll include this little YouTube video for you to watch, it’s a music video for JavaZone 2010 (8-9th September). It’s a “parody” of Lady Gaga – Poker Face. Now it’s Lady Java with JavaZone, enjoy!

ps3m2ts

I’ve used mkv2vob sometimes to convert my .mkv files to either a .vob or .m2ts file that my Playstation 3 console can play. Why? I’ve hooked it up to our big screen (well actually a projector) and surround system in my loft and so I thought it would be easier if I could watch all my media on the PS3 instead of switching between that and a PC. That may sound lazy but I really do enjoy the XMB interface and using the controller to navigate through folders etc. It just feels right.

The only problem with mkv2vob is that I have to manually add the files/folders I want to convert and I’m a guy that enjoys when things can run automatically. Mkv2vob does not have a command-line interface which makes running it from within a script or another application really difficult. So I began searching through the forums only to discover that it’s a feature request and intended for later; also the source code for the application isn’t available at this time.

I’m a software developer, self-taught in C# and other programming/scripting languages so I thought I might give it a try to write a command-line application that can do what mkv2vob can — so I can implement it in future projects for automating the process of converting media files. I attempted to write something similar maybe a year before but I never got anywhere because I didn’t know how I would fix the .h264 streams to work with the PS3. Back then you could and you still can use a tool called h264info that you could input the .h264 file and specify fps etc and it would add the necessary parts to the h264 file so the PS3 can read it. It would allow you to change the H.264 profile (PS3 won’t handle H.264 level 5.1 properly). I encountered the same problem, there was no command-line interface for h264info and I was unaware of any other methods at the time so I gave up after trying to rewrite h264info in C#.

This time around things are different. Thanks to a great tool made by SmartLabs called tsmuxer I can now do all of the necessary things to make a .mkv work on PS3. tsmuxer can demux .mkv and provide the same functionality as h264info in regards of fixing the .h264 parts and mux it back together as a .m2ts file which can be read and played back by the PS3. This made my work much easier.

I made a tool I call ps3m2ts (orginally ps3video but it might have been confusing with PS3 Video 9 etc). The tools used are actually the same tools found in mk2vob except that I don’t use mencoder or the dts applications provided with mkv2vob (yet). That’s the only minor setback with my tool as of now. If the media file contains a DTS stream it converts it to AC3 so you’ll know it will work. I haven’t figured out the DTS thing that mkv2vob does yet, but then again I started developing this tool three days or go.

Tools:

  • Mediainfo
    I use this tool to extract information from the .mkv file, what video streams and audio streams are available and what codecs they use, FPS and etc. This is most useful.
  • Tsmuxer
    As mentioned, this is the holy grail, the main tool used. If your .mkv contains a .h264 and AC3 audio this is the only tool used in the process of converting because it demuxes, fixes and mux the files back together all by itself.
  • eac3to
    This tool is used for converting the .dts audio stream to .ac3 so it can easily be played back on the PS3.
  • mkvextract
    A part of a set of tools, mkvtoolnix; this tool is used to extract the different tracks (files) contained in your .mkv file. This is only used when there’s a .dts audio stream because we need it to convert it to .ac3

If you want to create your own little tool for doing the same thing I did tsmuxer needs a META file to be created containing the information required so it can do it magic. The .meta file tells tsmuxer what options and what input files to be used. Here’s an example .meta for the movie Eurotrip:

MUXOPT –no-pcr-on-video-pid –new-audio-pes –vbr –split-size=4GB –vbv-len=500
V_MPEG4/ISO/AVC, “EuroTrip.2004.AC3D.720p.HDTV.x264.mkv”, fps=23.976, level=4.1, insertSEI, contSPS, ar=As source, track=1
A_AC3, “EuroTrip.2004.AC3D.720p.HDTV.x264.mkv”, track=2

The first line contains muxing options (I got these from the tsmuxer-GUI) except for the –split-size=4GB where you can specify if you want tsmuxer to split the file if you plan on copying it to a FAT32 drive where the max filesize is 4GB. So very useful. I don’t use the split options as I use a media server to stream it from my computer.

The next two lines specify the input files and the options. The format is as following:

<code name>, <file name>, <parameters>

The code name is the Codec ID we get from Mediainfo, in this case it’s AVC (H264), the file name and the parameters. A full listing of the different parameters and mux options you can go to this site and read up. The important ones to use when creating a .m2ts for playback on the PS3 is

insertSEI, contSPS
level=4.1  — If the level is higher than 4.1 use this so tsmuxer can change the profile level to 4.1 which is playable on the PS3

The other parameters you see like fps should be straight forward. We get this from Mediainfo and pass it a long so we hopefully won’t get wrong framerate and mess up the result. You also see track=ID parameter which is used if you don’t use external file but just want to use a track in the original .mkv. Save your file as something.meta and launch the tsmuxer

tsmuxer.exe something.meta output.m2ts

tsmuxer can output to m2ts, ts, Blu-Ray Disc or AVCHD folder.

This is a very long post, and I’m sorry you had to read until the end to get your hands on my tool but hopefully you have learned something (or not). Either way I give you the Binary and source versions. It’s written in C#, .NET version 3.0

Binary files: ps3m2ts-binary-v0.1.zip
Source code: ps3m2ts-source-v0.1.zip

To use the application

ps3m2ts.exe “file” [split]

Hopefully this application works for you, if it doesn’t give me a comment or send me an e-mail to me at henningms at gmail.com. If you’re a software developer hopefully you can improve upon the code and give me tips and ideas, it’s still a work in progress but all help is welcome. Join me!
Thanks for your time