You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
3.1 KiB

  1. #if NET40 || NET461
  2. using System;
  3. using System.Windows;
  4. using System.Windows.Media;
  5. namespace Apewer.Internals.QrCode
  6. {
  7. internal class XamlQRCode : AbstractQRCode, IDisposable
  8. {
  9. /// <summary>
  10. /// Constructor without params to be used in COM Objects connections
  11. /// </summary>
  12. public XamlQRCode() { }
  13. public XamlQRCode(QRCodeData data) : base(data) { }
  14. public DrawingImage GetGraphic(int pixelsPerModule)
  15. {
  16. return this.GetGraphic(pixelsPerModule, true);
  17. }
  18. public DrawingImage GetGraphic(int pixelsPerModule, bool drawQuietZones)
  19. {
  20. var drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
  21. var viewBox = new Size(pixelsPerModule * drawableModulesCount, pixelsPerModule * drawableModulesCount);
  22. return this.GetGraphic(viewBox, new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.White), drawQuietZones);
  23. }
  24. public DrawingImage GetGraphic(Size viewBox, bool drawQuietZones = true)
  25. {
  26. return this.GetGraphic(viewBox, new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.White), drawQuietZones);
  27. }
  28. public DrawingImage GetGraphic(int pixelsPerModule, string darkColorHex, string lightColorHex, bool drawQuietZones = true)
  29. {
  30. var drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
  31. var viewBox = new Size(pixelsPerModule * drawableModulesCount, pixelsPerModule * drawableModulesCount);
  32. return this.GetGraphic(viewBox, new SolidColorBrush((Color)ColorConverter.ConvertFromString(darkColorHex)), new SolidColorBrush((Color)ColorConverter.ConvertFromString(lightColorHex)), drawQuietZones);
  33. }
  34. public DrawingImage GetGraphic(Size viewBox, Brush darkBrush, Brush lightBrush, bool drawQuietZones = true)
  35. {
  36. var drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
  37. var qrSize = Math.Min(viewBox.Width, viewBox.Height);
  38. var unitsPerModule = qrSize / drawableModulesCount;
  39. var offsetModules = drawQuietZones ? 0 : 4;
  40. DrawingGroup drawing = new DrawingGroup();
  41. drawing.Children.Add(new GeometryDrawing(lightBrush, null, new RectangleGeometry(new Rect(new Point(0, 0), new Size(qrSize, qrSize)))));
  42. var group = new GeometryGroup();
  43. double x = 0d, y = 0d;
  44. for (int xi = offsetModules; xi < drawableModulesCount; xi++)
  45. {
  46. y = 0d;
  47. for (int yi = offsetModules; yi < drawableModulesCount; yi++)
  48. {
  49. if (this.QrCodeData.ModuleMatrix[yi][xi])
  50. {
  51. group.Children.Add(new RectangleGeometry(new Rect(x, y, unitsPerModule, unitsPerModule)));
  52. }
  53. y += unitsPerModule;
  54. }
  55. x += unitsPerModule;
  56. }
  57. drawing.Children.Add(new GeometryDrawing(darkBrush, null, group));
  58. return new DrawingImage(drawing);
  59. }
  60. }
  61. }
  62. #endif