-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
72 lines (61 loc) · 2.4 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using ZXing;
namespace qr;
internal static class Program
{
private static void Main()
{
var quads = Enumerable
.Range(1, 9)
.Select(n => Quad.LoadFromFolder($"data/{n}"))
.ToList();
var listsOfParts = new List<List<Part>>();
GenerateListsOfParts(quads, 0, new HashSet<int>(), new List<Part>(), listsOfParts);
Parallel.ForEach(
Enumerable.Range(0, listsOfParts.Count).Select(n => (n, listsOfParts[n])),
pair => ConstructAndDecodeQrCode($"out/{pair.Item1:D3}.png", pair.Item2));
}
private static void ConstructAndDecodeQrCode(string path, List<Part> listOfParts)
{
var qrCodeImage = new Image<Rgb24>(300, 300, new Rgb24(255, 255, 255));
for (var y = 0; y < 3; y++)
{
for (var x = 0; x < 3; x++)
{
var part = listOfParts[x + y * 3];
qrCodeImage.Mutate(context => context.DrawImage(part.Image, new Point(x * 100, y * 100), 1.0f));
}
}
// comment to skip saving QR code to /out and speed up function
qrCodeImage.Save(path);
var data = new byte[qrCodeImage.Width * qrCodeImage.Height * 3];
qrCodeImage.CopyPixelDataTo(new Span<byte>(data));
var result = new BarcodeReaderGeneric().Decode(data, qrCodeImage.Width, qrCodeImage.Height, RGBLuminanceSource.BitmapFormat.RGB24);
if(result != null)
Console.WriteLine(result);
}
private static void GenerateListsOfParts(IReadOnlyList<Quad> quads, int id, ISet<int> takenNumbers, IList<Part> parts, ICollection<List<Part>> listsOfParts)
{
if (id == quads.Count)
{
listsOfParts.Add(new List<Part>(parts));
return;
}
var quad = quads[id];
foreach (var (number, quadParts) in quad._parts)
{
if(takenNumbers.Contains(number))
continue;
takenNumbers.Add(number);
foreach (var part in quadParts)
{
parts.Add(part);
GenerateListsOfParts(quads, id + 1, takenNumbers, parts, listsOfParts);
parts.RemoveAt(parts.Count - 1);
}
takenNumbers.Remove(number);
}
}
}