4

I am using the ZXing.Net library(0.16.4) to encode and decode a QR Code. I got the reference of how to decode the qr code from here: C# with ZXing.Net: Decoding the QR code

Code:

Bitmap image = (Bitmap)Bitmap.FromFile(@"file.png");
byte[] bytes = File.ReadAllBytes(@"file.png");            
try
{
   using (image)
   {
      LuminanceSource source;
      source = new BitmapLuminanceSource(image);
      BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
      Result result = new MultiFormatReader().decode(bitmap);
      if (result != null)
      {
         //... code found
         var data = result.Text.Split(Environment.NewLine);
      }
      else
      {
         //... no code found
      }
     }
   }
   catch (Exception exception)
   {
      throw new Exception("Cannot decode the QR code " + exception.Message);
   }

Here the code throws the compile time error on BitmapLuminanceSource

The type or namespace name 'BitmapLuminanceSource' could not be found (are you missing a using directive or an assembly reference?

I have already installed ZXing.Net package here, I am not able to understand, why this class reference is not working here.

To make the code work, I have copied this class from git from here: https://github.com/micjahn/ZXing.Net/blob/master/Source/lib/BitmapLuminanceSource.cs

Praneet Nadkar
  • 823
  • 7
  • 16

2 Answers2

1

This class is not available in .NET Core version of the library (but will be, some time soon). micjahn, the author of ZXing.Net, advises to utilize some of the bindings listed here https://www.nuget.org/packages?q=ZXing.Net.Bindings I picked ZKWeb and my initialisation code looks like this:

var bitMap = (System.DrawingCore.Bitmap)System.DrawingCore.Bitmap.FromStream(stream);
var source = new ZXing.ZKWeb.BitmapLuminanceSource(bitMap);
var binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));

The original answer is here. Some additional information.

Ultimacho
  • 113
  • 1
  • 9
  • No need to deal with BitmapLuminanceSource, BinaryBitmap and HybridBinarizer. You can directly use the BarcodeReader class from the binding: `var bitMap = (System.DrawingCore.Bitmap)System.DrawingCore.Bitmap.FromStream(stream); var reader = new ZXing.ZKWeb.BarcodeReader(); var result = reader.Decode(bitMap); ` – Michael Sep 22 '20 at 04:58
  • your latest library generates error, Sir @Michael. I post it on https://stackoverflow.com/questions/76294211/zxing-net-0-16-0-hresult-0x80070057 – toha May 21 '23 at 15:57
  • done using philip's answer https://stackoverflow.com/questions/66250042/standard-way-to-read-barcode-after-upgrading-zxing-net-from-0-16-5-to-0-16-6 – toha May 22 '23 at 02:34
0

I was running into the same issue on Xamarin.Android, I installed the NuGet to my solution: https://www.nuget.org/packages/ZXing.Net/

I'd make sure all the references to the package files are correct. If you are using Visual Studio this is done for you, just be sure to add the Zxing.Net NuGet Package as the Zxing.Net.Mobile/Zxing.Net.Forms don't have the BitmapLuminanceSource method.

Joshua Wilkinson
  • 133
  • 2
  • 10