MCPcopy
hub / github.com/zxing-js/library / terminateBits

Method terminateBits

src/core/qrcode/encoder/Encoder.ts:310–335  ·  view source on GitHub ↗

* Terminate bits as described in 8.4.8 and 8.4.9 of JISX0510:2004 (p.24).

(numDataBytes: number /*int*/, bits: BitArray)

Source from the content-addressed store, hash-verified

308 * Terminate bits as described in 8.4.8 and 8.4.9 of JISX0510:2004 (p.24).
309 */
310 public static terminateBits(numDataBytes: number /*int*/, bits: BitArray): void /*throws WriterException*/ {
311 const capacity = numDataBytes * 8;
312 if (bits.getSize() > capacity) {
313 throw new WriterException('data bits cannot fit in the QR Code' + bits.getSize() + ' > ' +
314 capacity);
315 }
316 for (let i = 0; i < 4 && bits.getSize() < capacity; ++i) {
317 bits.appendBit(false);
318 }
319 // Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details.
320 // If the last byte isn't 8-bit aligned, we'll add padding bits.
321 const numBitsInLastByte = bits.getSize() & 0x07;
322 if (numBitsInLastByte > 0) {
323 for (let i = numBitsInLastByte; i < 8; i++) {
324 bits.appendBit(false);
325 }
326 }
327 // If we have more space, we'll fill the space with padding patterns defined in 8.4.9 (p.24).
328 const numPaddingBytes = numDataBytes - bits.getSizeInBytes();
329 for (let i = 0; i < numPaddingBytes; ++i) {
330 bits.appendBits((i & 0x01) === 0 ? 0xEC : 0x11, 8);
331 }
332 if (bits.getSize() !== capacity) {
333 throw new WriterException('Bits size does not equal capacity');
334 }
335 }
336
337 /**
338 * Get number of data bytes and number of error correction bytes for block id "blockID". Store

Callers 2

encodeMethod · 0.95
Encoder.spec.tsFile · 0.80

Calls 4

appendBitMethod · 0.80
getSizeInBytesMethod · 0.80
appendBitsMethod · 0.80
getSizeMethod · 0.45

Tested by

no test coverage detected