1 module ithox.qrcode.encoder.qrcode; 2 3 import ithox.qrcode.common.errorcorrectionlevel; 4 import ithox.qrcode.common.mode; 5 import ithox.qrcode.common.qrcodeversion; 6 import ithox.qrcode.common.bitmatrix; 7 import ithox.qrcode.common.bitarray; 8 import ithox.qrcode.encoder.bytematrix; 9 10 /** 11 * QR code. 12 * @author donglei 13 */ 14 class QrCode 15 { 16 /** 17 * Number of possible mask patterns. 18 */ 19 enum NUM_MASK_PATTERNS = 8; 20 21 protected 22 { 23 Mode _mode; ///Mode of the QR code. 24 ErrorCorrectionLevel _errorCorrectionLevel; /// EC level of the QR code. 25 26 QrCodeVersion _qrVersion; ///Version of the QR code. 27 28 int _maskPattern = -1; ///Mask pattern of the QR code. 29 30 ByteMatrix _matrix; ///Matrix of the QR code. 31 } 32 33 public @property Mode mode() 34 { 35 return _mode; 36 } 37 38 public @property void mode(Mode _m) 39 { 40 _mode = _m; 41 } 42 43 public @property ErrorCorrectionLevel errorCorrectionLevel() 44 { 45 return _errorCorrectionLevel; 46 } 47 48 public @property void errorCorrectionLevel(ErrorCorrectionLevel _m) 49 { 50 _errorCorrectionLevel = _m; 51 } 52 53 public @property QrCodeVersion qrVersion() 54 { 55 return _qrVersion; 56 } 57 58 public @property void qrVersion(QrCodeVersion _m) 59 { 60 _qrVersion = _m; 61 } 62 63 public @property int maskPattern() 64 { 65 return _maskPattern; 66 } 67 68 public @property void maskPattern(int _m) 69 { 70 _maskPattern = _m; 71 } 72 73 public @property ByteMatrix matrix() 74 { 75 return _matrix; 76 } 77 78 public @property void matrix(ByteMatrix _m) 79 { 80 _matrix = _m; 81 } 82 83 /** 84 * Validates whether a mask pattern is valid. 85 * 86 * @param integer $maskPattern 87 * @return boolean 88 */ 89 public static bool isValidMaskPattern(int maskPattern) 90 { 91 return maskPattern > 0 && maskPattern < NUM_MASK_PATTERNS; 92 } 93 94 /** 95 * Returns a string representation of the QR code. 96 * 97 * @return string 98 */ 99 public override string toString() 100 { 101 import std.array, std.conv; 102 103 Appender!string result = appender!string(); 104 result.put("<<\n"); 105 result.put(" mode: "); 106 result.put(this.mode.toString()); 107 result.put("\n"); 108 result.put(" ecLevel: "); 109 result.put(to!string(getOrdinalErrorCorrectionLevel(this.errorCorrectionLevel))); 110 result.put("\n"); 111 result.put(" version: "); 112 result.put(this.qrVersion.toString()); 113 result.put("\n"); 114 result.put(" maskPattern: "); 115 result.put(to!string(this.maskPattern)); 116 result.put("\n"); 117 if (this.matrix is null) 118 { 119 result.put(" matrix: null\n"); 120 } 121 else 122 { 123 result.put(" matrix:\n"); 124 result.put(this.matrix.toString()); 125 } 126 result.put(">>\n"); 127 return result.data; 128 } 129 }