44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using Android.Graphics;
|
|
using QRCoder;
|
|
using Exception = Java.Lang.Exception; // QRCoder-ImageSharp else it crashes
|
|
|
|
namespace DemoAndroid;
|
|
|
|
[Activity(Label = "@string/app_name", MainLauncher = true)]
|
|
public class MainActivity : Activity
|
|
{
|
|
private string _text = "";
|
|
private ImageView? _ivQr;
|
|
|
|
protected override void OnCreate(Bundle? savedInstanceState)
|
|
{
|
|
base.OnCreate(savedInstanceState);
|
|
|
|
// Set our view from the "main" layout resource
|
|
SetContentView(Resource.Layout.activity_main);
|
|
|
|
var etInput = FindViewById<EditText>(Resource.Id.etInput) ?? throw new Exception("EditText #etInput not found");
|
|
_ivQr = FindViewById<ImageView>(Resource.Id.ivQR) ?? throw new Exception("ImageView #ivQR not found");;
|
|
|
|
etInput.TextChanged += (sender, args) =>
|
|
{
|
|
_text = args.Text?.ToString() ?? string.Empty;
|
|
|
|
if (!string.IsNullOrEmpty(_text))
|
|
{
|
|
CreateQr();
|
|
}
|
|
};
|
|
}
|
|
|
|
private void CreateQr()
|
|
{
|
|
using var qrGenerator = new QRCodeGenerator();
|
|
using var qrCodeData = qrGenerator.CreateQrCode(_text, QRCodeGenerator.ECCLevel.Q);
|
|
using var qrCode = new PngByteQRCode(qrCodeData);
|
|
|
|
var qrCodeImage = qrCode.GetGraphic(20);
|
|
var javaBitmap = BitmapFactory.DecodeByteArray(qrCodeImage, 0, qrCodeImage.Length);
|
|
_ivQr?.SetImageBitmap(javaBitmap);
|
|
}
|
|
} |