Friday 12 September 2014
using Zxing for barcode reading
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.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment