|
1 | 1 | using Microsoft.UI.Xaml.Media.Imaging; |
2 | 2 | using System.Drawing; |
3 | 3 | using System.Drawing.Imaging; |
| 4 | +using System.Globalization; |
4 | 5 | using Windows.Storage; |
5 | 6 | using ZXing; |
6 | 7 | using ZXing.Common; |
@@ -46,6 +47,57 @@ public static WriteableBitmap GetQrCodeBitmapFromText(string text, ErrorCorrecti |
46 | 47 | return bitmapImage; |
47 | 48 | } |
48 | 49 |
|
| 50 | + /// <summary> |
| 51 | + /// Calculate the smallest side of a QR code based on the distance between the camera and the QR code |
| 52 | + /// </summary> |
| 53 | + /// <param name="distance">Distance of camera from QR Code (in)</param> |
| 54 | + /// <param name="numberOfBlocks">Number of blocks in the QR Code (Version)</param> |
| 55 | + /// <returns>The smallest size (in) of a QR Code scanning at the given distance</returns> |
| 56 | + public static double SmallestCodeSide(double distance, int numberOfBlocks) |
| 57 | + { |
| 58 | + // TODO when margin or padding can be set in settings |
| 59 | + // account for padding on both sides |
| 60 | + int padding = 2 * 2; |
| 61 | + |
| 62 | + double blockSize = (distance + 2.721) / 1759.1; |
| 63 | + double codeSize = blockSize * (numberOfBlocks + padding); |
| 64 | + return codeSize; |
| 65 | + } |
| 66 | + |
| 67 | + public static double ContrastRatioLossFrac(double constrastRatio) |
| 68 | + { |
| 69 | + double x1 = 21; |
| 70 | + double y1 = 1; |
| 71 | + double x2 = 2.5; |
| 72 | + double y2 = 0.8; |
| 73 | + |
| 74 | + double slope = (y2 - y1) / (x2 - x1); |
| 75 | + double yIntercept = y1 - slope * x1; |
| 76 | + |
| 77 | + return slope * constrastRatio + yIntercept; |
| 78 | + } |
| 79 | + |
| 80 | + public static string SmallestSideWithUnits(double distance, int numberOfBlocks, Windows.UI.Color foreground, Windows.UI.Color background) |
| 81 | + { |
| 82 | + bool isMetric = RegionInfo.CurrentRegion.IsMetric; |
| 83 | + double smallestSide = SmallestCodeSide(distance, numberOfBlocks); |
| 84 | + |
| 85 | + double contrastRatio = ColorHelpers.GetContrastRatio(foreground, background); |
| 86 | + |
| 87 | + if (contrastRatio < 2.5) |
| 88 | + return "Color contrast too low"; |
| 89 | + |
| 90 | + double fractionalLoss = ContrastRatioLossFrac(contrastRatio); |
| 91 | + |
| 92 | + smallestSide /= fractionalLoss; |
| 93 | + |
| 94 | + if (!isMetric) |
| 95 | + return $"{smallestSide:F2} x {smallestSide:F2} in"; |
| 96 | + |
| 97 | + double smallestSideCm = smallestSide * 2.54; |
| 98 | + return $"{smallestSideCm:F2} x {smallestSideCm:F2} cm"; |
| 99 | + } |
| 100 | + |
49 | 101 | public static SvgImage GetSvgQrCodeForText(string text, ErrorCorrectionLevel correctionLevel, System.Drawing.Color foreground, System.Drawing.Color background) |
50 | 102 | { |
51 | 103 | SvgRenderer svgRenderer = new() |
|
0 commit comments