// Embed position detection patterns and surrounding vertical/horizontal separators. private static void EmbedPositionDetectionPatternsAndSeparators(ByteMatrix matrix) { // Embed three big squares at corners. int pdpWidth = POSITION_DETECTION_PATTERN[0].Length; // Left top corner. EmbedPositionDetectionPattern(0, 0, matrix); // Right top corner. EmbedPositionDetectionPattern(matrix.GetWidth() - pdpWidth, 0, matrix); // Left bottom corner. EmbedPositionDetectionPattern(0, matrix.GetWidth() - pdpWidth, matrix); // Embed horizontal separation patterns around the squares. int hspWidth = HORIZONTAL_SEPARATION_PATTERN[0].Length; // Left top corner. EmbedHorizontalSeparationPattern(0, hspWidth - 1, matrix); // Right top corner. EmbedHorizontalSeparationPattern(matrix.GetWidth() - hspWidth, hspWidth - 1, matrix); // Left bottom corner. EmbedHorizontalSeparationPattern(0, matrix.GetWidth() - hspWidth, matrix); // Embed vertical separation patterns around the squares. int vspSize = VERTICAL_SEPARATION_PATTERN.Length; // Left top corner. EmbedVerticalSeparationPattern(vspSize, 0, matrix); // Right top corner. EmbedVerticalSeparationPattern(matrix.GetHeight() - vspSize - 1, 0, matrix); // Left bottom corner. EmbedVerticalSeparationPattern(vspSize, matrix.GetHeight() - vspSize, matrix); }
private static void EmbedTimingPatterns(ByteMatrix matrix) { // -8 is for skipping position detection patterns (size 7), and two horizontal/vertical // separation patterns (size 1). Thus, 8 = 7 + 1. for (int i = 8; i < matrix.GetWidth() - 8; ++i) { int bit = (i + 1) % 2; // Horizontal line. if (!IsValidValue(matrix.Get(i, 6))) { throw new WriterException(); } if (IsEmpty(matrix.Get(i, 6))) { matrix.Set(i, 6, bit); } // Vertical line. if (!IsValidValue(matrix.Get(6, i))) { throw new WriterException(); } if (IsEmpty(matrix.Get(6, i))) { matrix.Set(6, i, bit); } } }
/// <summary>Embed type information into the matrix</summary> /// <param name="ecLevel">The error correction level (L,M,Q,H)</param> /// <param name="maskPattern">the masking pattern</param> /// <param name="matrix">Bytematrix in which the output will be stored</param> public static void EmbedTypeInfo(ErrorCorrectionLevel ecLevel, int maskPattern, ByteMatrix matrix) { BitVector typeInfoBits = new BitVector(); MakeTypeInfoBits(ecLevel, maskPattern, typeInfoBits); for (int i = 0; i < typeInfoBits.Size(); ++i) { // Place bits in LSB to MSB order. LSB (least significant bit) is the last value in // "typeInfoBits". int bit = typeInfoBits.At(typeInfoBits.Size() - 1 - i); // Type info bits at the left top corner. See 8.9 of JISX0510:2004 (p.46). int x1 = TYPE_INFO_COORDINATES[i][0]; int y1 = TYPE_INFO_COORDINATES[i][1]; matrix.Set(x1, y1, bit); if (i < 8) { // Right top corner. int x2 = matrix.GetWidth() - i - 1; int y2 = 8; matrix.Set(x2, y2, bit); } else { // Left bottom corner. int x2 = 8; int y2 = matrix.GetHeight() - 7 + (i - 8); matrix.Set(x2, y2, bit); } } }
// Embed "dataBits" using "getMaskPattern". On success, modify the matrix and return true. // For debugging purposes, it skips masking process if "getMaskPattern" is -1. // See 8.7 of JISX0510:2004 (p.38) for how to embed data bits. public static void EmbedDataBits(BitVector dataBits, int maskPattern, ByteMatrix matrix) { var bitIndex = 0; var direction = -1; // Start from the right bottom cell. var x = matrix.GetWidth() - 1; var y = matrix.GetHeight() - 1; while (x > 0) { // Skip the vertical timing pattern. if (x == 6) { x -= 1; } while (y >= 0 && y < matrix.GetHeight()) { for (var i = 0; i < 2; ++i) { var xx = x - i; // Skip the cell if it's not empty. if (!IsEmpty(matrix.Get(xx, y))) { continue; } int bit; if (bitIndex < dataBits.Size()) { bit = dataBits.At(bitIndex); ++bitIndex; } else { // Padding bit. If there is no bit left, we'll fill the left cells with 0, as described // in 8.4.9 of JISX0510:2004 (p. 24). bit = 0; } // Skip masking if mask_pattern is -1. if (maskPattern != -1) { if (MaskUtil.GetDataMaskBit(maskPattern, xx, y)) { bit ^= 0x1; } } matrix.Set(xx, y, bit); } y += direction; } direction = -direction; // Reverse the direction. y += direction; x -= 2; // Move to the left. } // All bits should be consumed. if (bitIndex != dataBits.Size()) { throw new WriterException("Not all bits consumed: " + bitIndex + '/' + dataBits.Size()); } }
private byte[] GetBitMatrix() { int width = bm.GetWidth(); int height = bm.GetHeight(); int stride = (width + 7) / 8; byte[] b = new byte[stride * height]; sbyte[][] mt = bm.GetArray(); for (int y = 0; y < height; ++y) { sbyte[] line = mt[y]; for (int x = 0; x < width; ++x) { if (line[x] != 0) { int offset = stride * y + x / 8; b[offset] |= (byte)(0x80 >> (x % 8)); } } } return(b); }
/// <summary>Gets the size of the barcode grid</summary> public override Rectangle GetBarcodeSize() { return(new Rectangle(0, 0, bm.GetWidth(), bm.GetHeight())); }