123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing;
- using System.ComponentModel;
- using FastReport.Barcode.QRCode;
- using FastReport.Utils;
- using System.Drawing.Drawing2D;
- namespace FastReport.Barcode
- {
- /// <summary>
- /// Specifies the QR code error correction level.
- /// </summary>
- public enum QRCodeErrorCorrection
- {
- /// <summary>
- /// L = ~7% correction.
- /// </summary>
- L,
- /// <summary>
- /// M = ~15% correction.
- /// </summary>
- M,
- /// <summary>
- /// Q = ~25% correction.
- /// </summary>
- Q,
- /// <summary>
- /// H = ~30% correction.
- /// </summary>
- H
- }
- /// <summary>
- /// Specifies the QR Code encoding.
- /// </summary>
- public enum QRCodeEncoding
- {
- /// <summary>
- /// UTF-8 encoding.
- /// </summary>
- UTF8,
- /// <summary>
- /// ISO 8859-1 encoding.
- /// </summary>
- ISO8859_1,
- /// <summary>
- /// Shift_JIS encoding.
- /// </summary>
- Shift_JIS,
- /// <summary>
- /// Windows-1251 encoding.
- /// </summary>
- Windows_1251,
- /// <summary>
- /// cp866 encoding.
- /// </summary>
- cp866
- }
- /// <summary>
- /// Generates the 2D QR code barcode.
- /// </summary>
- public class BarcodeQR : Barcode2DBase
- {
- #region Fields
- private QRCodeErrorCorrection errorCorrection;
- private QRCodeEncoding encoding;
- private bool quietZone;
- private ByteMatrix matrix;
- private const int PixelSize = 4;
- #endregion
- #region Properties
- /// <summary>
- /// Gets or sets the error correction.
- /// </summary>
- [DefaultValue(QRCodeErrorCorrection.L)]
- public QRCodeErrorCorrection ErrorCorrection
- {
- get { return errorCorrection; }
- set { errorCorrection = value; }
- }
- /// <summary>
- /// Gets or sets the encoding used for text conversion.
- /// </summary>
- [DefaultValue(QRCodeEncoding.UTF8)]
- public QRCodeEncoding Encoding
- {
- get { return encoding; }
- set { encoding = value; }
- }
- /// <summary>
- /// Gets or sets the value indicating that quiet zone must be shown.
- /// </summary>
- [DefaultValue(true)]
- public bool QuietZone
- {
- get { return quietZone; }
- set { quietZone = value; }
- }
- #endregion
- #region Private Methods
- private ErrorCorrectionLevel GetErrorCorrectionLevel()
- {
- switch (errorCorrection)
- {
- case QRCodeErrorCorrection.L:
- return ErrorCorrectionLevel.L;
- case QRCodeErrorCorrection.M:
- return ErrorCorrectionLevel.M;
- case QRCodeErrorCorrection.Q:
- return ErrorCorrectionLevel.Q;
- case QRCodeErrorCorrection.H:
- return ErrorCorrectionLevel.H;
- }
- return ErrorCorrectionLevel.L;
- }
- private string GetEncoding()
- {
- switch (encoding)
- {
- case QRCodeEncoding.UTF8:
- return "UTF-8";
- case QRCodeEncoding.ISO8859_1:
- return "ISO-8859-1";
- case QRCodeEncoding.Shift_JIS:
- return "Shift_JIS";
- case QRCodeEncoding.Windows_1251:
- return "Windows-1251";
- case QRCodeEncoding.cp866:
- return "cp866";
- }
- return "";
- }
- #endregion
- #region Public Methods
- /// <inheritdoc/>
- public override void Assign(BarcodeBase source)
- {
- base.Assign(source);
- BarcodeQR src = source as BarcodeQR;
- ErrorCorrection = src.ErrorCorrection;
- Encoding = src.Encoding;
- QuietZone = src.QuietZone;
- }
- internal override void Serialize(FastReport.Utils.FRWriter writer, string prefix, BarcodeBase diff)
- {
- base.Serialize(writer, prefix, diff);
- BarcodeQR c = diff as BarcodeQR;
- if (c == null || ErrorCorrection != c.ErrorCorrection)
- writer.WriteValue(prefix + "ErrorCorrection", ErrorCorrection);
- if (c == null || Encoding != c.Encoding)
- writer.WriteValue(prefix + "Encoding", Encoding);
- if (c == null || QuietZone != c.QuietZone)
- writer.WriteBool(prefix + "QuietZone", QuietZone);
- }
- internal override void Initialize(string text, bool showText, int angle, float zoom)
- {
- base.Initialize(text, showText, angle, zoom);
- matrix = QRCodeWriter.encode(base.text, 0, 0, GetErrorCorrectionLevel(), GetEncoding(), QuietZone);
- }
- internal override SizeF CalcBounds()
- {
- int textAdd = showText ? 18 : 0;
- return new SizeF(matrix.Width * PixelSize, matrix.Height * PixelSize + textAdd);
- }
- internal override void Draw2DBarcode(IGraphics g, float kx, float ky)
- {
- Brush dark = new SolidBrush(Color);
- for (int y = 0; y < matrix.Height; y++)
- {
- for (int x = 0; x < matrix.Width; x++)
- {
- if (matrix.get_Renamed(x, y) == 0)
- {
- g.FillRectangle(dark, new RectangleF(
- x * PixelSize * kx,
- y * PixelSize * ky,
- PixelSize * kx,
- PixelSize * ky
- ));
- }
- }
- }
- if (text.StartsWith("SPC"))
- {
- ErrorCorrection = QRCodeErrorCorrection.M;
- }
- dark.Dispose();
- }
- #endregion
- /// <summary>
- /// Initializes a new instance of the <see cref="BarcodeQR"/> class with default settings.
- /// </summary>
- public BarcodeQR()
- {
- Encoding = QRCodeEncoding.UTF8;
- QuietZone = true;
- }
- }
- }
|