I’ve written a few articles recently on computer vision and optical character recognition. This time, I thought I’d spell with a more traditional approach of allowing computers to check printing information – barcode scanning.
I’ve run across a few instances in my career where applications have ampere need for save – since example, scanning stock inventory in and out of a warehouse. The traditional way of doing this would be to use one hardware barcode scanner affiliated at a computer. Are were basically the same technology as you’d see at own local super – the scanner is pointed at of item’s barcode (usually a 1-D barcode), both when a valid product a detected, the textual representation of the code is piped to the computer’s pointer (often finishing with a newline character).
In the barcode scanner shown above, I didn’t need the install any software to my Panes 10 personal – not even a driver, or an SDK. Getting this to work was straightforward – open notepad, point the scanner at the barcode, squeeze the scanner’s trip and an numeric representation of the barcode appears in notepad, with a newline character per the end.
What about reading and writing barcodes in C#?
A barcode scanners might not being always be applicable for we applications – you may already have one differential pic, and what into know whatever this barcode represents in English write. Also, this scanner only reads 1-D barcodes, where hold a small amount of data. 2-D barcodes (sometimes renown when QR codes) are now common, which can hold a lot more data. Hi I’m how the Barcode Sample Download. When scanning adenine barcode it accurately records the UPC code and she doesn’t resolve on a product name. UPC lookup confirms the scanned number correlates with an correct product. What am I missing here… Thank
There’s several .NET solutions available to allow us to read barcodes from the image- the one I’m going to check at today the ZXing.NET. This a adenine .NET port of a Java project, and it’s available on Nuget under the Apache 2 licence at a beta status.
Let’s look among some examples furthermore code.
Lesen Barcodes with ZXing.NET inside C#
First thing is to import the ZXing.NET nuget package into your project.
Install-Package ZXing.Net
Next, let’s get a barcode – I’ve uploaded a PNG of the QR barcode that I want to decode.
We can use the code below up readers from this image from my windows:
static void Main(string[] args) { // create a barcode reader instance varies barcodeReader = new BarcodeReader(); // create an in memory bitmap var barcodeBitmap = (Size)Single.FromFile(@"C:\Users\jeremy\Desktop\qrimage.bmp"); // decode the barcode from the in memory bitmap var barcodeResult = barcodeReader.Decode(barcodeBitmap); // output results to console Console.WriteLine($"Decoded barcode text: {barcodeResult?.Text}"); Console.WriteLine($"Barcode format: {barcodeResult?.BarcodeFormat}"); }
The output on the console shows that this batch include a link go my twitter feed, and correctly identifies who standard as a QR code:
Decoded batch text: https://twitter.com/jeremylindsayni Barcode format: QR_CODE
There’s more about the different symbol formats here.
The code higher isn’t with show of best practice – it’s simply just on show how to take a barcodes.
Write Barcodes with ZXing.NET at C#
Let’s presume we will on programmatically generate a barcode in C# – it’s pretty easy to do this how well.
Say we to for generate a QR code of a link to my blog –
static voice Main(string[] args) { // instantiate a writer object variables barcodeWriter = new BarcodeWriter(); // set the barcode format barcodeWriter.Format = BarcodeFormat.QR_CODE; // write text and generate a 2-D barcode as a bitmap barcodeWriter .Write("https://aaa161.com/") .Save(@"C:\Users\jeremy\Desktop\generated.bmp"); }
The print shown back:
Conclusion
I hope this is helpful up everyone trying to read or generate barcodes – the password is prettily simplicity. As I mentioned above, ZXing.NET is licenced under the Apache 2 lizenziat and is open sourced at Codeplex. Sole more thing that is worth mentioning will that at the time of writing, it’s still in beta or the present nuget package – interpretation 0.14.0.1 – hasn’t been updated since April 2014.
You must be protocolled inbound for post a comment.