Dr Tran's playground

Dr Tran's step by step programming lessons

Friday 12 September 2014

using Zxing for barcode reading

No comments :

I needed to be able to decode barcodes so looked for a library and stumbled across ZXing which is an open source project originally written in java. It was ported over to C# and can be found at https://zxingnet.codeplex.com/


Here is how to use it
- download the library with github, link on the site above
- add reference to the zxing.dll that corresponds to the version of .net you are using in this project (I’m using .Net 4.5 so navigate to C:\zxingnet2\net4.5

- get a barcode and put it at this location
"C:\\examples\\barcode.png"

- add this code to your function. I’m using the same code as the previous project.
- add a picturebox on the form (picturebox1)
- add two textboxes on the form (textbox1, textbox2)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
using ZXing;


namespace WindowsFormsApplication2
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
           try
           {
               IplImage test = Cv.LoadImage("C:\\examples\\barcode.png", LoadMode.Color);
               Cv.NamedWindow("hello");
               Cv.ShowImage("hello", test);
               textBox1.Text = "fou";

               // create a barcode reader instance
               IBarcodeReader reader = new BarcodeReader();
               // load a bitmap
               
               var barcodeBitmap = (Bitmap)Bitmap.FromFile("C:\\examples\\barcode.png");
               // detect and decode the barcode inside the bitmap
               pictureBox1.Image = barcodeBitmap;
               var result = reader.Decode(barcodeBitmap);
               // do something with the result
               if (result != null)
               {
                   textBox1.Text = result.BarcodeFormat.ToString();
                   textBox2.Text = result.Text;
               }


               //--------------------
                


               Cv.ShowImage("hello", test);

           }
           catch (Exception ex)
           {
               int i = 0;
           }
       }
   }
}

- F5 and it will run and show the code type and code

- note that this is quite a robust reading method as long as the code is well isolated from print. Preprocessing should be used to remove another within a certain distance from the barcode.

Saturday 6 September 2014

Using opencsharp

No comments :
Now this was much simpler than I thought, and since I don’t anticipate going into OpenCV functions to alter them, I will just run with the DLLs of OpenCV.

- download gitgui for windows. This program will allow us to easily clone GitHub repositories to our hard drive.

- clone the opencvsharp
- bottom-right corner→ press clone to desktop → put in a location (I chose C:/opencvsharp)

- compile and run some sample code
- open the solution at C:\opencvsharp\sample
- build all
- set a project as startup project (I chose CStyleSamples)
- run (F5)

You should get these outputs.


It works!!! If you are having problems, please post questions here.

Open CV setup

No comments :

For all of us less fluent Visual Studio users, here are step by step of how I setup Visual Studio, Cmake, and OpenCV to running a working “hello world” type project. Please let me know if I’m doing something unconventional or that is bad practice. We’re all here to learn!


- install Visual Studio 2012 (or any other versions but do know which versions you are installing)

→ note: VC11 is Visual Studio 2012, VC12 is VIsual Studio 2013…

I installed it with all the standard settings, yes to everything


- install OpenCV latest version (I stay away from Alpha versions because they might have bugs and might not be supported by the latest OpenCVSharp which is my end goal.


I went with all the default values and put it in the default directory “C:\opencv249”


- install cmake

go to this address


get the cmake-3.0.1-win32-x86.exe binaries, it makes your life way simpler because it just extracts itself. I went with default locations.

“C:\Program Files (x86)\CMake”


- write a .cpp file with enough to call some OpenCV functions (got this from the openCV site tutorials) http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html#linux-gcc-usage




Put this file in this directory for example

.\Documents\Visual Studio 2012\Projects\DisplayImage


open cv setup


#include <stdio.h>

#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char** argv )

{

if ( argc != 2 )

{

printf("usage: DisplayImage.out <Image_Path>\n");

return -1;

}

Mat image;

image = imread( argv[1], 1 );

if ( !image.data )

{

printf("No image data \n");

return -1;

}

namedWindow("Display Image", CV_WINDOW_AUTOSIZE );

imshow("Display Image", image);

waitKey(0);

return 0;

}



open cv setup


- write a cmakelists.txt file that has this inside

cmake_minimum_required(VERSION 2.8)

project( DisplayImage )

find_package( OpenCV REQUIRED )

add_executable( DisplayImage DisplayImage.cpp )

target_link_libraries( DisplayImage ${OpenCV_LIBS} )



open cv setup


- run cmake.exe and browse to the location of the source file and cmakelists.txt

C:/Users/NAME/Documents/Visual Studio 2012/Projects/DisplayImage

point the build to where you want the solution and project files need to be.

C:/Users/NAME/Documents/Visual Studio 2012/Projects/DisplayImage/Build

- when prompted for which compiler, choose Visual Studio 2012 (I am using the 32bit version, you can tell it’s 32bits by the fact that the Visual Studio 2012 folder is under “c:/program files x86” folder.)


→ note: I had the error “error in configuration process” because I had selected Visual Studio 2013, selecting the right version should go through quite nicely

What’s happening here is that you are creating solution and project files that Visual Studio will be able to open, compile and build the project.

open cv setup







it will configure...

open cv setup


Then press Generate, it will generate

open cv setup


- Now go to “C:\Users\NAME\Documents\Visual Studio 2012\Projects\DisplayImage\build” where you just created the solution and project files.

open cv setup

Open the DisplayImage solution file


open cv setup


- Press F6 to build all.

open cv setup


- Now everything is built but this is a command line project so you need to run it from command line

- navigate to the build directory

C:\Users\NAME\Documents\Visual Studio 2012\Projects\DisplayImage\build\Debug

Bring in the needed dlls

→ opencv_highgui249d.dll

→ opencv_imgproc249d.dll

→ opencv_core249d.dll

Bring in an image of your choice (here I brought in an image I called lena.jpg)

open cv setup

(you can right-click and save this one as lena.jpg, to that build directory)

open cv setup


- Now navigate to the right directory in command prompt

open cv setup


type in DisplayImage lena.jpg and you will see the image you put in that directory!


open cv setup

Tuesday 1 May 2012

How to display Hello World with C#

No comments :
Well, this is where it begins, C#, a HelloWorld console and Windows form application.

Microsoft has made it super easy for anybody to start programming, but this is really just the beginning. You'll find tons of sweet additions in C#, my favorite is Intellisense, which accurately predicts what I'm going to be typing simply based on a few letters and history of what I've typed. Intellisense was present in Microsoft Visual C++ but really not to this extent, it was pretty much only useful when you had a class with functions or members.

Here's a video that should guide you through how to get started in C#. It gets more interesting later on but let's start by getting our feet wet.

I like to have a recipe for everything, that way its easier to tell where it went downhill. Watch the video to guide you through.

1- Start Microsoft Visual Studio (whichever version, it'll be the same for now). I'm using the Ultimate edition.
2- Click on New Project
3- Select Windows Console Application
4- Type in a name in the application name: I will type HelloWorldConsole
5- Type Console.WriteLine("Hello World!!!") in the main function
6- Press F5 to start debugging or Ctrl-F5 for start without debugging which will keep the screen on after the application exits.

And there you have it. Your own first program. Play around with what you can type in there as well as format just to confirm to yourself that you are in control of C#... for now.




Console applications are fun for beginners but if you want to do something commercial, it's just not awesome enough. I'd go to at least Windows form for something acceptable and make money or make someone happy. Here's a tutorial on how to do Hello World using Windows Form with C#. As you can see, its very simple and that's thanks to Microsoft (i'm a big fan).




And what's hot now are phone apps. Windows also has its own phone and the beauty of it is that the language used to program it is C#!!! Which means that all the stuff you learn from Windows Forms is reusable here, plus more. I've programmed iPhone-iPad apps before and I'll tell you, the submission procedure is so much simpler with Windows phone that I was considering switching over to Windows phone only.

Here's a hello world video on windows phone, hope you enjoy how simple it is and go from there!