@@ -303,11 +303,19 @@ void AddCodeWordBlocks(int blockNum, int blocksInGroup, int codewordsInGroup, Bi
303
303
304
304
private static readonly BitArray _getFormatGenerator = new BitArray ( new bool [ ] { true , false , true , false , false , true , true , false , true , true , true } ) ;
305
305
private static readonly BitArray _getFormatMask = new BitArray ( new bool [ ] { true , false , true , false , true , false , false , false , false , false , true , false , false , true , false } ) ;
306
+ /// <summary>
307
+ /// Generates a BitArray containing the format string for a QR code based on the error correction level and mask pattern version.
308
+ /// The format string includes the error correction level, mask pattern version, and error correction coding.
309
+ /// </summary>
310
+ /// <param name="level">The error correction level to be encoded in the format string.</param>
311
+ /// <param name="maskVersion">The mask pattern version to be encoded in the format string.</param>
312
+ /// <returns>A BitArray containing the 15-bit format string used in QR code generation.</returns>
306
313
private static BitArray GetFormatString ( ECCLevel level , int maskVersion )
307
314
{
308
315
var fStrEcc = new BitArray ( 15 ) ; // Total length including space for mask version and padding
309
316
WriteEccLevelAndVersion ( ) ;
310
317
318
+ // Apply the format generator polynomial to add error correction to the format string.
311
319
int index = 0 ;
312
320
int count = 15 ;
313
321
TrimLeadingZeros ( fStrEcc , ref index , ref count ) ;
@@ -317,10 +325,16 @@ private static BitArray GetFormatString(ECCLevel level, int maskVersion)
317
325
fStrEcc [ index + i ] ^= _getFormatGenerator [ i ] ;
318
326
TrimLeadingZeros ( fStrEcc , ref index , ref count ) ;
319
327
}
328
+
329
+ // Align bits with the start of the array.
320
330
ShiftTowardsBit0 ( fStrEcc , index ) ;
331
+
332
+ // Prefix the error correction bits with the ECC level and version number.
321
333
fStrEcc . Length = 10 + 5 ;
322
334
ShiftAwayFromBit0 ( fStrEcc , ( 10 - count ) + 5 ) ;
323
335
WriteEccLevelAndVersion ( ) ;
336
+
337
+ // XOR the format string with a predefined mask to add robustness against errors.
324
338
fStrEcc . Xor ( _getFormatMask ) ;
325
339
return fStrEcc ;
326
340
@@ -341,6 +355,8 @@ void WriteEccLevelAndVersion()
341
355
default : // M: 00
342
356
break ;
343
357
}
358
+
359
+ // Insert the 3-bit mask version directly after the error correction level bits.
344
360
DecToBin ( maskVersion , 3 , fStrEcc , 2 ) ;
345
361
}
346
362
}
0 commit comments