If you are wondering if you can
generate barcodes in .NET MAUI, then wonder no more. In this video
we're going to learn how to generate barcodes with the ZXing plugin. In a previous video, which I highly
recommend you watch as well. We've seen how to to scan barcode. So
that's basically open up your camera, point your camera to
that barcode QR code. Ean one dimensional, two dimensional, can be
any barcode and just scan it with your .NET MAUI application. Now
we're going to look at kind of like the inverse thing where we're
going to generate barcode. So maybe you have this little piece of data that you
want to show to a user. Maybe you have some kind of link generator, I
don't know. Generate a QR code from that. Let someone else point their camera at
your QR code on the screen and they can navigate from there. So we're
going to learn how to, how easy it is basically to implement generating
barcodes in your .NET MAUI application. And it is the exact same package that
we've seen in the other video, which I highly
recommend you watch about scanning the barcodes. So it should be really
easy to get started with this. Let's check it out. So here we are in
Visual Studio 2022. I just whipped up a .NET MAUI
application. Just a project File, New in Visual Studio. I didn't
change anything. This is just a File, New template. I did one thing, I installed the NuGet
package already. So let's go over to our Solution Explorer and right click
here on the project Manage NuGet packages. And here under Browse
you want to look for ZXing.Net. Maui. That's the one that you're
after. It should pop up here. It's a prerelease at the time of
recording. So make sure that you get the latest version. And check that include
pre release. Checkbox here so that it shows up.
This version is the latest at the time of recording. If
you're watching this, it might be a newer one already. Just take the latest one.
That should work with that installed. There is one other thing
that we need to do. Go over to the Solution Explorer again and open
your MauiProgram.cs. And inside of there you want to use
your generic host builder and right behind your UseMauiApp. Or
the order doesn't really matter. You want to add
the UseBarcodeReader. Now mind you, it says barcode reader
but you also need it for the generation. So maybe because this
is still a preview, maybe Jon, who is creating this plugin,
will decide like no, this is not the right name.
I'm going to change it a bit. So be aware if this
changed. But for now it's used barcode reader. Also the
green squiggles in here is something that needs to be fixed on the library side.
So don't worry about that for now. Okay? We got that. If it
doesn't add the using here at the top
automatically using ZXing.Net.Maui. Make sure that you add it for
UseBarcodeReader right here. So now that we've
got that in place it's actually time to use the barcode generation and to do
that I like to do an example. You can totally do
it in C# code if that's what you want as well. We want to add this
little xmlns here first. And I want to name this
zxing. So this can be any name but zxing seems to
make sense. So I'm going to add that and whenever
you type in here the IntelliSense is going to help
us and you're going to have to look for that ZXing.Net.Maui.Controls. That is
the namespace where all the controls live. So let me
just get that in here and I'm going to replace this image with our
actual barcode. So zxing. I can type that. Now as our
XML namespace we have the BarcodeGeneratorView,
that's the one we're going to see here. And the CameraBarcodeReaderView which is
what you can see in the other video. And a generic
CameraView. We're not going to worry about that for now. So let's get
this BarcodeGeneratorView. And this only has a couple of
properties basically which is the Format. So here you can specify
the format that you want to generate which supports a lot: Aztec, code 39, code 93, EAN 13. I haven't even heard of most of these.
And here you have the QR code. Let's just start with that
one. And you can specify a value. So with this value for a QR code it
can be anything. So I don't know, let's do something
crazy like: please subscribe. Maybe you have a favorite channel with
a favorite YouTuber who tells you about your favorite .NET MAUI content
that you want to subscribe to. Do it now. So please subscribe to my channel.
Let's just put that in here. It doesn't really matter. And for styling you can
do some other things. You can say background color, you can specify a
background color that you want or maybe more interesting is a foreground
color. Let's just make it, I don't know, red so it will show up
nice and red. And this is basically all that you can do. Well
probably it's nice to also specify a width and a height so that we don't
make it fullscreen or whatever it's going to do by
default. So let's make this 200 by 200. Actually while checking this and
running it on Windows, on Windows there seems to be a little bug right
now where it will just take the full height. So you probably
want to, I don't know, put that in some kind of layout and kind of
like size the image accordingly. It will probably be fixed with a next
release. But for now on Android we're just
going to see this QR code it should show up 200 by 200 and we have
this nice pretty QR code and I'll wait here
for a second. Just whip out your phone and get that QR code
scanner in here and you will see that the value is please subscribe now
inside of this QR code. So that's basically how easy it
is to generate QR codes with this package. One little note before I
let you go. If you are going to do EAN 13 or probably it's
true for some other formats as well, then you want to make sure
that the values actually line up. So what you want to do is here format. So we have EAN 13. I think it's going
to give you some runtime exception as well. So you want to do
012-345-6789 which is like 10 characters right?
But it's EAN 13 so I think it will say something like
hey, I got 10 characters now but I expect 12 or 13 if there
is some kind of checksum character in there. So you want
to make sure that you have the good amount of characters in
here because else there will be some validation
thing and it will throw you an exception. And that is how you
generate barcodes in .NET MAUI. Well, short and sweet video. It's just
install the NuGet package, add in that initialization line
and you can start generating barcodes. Now of course the properties that
you've seen here are all bindable so you can have dynamic values put in here
and generate all kinds of QR codes and all kinds of
other formats of barcodes in here inside of your app. This works
for Android, iOS, macOS and Windows. It is still a
preview package. There might be a couple of bugs but we'll figure it
out. Please report them on the plugin repository so that we
can start working on fixing them. Thank you so much for watching.
In one of my videos, please click the like button if you've actually liked
this video so that it will spread out to more people throughout the YouTube
universe. And if you are wondering about more .NET MAUI
content, check out this playlist right here for more .NET
MAUI content. If you're still looking for that video on scanning barcodes,
that's right here and click this button to make sure that you're subscribed and
I'll be seeing you for the next video.