BarcodeAztec.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.ComponentModel;
  6. using FastReport.Barcode.Aztec;
  7. using FastReport.Utils;
  8. namespace FastReport.Barcode
  9. {
  10. /// <summary>
  11. /// Generates the 2D Aztec barcode.
  12. /// </summary>
  13. public class BarcodeAztec : Barcode2DBase
  14. {
  15. BitMatrix matrix;
  16. int errorCorrectionPercent;
  17. const int PIXEL_SIZE = 4;
  18. /// <summary>
  19. /// Gets or sets the error correction percent.
  20. /// </summary>
  21. [DefaultValue(33)]
  22. public int ErrorCorrectionPercent
  23. {
  24. get { return errorCorrectionPercent; }
  25. set { errorCorrectionPercent = (value < 5) ? 5 : ((value > 95) ? 95 : value); }
  26. }
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="BarcodeAztec"/> class with default settings.
  29. /// </summary>
  30. public BarcodeAztec()
  31. {
  32. ErrorCorrectionPercent = 33;
  33. }
  34. internal override void Initialize(string text, bool showText, int angle, float zoom)
  35. {
  36. base.Initialize(text, showText, angle, zoom);
  37. matrix = Encoder.encode(System.Text.Encoding.ASCII.GetBytes(text), ErrorCorrectionPercent, 0).Matrix;
  38. }
  39. internal override SizeF CalcBounds()
  40. {
  41. int textAdd = showText ? 18 : 0;
  42. return new SizeF(matrix.Width * PIXEL_SIZE, matrix.Height * PIXEL_SIZE + textAdd);
  43. }
  44. internal override void Draw2DBarcode(IGraphics g, float kx, float ky)
  45. {
  46. Brush light = Brushes.White;
  47. Brush dark = new SolidBrush(Color);
  48. for (int y = 0; y < matrix.Height; y++)
  49. {
  50. for (int x = 0; x < matrix.Width; x++)
  51. {
  52. bool b = matrix.getRow(y, null)[x];
  53. Brush brush = /*b == true ?*/ dark /*: light*/;
  54. if (b == true)
  55. g.FillRectangle(brush, x * PIXEL_SIZE * kx, y * PIXEL_SIZE * ky,
  56. PIXEL_SIZE * kx, PIXEL_SIZE * ky);
  57. }
  58. }
  59. dark.Dispose();
  60. }
  61. /// <inheritdoc/>
  62. public override void Assign(BarcodeBase source)
  63. {
  64. base.Assign(source);
  65. BarcodeAztec src = source as BarcodeAztec;
  66. ErrorCorrectionPercent = src.ErrorCorrectionPercent;
  67. }
  68. internal override void Serialize(FastReport.Utils.FRWriter writer, string prefix, BarcodeBase diff)
  69. {
  70. base.Serialize(writer, prefix, diff);
  71. BarcodeAztec c = diff as BarcodeAztec;
  72. if (c == null || ErrorCorrectionPercent != c.ErrorCorrectionPercent)
  73. writer.WriteInt(prefix + "ErrorCorrection", ErrorCorrectionPercent);
  74. }
  75. }
  76. }