(content: string,
ecLevel: ErrorCorrectionLevel,
hints: Map<EncodeHintType, any> = null)
| 81 | // } |
| 82 | |
| 83 | public static encode(content: string, |
| 84 | ecLevel: ErrorCorrectionLevel, |
| 85 | hints: Map<EncodeHintType, any> = null): QRCode /*throws WriterException*/ { |
| 86 | |
| 87 | // Determine what character encoding has been specified by the caller, if any |
| 88 | let encoding: string = Encoder.DEFAULT_BYTE_MODE_ENCODING; |
| 89 | const hasEncodingHint: boolean = hints !== null && undefined !== hints.get(EncodeHintType.CHARACTER_SET); |
| 90 | if (hasEncodingHint) { |
| 91 | encoding = hints.get(EncodeHintType.CHARACTER_SET).toString(); |
| 92 | } |
| 93 | |
| 94 | // Pick an encoding mode appropriate for the content. Note that this will not attempt to use |
| 95 | // multiple modes / segments even if that were more efficient. Twould be nice. |
| 96 | const mode: Mode = this.chooseMode(content, encoding); |
| 97 | |
| 98 | // This will store the header information, like mode and |
| 99 | // length, as well as "header" segments like an ECI segment. |
| 100 | const headerBits = new BitArray(); |
| 101 | |
| 102 | // Append ECI segment if applicable |
| 103 | if (mode === Mode.BYTE && (hasEncodingHint || Encoder.DEFAULT_BYTE_MODE_ENCODING !== encoding)) { |
| 104 | const eci = CharacterSetECI.getCharacterSetECIByName(encoding); |
| 105 | if (eci !== undefined) { |
| 106 | this.appendECI(eci, headerBits); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // (With ECI in place,) Write the mode marker |
| 111 | this.appendModeInfo(mode, headerBits); |
| 112 | |
| 113 | // Collect data within the main segment, separately, to count its size if needed. Don't add it to |
| 114 | // main payload yet. |
| 115 | const dataBits = new BitArray(); |
| 116 | this.appendBytes(content, mode, dataBits, encoding); |
| 117 | |
| 118 | let version: Version; |
| 119 | if (hints !== null && undefined !== hints.get(EncodeHintType.QR_VERSION)) { |
| 120 | const versionNumber = Number.parseInt(hints.get(EncodeHintType.QR_VERSION).toString(), 10); |
| 121 | version = Version.getVersionForNumber(versionNumber); |
| 122 | const bitsNeeded = this.calculateBitsNeeded(mode, headerBits, dataBits, version); |
| 123 | if (!this.willFit(bitsNeeded, version, ecLevel)) { |
| 124 | throw new WriterException('Data too big for requested version'); |
| 125 | } |
| 126 | } else { |
| 127 | version = this.recommendVersion(ecLevel, mode, headerBits, dataBits); |
| 128 | } |
| 129 | |
| 130 | const headerAndDataBits = new BitArray(); |
| 131 | headerAndDataBits.appendBitArray(headerBits); |
| 132 | // Find "length" of main segment and write it |
| 133 | const numLetters = mode === Mode.BYTE ? dataBits.getSizeInBytes() : content.length; |
| 134 | this.appendLengthInfo(numLetters, version, mode, headerAndDataBits); |
| 135 | // Put data together into the overall payload |
| 136 | headerAndDataBits.appendBitArray(dataBits); |
| 137 | |
| 138 | const ecBlocks: ECBlocks = version.getECBlocksForLevel(ecLevel); |
| 139 | const numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords(); |
| 140 |
nothing calls this directly
no test coverage detected