micjahn/ZXing.Net

Xamarin - ZXing.Net.Binding.SkiaSharp - NullReferenceException at at ZXing.Common.StringUtils..cctor ()

martienpoels-bank2loyalty opened this issue · 7 comments

If I use the SkiaSharp.BarcodeWriter with Format = BarcodeFormat.QR_CODE
the below code excepts on a null reference exception.

exception = The type initializer for 'ZXing.QrCode.Internal.Encoder' threw an exception.
at ZXing.QrCode.QRCodeWriter.encode (System.String contents, ZXing.BarcodeFormat format, System.Int32 width, System.Int32 height, System.Collections.Generic.IDictionary2[TKey,TValue] hints) [0x00120] in <6f4f8ea0232543238f2e06ec9bd0c0fd>:0 at ZXing.MultiFormatWriter.encode (System.String contents, ZXing.BarcodeFormat format, System.Int32 width, System.Int32 height, System.Collections.Generic.IDictionary2[TKey,TValue] hints) [0x0003a] in <6f4f8ea0232543238f2e06ec9bd0c0fd>:0
at ZXing.BarcodeWriterGeneric.Encode (System.String contents) [0x0002f] in <6f4f8ea0232543238f2e06ec9bd0c0fd>:0
at ZXing.BarcodeWriter`1[TOutput].Write (System.String contents) [0x00013] in <6f4f8ea0232543238f2e06ec9bd0c0fd>:0
TypeName: ZXing.QrCode.Internal.Encoder

Inner exception: The type initializer for 'ZXing.Common.StringUtils' threw an exception.
TypeName: ZXing.Common.StringUtils

try
{
	var writer = new ZXing.SkiaSharp.BarcodeWriter()
	{
		Format = BarcodeFormat.QR_CODE
	};
	var options = new ZXing.Common.EncodingOptions()
	{
		Width = 100,
		Height = 100,
	};
	var image = writer.Write("123456");
}
catch (Exception ex)
{
	Console.WriteLine(ex.Message);
}

It only happens when I select format QR-code.

The below code excepts too with the innerexception StringUtils. on type ZXing.QrCode.Internal.Encoder

try
{
	var pixels = new BarcodeWriterPixelData()
	{
		Format = BarcodeFormat.QR_CODE,
		Options = new EncodingOptions()
		{
			Width = 200,
			Height = 200,
		}
	};

	var image = pixels.Write("123456");
}
catch (Exception ex)
{
	Console.WriteLine(ex.Message);
}

Sorry I made a typo in the version in the mail ZXing.Net was 0.16.7 instead of 0.16.6...

I can add to it that half a year or so with older version of Xamarin.Forms etc it was working, due to another problem I had to update my android app (Android 12 issue), and now all of a sudden encoding of QR codes failed to work. Very inconvenient of course because now I can not fix the other issue because of this problem. If I can assist somewhere let me know.

Ok, I will give it a shot.

The exception is gone!
From home I can not test the application because it interacts with bluetooth with another device, but this looks good! The testcode now passes without problems.
Pfjew, that's a relieve. Thank you very much for guiding me to the solution.

I will let you know if the whole application is working now tomorrow.

I can confirm by loading the ZXing.Net newer package 0.16.8, this fixes the exception occuring by trying to encode qr-codes.

Thank you for helping with this matter.
A future update of the package ZXing.Net.Bindings.SkiaSharp to include the newer ZXing.Net version will resolve this issue.
For now this workaround does the job.