Program.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright 2024 Axel Waggershauser
  3. */
  4. // SPDX-License-Identifier: Apache-2.0
  5. using System.Collections.Generic;
  6. using SkiaSharp;
  7. using ZXingCpp;
  8. public static class SkBitmapBarcodeWriter
  9. {
  10. public static SKBitmap Write(Barcode barcode, WriterOptions? opts = null)
  11. {
  12. var img = barcode.ToImage(opts);
  13. var info = new SKImageInfo(img.Width, img.Height, SKColorType.Gray8);
  14. var res = new SKBitmap();
  15. res.InstallPixels(info, img.Data, img.Width, (IntPtr _, object _) => img.Dispose());
  16. return res;
  17. }
  18. public static SKBitmap ToSKBitmap(this Barcode barcode, WriterOptions? opts = null) => Write(barcode, opts);
  19. }
  20. public class Program
  21. {
  22. public static void Main(string[] args)
  23. {
  24. var (format, text, fn) = (args[0], args[1], args[2]);
  25. var bc = new Barcode(text, Barcode.FormatFromString(format));
  26. var img = bc.ToImage();
  27. Console.WriteLine($"{img.Data}, {img.Width}, {img.Height}, {img.ToArray()}");
  28. if (fn.EndsWith(".svg"))
  29. File.WriteAllText(fn, bc.ToSVG());
  30. else
  31. using (SKBitmap skb = bc.ToSKBitmap(new WriterOptions(){Scale = 5})) {
  32. skb.Encode(new SKFileWStream(args[2]), SKEncodedImageFormat.Png, 100);
  33. }
  34. }
  35. }