view mupdf-source/thirdparty/zxing-cpp/wrappers/dotnet/ZXingCpp.DemoWriter/Program.cs @ 46:7ee69f120f19 default tip

>>>>> tag v1.26.5+1 for changeset b74429b0f5c4
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 11 Oct 2025 17:17:30 +0200
parents b50eed0cc0ef
children
line wrap: on
line source

/*
* Copyright 2024 Axel Waggershauser
*/
// SPDX-License-Identifier: Apache-2.0

using System.Collections.Generic;
using SkiaSharp;
using ZXingCpp;

public static class SkBitmapBarcodeWriter
{
	public static SKBitmap Write(Barcode barcode, WriterOptions? opts = null)
	{
		var img = barcode.ToImage(opts);
		var info = new SKImageInfo(img.Width, img.Height, SKColorType.Gray8);
		var res = new SKBitmap();
		res.InstallPixels(info, img.Data, img.Width, (IntPtr _, object _) => img.Dispose());
		return res;
	}

	public static SKBitmap ToSKBitmap(this Barcode barcode, WriterOptions? opts = null) => Write(barcode, opts);
}

public class Program
{
	public static void Main(string[] args)
	{
		var (format, text, fn) = (args[0], args[1], args[2]);

		var bc = new Barcode(text, Barcode.FormatFromString(format));

		var img = bc.ToImage();
		Console.WriteLine($"{img.Data}, {img.Width}, {img.Height}, {img.ToArray()}");

		if (fn.EndsWith(".svg"))
			File.WriteAllText(fn, bc.ToSVG());
		else
			using (SKBitmap skb = bc.ToSKBitmap(new WriterOptions(){Scale = 5})) {
				skb.Encode(new SKFileWStream(args[2]), SKEncodedImageFormat.Png, 100);
			}
	}

}