// Embed type information. On success, modify the matrix. 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_Renamed(x1, y1, bit); if (i < 8) { // Right top corner. int x2 = matrix.Width - i - 1; int y2 = 8; matrix.set_Renamed(x2, y2, bit); } else { // Left bottom corner. int x2 = 8; int y2 = matrix.Height - 7 + (i - 8); matrix.set_Renamed(x2, y2, bit); } } }
// Make bit vector of type information. On success, store the result in "bits" and return true. // Encode error correction level and mask pattern. See 8.9 of // JISX0510:2004 (p.45) for details. public static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitVector bits) { if (!QRCode.isValidMaskPattern(maskPattern)) { throw new WriterException("Invalid mask pattern"); } int typeInfo = (ecLevel.Bits << 3) | maskPattern; bits.appendBits(typeInfo, 5); int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY); bits.appendBits(bchCode, 10); BitVector maskBits = new BitVector(); maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15); bits.xor(maskBits); if (bits.size() != 15) { // Just in case. throw new WriterException("should not happen but we got: " + bits.size()); } }
public ByteMatrix encode(string contents, BarcodeFormat format, int width, int height, System.Collections.Generic.Dictionary <Object, Object> hints) { if (contents == null || contents.Length == 0) { throw new System.ArgumentException("Found empty contents"); } if (format != BarcodeFormat.QR_CODE) { throw new System.ArgumentException("Can only encode QR_CODE, but got " + format); } if (width < 0 || height < 0) { throw new System.ArgumentException("Requested dimensions are too small: " + width + 'x' + height); } ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L; if (hints != null && hints.ContainsKey(EncodeHintType.ERROR_CORRECTION)) { errorCorrectionLevel = (ErrorCorrectionLevel)hints[EncodeHintType.ERROR_CORRECTION]; //UPGRADE: Fixed dictionary key grab issue } QRCode code = new QRCode(); Encoder.encode(contents, errorCorrectionLevel, hints, code); return(renderResult(code, width, height)); }
// public ByteMatrix encode(System.String contents, BarcodeFormat format, int width, int height, System.Collections.Hashtable hints) // commented by .net follower (http://dotnetfollower.com) public ByteMatrix encode(System.String contents, BarcodeFormat format, int width, int height, System.Collections.Generic.Dictionary <Object, Object> hints) // added by .net follower (http://dotnetfollower.com) { if (contents == null || contents.Length == 0) { throw new System.ArgumentException("Found empty contents"); } if (format != BarcodeFormat.QR_CODE) { throw new System.ArgumentException("Can only encode QR_CODE, but got " + format); } if (width < 0 || height < 0) { throw new System.ArgumentException("Requested dimensions are too small: " + width + 'x' + height); } ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L; if (hints != null) { // ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints[EncodeHintType.ERROR_CORRECTION]; // commented by .net follower (http://dotnetfollower.com) ErrorCorrectionLevel requestedECLevel = hints.ContainsKey(EncodeHintType.ERROR_CORRECTION) ? (ErrorCorrectionLevel)hints[EncodeHintType.ERROR_CORRECTION] : null; // added by .net follower (http://dotnetfollower.com) if (requestedECLevel != null) { errorCorrectionLevel = requestedECLevel; } } QRCode code = new QRCode(); Encoder.encode(contents, errorCorrectionLevel, hints, code); return(renderResult(code, width, height)); }
public ByteMatrix encode(System.String contents, BarcodeFormat format, int width, int height, System.Collections.Hashtable hints) { if (contents == null || contents.Length == 0) { throw new System.ArgumentException("Found empty contents"); } if (format != BarcodeFormat.QR_CODE) { throw new System.ArgumentException("Can only encode QR_CODE, but got " + format); } if (width < 0 || height < 0) { throw new System.ArgumentException("Requested dimensions are too small: " + width + 'x' + height); } ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L; if (hints != null) { ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel)hints[EncodeHintType.ERROR_CORRECTION]; if (requestedECLevel != null) { errorCorrectionLevel = requestedECLevel; } } QRCode code = new QRCode(); Encoder.encode(contents, errorCorrectionLevel, hints, code); return(renderResult(code, width, height)); }
public static void encode(System.String content, ErrorCorrectionLevel ecLevel, System.Collections.Hashtable hints, QRCode qrCode) { System.String encoding = hints == null?null:(System.String) hints[EncodeHintType.CHARACTER_SET]; if (encoding == null) { encoding = DEFAULT_BYTE_MODE_ENCODING; } // Step 1: Choose the mode (encoding). Mode mode = chooseMode(content, encoding); // Step 2: Append "bytes" into "dataBits" in appropriate encoding. BitVector dataBits = new BitVector(); appendBytes(content, mode, dataBits, encoding); // Step 3: Initialize QR code that can contain "dataBits". int numInputBytes = dataBits.sizeInBytes(); initQRCode(numInputBytes, ecLevel, mode, qrCode); // Step 4: Build another bit vector that contains header and data. BitVector headerAndDataBits = new BitVector(); // Step 4.5: Append ECI message if applicable if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.Equals(encoding)) { CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding); if (eci != null) { appendECI(eci, headerAndDataBits); } } appendModeInfo(mode, headerAndDataBits); int numLetters = mode.Equals(Mode.BYTE)?dataBits.sizeInBytes():content.Length; appendLengthInfo(numLetters, qrCode.Version, mode, headerAndDataBits); headerAndDataBits.appendBitVector(dataBits); // Step 5: Terminate the bits properly. terminateBits(qrCode.NumDataBytes, headerAndDataBits); // Step 6: Interleave data bits with error correction code. BitVector finalBits = new BitVector(); interleaveWithECBytes(headerAndDataBits, qrCode.NumTotalBytes, qrCode.NumDataBytes, qrCode.NumRSBlocks, finalBits); // Step 7: Choose the mask pattern and set to "qrCode". ByteMatrix matrix = new ByteMatrix(qrCode.MatrixWidth, qrCode.MatrixWidth); qrCode.MaskPattern = chooseMaskPattern(finalBits, qrCode.ECLevel, qrCode.Version, matrix); // Step 8. Build the matrix and set it to "qrCode". MatrixUtil.buildMatrix(finalBits, qrCode.ECLevel, qrCode.Version, qrCode.MaskPattern, matrix); qrCode.Matrix = matrix; // Step 9. Make sure we have a valid QR Code. if (!qrCode.Valid) { throw new WriterException("Invalid QR code: " + qrCode.ToString()); } }
// Build 2D matrix of QR Code from "dataBits" with "ecLevel", "version" and "getMaskPattern". On // success, store the result in "matrix" and return true. public static void buildMatrix(BitVector dataBits, ErrorCorrectionLevel ecLevel, int version, int maskPattern, ByteMatrix matrix) { clearMatrix(matrix); embedBasicPatterns(version, matrix); // Type information appear with any version. embedTypeInfo(ecLevel, version, maskPattern, matrix); // Data should be embedded at end. embedDataBits(dataBits, maskPattern, matrix); }
// public DecoderResult(sbyte[] rawBytes, System.String text, System.Collections.ArrayList byteSegments, ErrorCorrectionLevel ecLevel) // commented by .net follower (http://dotnetfollower.com) public DecoderResult(sbyte[] rawBytes, System.String text, System.Collections.Generic.List <Object> byteSegments, ErrorCorrectionLevel ecLevel) // added by .net follower (http://dotnetfollower.com) { if (rawBytes == null && text == null) { throw new System.ArgumentException(); } this.rawBytes = rawBytes; this.text = text; this.byteSegments = byteSegments; this.ecLevel = ecLevel; }
// Build 2D matrix of QR Code from "dataBits" with "ecLevel", "version" and "getMaskPattern". On // success, store the result in "matrix" and return true. //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: static void buildMatrix(com.google.zxing.common.BitArray dataBits, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel ecLevel, com.google.zxing.qrcode.decoder.Version version, int maskPattern, ByteMatrix matrix) throws com.google.zxing.WriterException internal static void buildMatrix(BitArray dataBits, ErrorCorrectionLevel ecLevel, Version version, int maskPattern, ByteMatrix matrix) { clearMatrix(matrix); embedBasicPatterns(version, matrix); // Type information appear with any version. embedTypeInfo(ecLevel, maskPattern, matrix); // Version info appear if version >= 7. maybeEmbedVersionInfo(version, matrix); // Data should be embedded at end. embedDataBits(dataBits, maskPattern, matrix); }
public DecoderResult(sbyte[] rawBytes, String text, ArrayList byteSegments, ErrorCorrectionLevel ecLevel) { if (rawBytes == null && text == null) { throw new Exception("ArgumentException"); } this.rawBytes = rawBytes; this.text = text; this.byteSegments = byteSegments; this.ecLevel = ecLevel; }
public DecoderResult(sbyte[] rawBytes, System.String text, System.Collections.Generic.List<Object> byteSegments, ErrorCorrectionLevel ecLevel) { if (rawBytes == null && text == null) { throw new System.ArgumentException(); } this.rawBytes = rawBytes; this.text = text; this.byteSegments = byteSegments; this.ecLevel = ecLevel; }
public DecoderResult(sbyte[] rawBytes, System.String text, System.Collections.ArrayList byteSegments, ErrorCorrectionLevel ecLevel) { if (rawBytes == null && text == null) { throw new System.ArgumentException(); } this.rawBytes = rawBytes; this.text = text; this.byteSegments = byteSegments; this.ecLevel = ecLevel; }
public QRCode() { mode = null; ecLevel = null; version = -1; matrixWidth = -1; maskPattern = -1; numTotalBytes = -1; numDataBytes = -1; numECBytes = -1; numRSBlocks = -1; matrix = null; }
public MicroQRCode() { mode = null; ecLevel = null; version = - 1; matrixWidth = - 1; maskPattern = - 1; numTotalBytes = - 1; numDataBytes = - 1; numECBytes = - 1; numRSBlocks = - 1; matrix = null; }
private static int chooseMaskPattern(BitVector bits, ErrorCorrectionLevel ecLevel, int version, ByteMatrix matrix) { int minPenalty = System.Int32.MaxValue; // Lower penalty is better. int bestMaskPattern = -1; // We try all mask patterns to choose the best one. for (int maskPattern = 0; maskPattern < QRCode.NUM_MASK_PATTERNS; maskPattern++) { MatrixUtil.buildMatrix(bits, ecLevel, version, maskPattern, matrix); int penalty = calculateMaskPenalty(matrix); if (penalty < minPenalty) { minPenalty = penalty; bestMaskPattern = maskPattern; } } return(bestMaskPattern); }
/// <summary> /// Make bit vector of type information. On success, store the result in "bits" and return true. /// Encode error correction level and mask pattern. See 8.9 of /// JISX0510:2004 (p.45) for details. /// </summary> /// <param name="ecLevel"></param> /// <param name="maskPattern"></param> /// <param name="bits"></param> /// <remarks> /// ISO/IEC 18004:2006(E) 6.9 Format information /// </remarks> public static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int version, int maskPattern, BitVector bits) { if (!MicroQRCode.isValidMaskPattern(maskPattern)) { throw new WriterException("Invalid mask pattern"); } //ISO/IEC 18004:2006(E) 6.9 Format information //Symbol number 0: 000 //Data mask pattern reference: 11 //Data bits (symbol number, data mask pattern reference): 00011 //BCH bits: 1101011001 //Unmasked bit sequence: 000111101011001 //Mask pattern for XOR operation: 100010001000101 //Format information module pattern: 100101100011100 int typeInfo = SYMBOL_NUMBERS_INFO[version - 1][ecLevel.ordinal()]; if (typeInfo == -1) { throw new WriterException("Invalid Version&Level info"); } typeInfo = (typeInfo << 2) | maskPattern; bits.appendBits(typeInfo, 5); int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY); bits.appendBits(bchCode, 10); BitVector maskBits = new BitVector(); maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15); bits.xor(maskBits); if (bits.size() != 15) { // Just in case. throw new WriterException("should not happen but we got: " + bits.size()); } }
/// <summary> Initialize "qrCode" according to "numInputBytes", "ecLevel", and "mode". On success, /// modify "qrCode". /// </summary> private static void initQRCode(int numInputBytes, ErrorCorrectionLevel ecLevel, Mode mode, QRCode qrCode) { qrCode.ECLevel = ecLevel; qrCode.Mode = mode; // In the following comments, we use numbers of Version 7-H. for (int versionNum = 1; versionNum <= 40; versionNum++) { Version version = Version.getVersionForNumber(versionNum); // numBytes = 196 int numBytes = version.TotalCodewords; // getNumECBytes = 130 Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel); int numEcBytes = ecBlocks.TotalECCodewords; // getNumRSBlocks = 5 int numRSBlocks = ecBlocks.NumBlocks; // getNumDataBytes = 196 - 130 = 66 int numDataBytes = numBytes - numEcBytes; // We want to choose the smallest version which can contain data of "numInputBytes" + some // extra bits for the header (mode info and length info). The header can be three bytes // (precisely 4 + 16 bits) at most. Hence we do +3 here. if (numDataBytes >= numInputBytes + 3) { // Yay, we found the proper rs block info! qrCode.Version = versionNum; qrCode.NumTotalBytes = numBytes; qrCode.NumDataBytes = numDataBytes; qrCode.NumRSBlocks = numRSBlocks; // getNumECBytes = 196 - 66 = 130 qrCode.NumECBytes = numEcBytes; // matrix width = 21 + 6 * 4 = 45 qrCode.MatrixWidth = version.DimensionForVersion; return; } } throw new WriterException("Cannot find proper rs block info (input data too big?)"); }
/// <summary> Encode "bytes" with the error correction level "ecLevel". The encoding mode will be chosen /// internally by chooseMode(). On success, store the result in "qrCode". /// /// We recommend you to use QRCode.EC_LEVEL_L (the lowest level) for /// "getECLevel" since our primary use is to show QR code on desktop screens. We don't need very /// strong error correction for this purpose. /// /// Note that there is no way to encode bytes in MODE_KANJI. We might want to add EncodeWithMode() /// with which clients can specify the encoding mode. For now, we don't need the functionality. /// </summary> public static void encode(System.String content, ErrorCorrectionLevel ecLevel, MicroQRCode qrCode, int versionNum) { encode(content, ecLevel, null, qrCode, versionNum); }
/// <summary> /// Make bit vector of type information. On success, store the result in "bits" and return true. /// Encode error correction level and mask pattern. See 8.9 of /// JISX0510:2004 (p.45) for details. /// </summary> /// <param name="ecLevel"></param> /// <param name="maskPattern"></param> /// <param name="bits"></param> /// <remarks> /// ISO/IEC 18004:2006(E) 6.9 Format information /// </remarks> public static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int version, int maskPattern, BitVector bits) { if (!MicroQRCode.isValidMaskPattern(maskPattern)) { throw new WriterException("Invalid mask pattern"); } //ISO/IEC 18004:2006(E) 6.9 Format information //Symbol number 0: 000 //Data mask pattern reference: 11 //Data bits (symbol number, data mask pattern reference): 00011 //BCH bits: 1101011001 //Unmasked bit sequence: 000111101011001 //Mask pattern for XOR operation: 100010001000101 //Format information module pattern: 100101100011100 int typeInfo = SYMBOL_NUMBERS_INFO[version - 1][ecLevel.ordinal()]; if (typeInfo == -1) throw new WriterException("Invalid Version&Level info"); typeInfo = (typeInfo << 2) | maskPattern; bits.appendBits(typeInfo, 5); int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY); bits.appendBits(bchCode, 10); BitVector maskBits = new BitVector(); maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15); bits.xor(maskBits); if (bits.size() != 15) { // Just in case. throw new WriterException("should not happen but we got: " + bits.size()); } }
/// <summary> Encode "bytes" with the error correction level "ecLevel". The encoding mode will be chosen /// internally by chooseMode(). On success, store the result in "qrCode". /// /// We recommend you to use QRCode.EC_LEVEL_L (the lowest level) for /// "getECLevel" since our primary use is to show QR code on desktop screens. We don't need very /// strong error correction for this purpose. /// /// Note that there is no way to encode bytes in MODE_KANJI. We might want to add EncodeWithMode() /// with which clients can specify the encoding mode. For now, we don't need the functionality. /// </summary> public static void encode(System.String content, ErrorCorrectionLevel ecLevel, QRCode qrCode) { encode(content, ecLevel, null, qrCode); }
/// <summary> /// /// </summary> /// <param name="content"></param> /// <param name="ecLevel"></param> /// <param name="hints"></param> /// <param name="qrCode"></param> public static void encode(System.String content, ErrorCorrectionLevel ecLevel, System.Collections.Hashtable hints, MicroQRCode qrCode, int versionNum) { if (versionNum < 1 || versionNum > 4) throw new ArgumentOutOfRangeException("versionNum", "versionNum [1, 4]"); System.String encoding = hints == null ? null : (System.String)hints[EncodeHintType.CHARACTER_SET]; if (encoding == null) { encoding = DEFAULT_BYTE_MODE_ENCODING; } // Step 1: Choose the mode (encoding). Mode mode = chooseMode(content, encoding); // Step 2: Append "bytes" into "dataBits" in appropriate encoding. BitVector dataBits = new BitVector(); appendBytes(content, mode, dataBits, encoding, versionNum); // Step 3: Initialize QR code that can contain "dataBits". int numInputBytes = dataBits.sizeInBytes(); initQRCode(numInputBytes, ecLevel, mode, qrCode, versionNum); // Step 4: Build another bit vector that contains header and data. BitVector headerAndDataBits = new BitVector(); //INFO ECB+Mode+Length+Data[+terminate] // Step 4.5: Append ECI message if applicable appendModeInfo(mode, headerAndDataBits, versionNum); int numLetters = mode.Equals(Mode.BYTE) ? dataBits.sizeInBytes() : content.Length; appendLengthInfo(numLetters, qrCode.Version, mode, headerAndDataBits); headerAndDataBits.appendBitVector(dataBits); // Step 5: Terminate the bits properly. terminateBits(qrCode.NumDataBytes, headerAndDataBits, versionNum); // Step 6: Interleave data bits with error correction code. BitVector finalBits = new BitVector(); interleaveWithECBytes(headerAndDataBits, versionNum, qrCode.NumTotalBytes, qrCode.NumDataBytes, qrCode.NumRSBlocks, finalBits); // Step 7: Choose the mask pattern and set to "qrCode". ByteMatrix matrix = new ByteMatrix(qrCode.MatrixWidth, qrCode.MatrixWidth); qrCode.MaskPattern = chooseMaskPattern(finalBits, qrCode.ECLevel, qrCode.Version, matrix); // Step 8. Build the matrix and set it to "qrCode". MatrixUtil.buildMatrix(finalBits, qrCode.ECLevel, qrCode.Version, qrCode.MaskPattern, matrix); qrCode.Matrix = matrix; //var decoder = new com.google.zxing.microqrcode.decoder.Decoder(); //var res = decoder.decode(com.google.zxing.common.BitMatrix.FromByteMatrix(matrix)); //Console.WriteLine(res.Text); // Step 9. Make sure we have a valid QR Code. if (!qrCode.Valid) { throw new WriterException("Invalid QR code: " + qrCode.ToString()); } }
private static int chooseMaskPattern(BitVector bits, ErrorCorrectionLevel ecLevel, int version, ByteMatrix matrix) { int minPenalty = System.Int32.MaxValue; // Lower penalty is better. int bestMaskPattern = - 1; // We try all mask patterns to choose the best one. for (int maskPattern = 0; maskPattern < QRCode.NUM_MASK_PATTERNS; maskPattern++) { MatrixUtil.buildMatrix(bits, ecLevel, version, maskPattern, matrix); int penalty = calculateMaskPenalty(matrix); if (penalty < minPenalty) { minPenalty = penalty; bestMaskPattern = maskPattern; } } return bestMaskPattern; }
/// <summary> Initialize "qrCode" according to "numInputBytes", "ecLevel", and "mode". On success, /// modify "qrCode". /// </summary> private static void initQRCode(int numInputBytes, ErrorCorrectionLevel ecLevel, Mode mode, QRCode qrCode) { qrCode.ECLevel = ecLevel; qrCode.Mode = mode; // In the following comments, we use numbers of Version 7-H. for (int versionNum = 1; versionNum <= 40; versionNum++) { Version version = Version.getVersionForNumber(versionNum); // numBytes = 196 int numBytes = version.TotalCodewords; // getNumECBytes = 130 Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel); int numEcBytes = ecBlocks.TotalECCodewords; // getNumRSBlocks = 5 int numRSBlocks = ecBlocks.NumBlocks; // getNumDataBytes = 196 - 130 = 66 int numDataBytes = numBytes - numEcBytes; // We want to choose the smallest version which can contain data of "numInputBytes" + some // extra bits for the header (mode info and length info). The header can be three bytes // (precisely 4 + 16 bits) at most. Hence we do +3 here. if (numDataBytes >= numInputBytes + 3) { // Yay, we found the proper rs block info! qrCode.Version = versionNum; qrCode.NumTotalBytes = numBytes; qrCode.NumDataBytes = numDataBytes; qrCode.NumRSBlocks = numRSBlocks; // getNumECBytes = 196 - 66 = 130 qrCode.NumECBytes = numEcBytes; // matrix width = 21 + 6 * 4 = 45 qrCode.MatrixWidth = version.DimensionForVersion; return ; } } throw new WriterException("Cannot find proper rs block info (input data too big?)"); }
/// <summary> <p>When QR Codes use multiple data blocks, they are actually interleaved. /// That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This /// method will separate the data into original blocks.</p> /// /// </summary> /// <param name="rawCodewords">bytes as read directly from the QR Code /// </param> /// <param name="version">version of the QR Code /// </param> /// <param name="ecLevel">error-correction level of the QR Code /// </param> /// <returns> {@link DataBlock}s containing original bytes, "de-interleaved" from representation in the /// QR Code /// </returns> internal static DataBlock[] getDataBlocks(sbyte[] rawCodewords, Version version, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel ecLevel) { if (rawCodewords.Length != version.TotalCodewords) { throw new System.ArgumentException(); } // Figure out the number and size of data blocks used by this version and // error correction level Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel); // First count the total number of data blocks int totalBlocks = 0; Version.ECB[] ecBlockArray = ecBlocks.getECBlocks(); for (int i = 0; i < ecBlockArray.Length; i++) { totalBlocks += ecBlockArray[i].Count; } // Now establish DataBlocks of the appropriate size and number of data codewords DataBlock[] result = new DataBlock[totalBlocks]; int numResultBlocks = 0; for (int j = 0; j < ecBlockArray.Length; j++) { Version.ECB ecBlock = ecBlockArray[j]; for (int i = 0; i < ecBlock.Count; i++) { int numDataCodewords = ecBlock.DataCodewords; int numBlockCodewords = ecBlocks.ECCodewordsPerBlock + numDataCodewords; result[numResultBlocks++] = new DataBlock(numDataCodewords, new sbyte[numBlockCodewords]); } } // All blocks have the same amount of data, except that the last n // (where n may be 0) have 1 more byte. Figure out where these start. int shorterBlocksTotalCodewords = result[0].codewords.Length; int longerBlocksStartAt = result.Length - 1; while (longerBlocksStartAt >= 0) { int numCodewords = result[longerBlocksStartAt].codewords.Length; if (numCodewords == shorterBlocksTotalCodewords) { break; } longerBlocksStartAt--; } longerBlocksStartAt++; int shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks.ECCodewordsPerBlock; // The last elements of result may be 1 element longer; // first fill out as many elements as all of them have int rawCodewordsOffset = 0; for (int i = 0; i < shorterBlocksNumDataCodewords; i++) { for (int j = 0; j < numResultBlocks; j++) { result[j].codewords[i] = rawCodewords[rawCodewordsOffset++]; } } // Fill out the last data block in the longer ones for (int j = longerBlocksStartAt; j < numResultBlocks; j++) { result[j].codewords[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset++]; } // Now add in error correction blocks int max = result[0].codewords.Length; for (int i = shorterBlocksNumDataCodewords; i < max; i++) { for (int j = 0; j < numResultBlocks; j++) { int iOffset = j < longerBlocksStartAt?i:i + 1; result[j].codewords[iOffset] = rawCodewords[rawCodewordsOffset++]; } } return(result); }
/// <summary> /// /// </summary> /// <param name="content"></param> /// <param name="ecLevel"></param> /// <param name="hints"></param> /// <param name="qrCode"></param> public static void encode(System.String content, ErrorCorrectionLevel ecLevel, System.Collections.Hashtable hints, MicroQRCode qrCode, int versionNum) { if (versionNum < 1 || versionNum > 4) { throw new ArgumentOutOfRangeException("versionNum", "versionNum [1, 4]"); } System.String encoding = hints == null ? null : (System.String)hints[EncodeHintType.CHARACTER_SET]; if (encoding == null) { encoding = DEFAULT_BYTE_MODE_ENCODING; } // Step 1: Choose the mode (encoding). Mode mode = chooseMode(content, encoding); // Step 2: Append "bytes" into "dataBits" in appropriate encoding. BitVector dataBits = new BitVector(); appendBytes(content, mode, dataBits, encoding, versionNum); // Step 3: Initialize QR code that can contain "dataBits". int numInputBytes = dataBits.sizeInBytes(); initQRCode(numInputBytes, ecLevel, mode, qrCode, versionNum); // Step 4: Build another bit vector that contains header and data. BitVector headerAndDataBits = new BitVector(); //INFO ECB+Mode+Length+Data[+terminate] // Step 4.5: Append ECI message if applicable appendModeInfo(mode, headerAndDataBits, versionNum); int numLetters = mode.Equals(Mode.BYTE) ? dataBits.sizeInBytes() : content.Length; appendLengthInfo(numLetters, qrCode.Version, mode, headerAndDataBits); headerAndDataBits.appendBitVector(dataBits); // Step 5: Terminate the bits properly. terminateBits(qrCode.NumDataBytes, headerAndDataBits, versionNum); // Step 6: Interleave data bits with error correction code. BitVector finalBits = new BitVector(); interleaveWithECBytes(headerAndDataBits, versionNum, qrCode.NumTotalBytes, qrCode.NumDataBytes, qrCode.NumRSBlocks, finalBits); // Step 7: Choose the mask pattern and set to "qrCode". ByteMatrix matrix = new ByteMatrix(qrCode.MatrixWidth, qrCode.MatrixWidth); qrCode.MaskPattern = chooseMaskPattern(finalBits, qrCode.ECLevel, qrCode.Version, matrix); // Step 8. Build the matrix and set it to "qrCode". MatrixUtil.buildMatrix(finalBits, qrCode.ECLevel, qrCode.Version, qrCode.MaskPattern, matrix); qrCode.Matrix = matrix; //var decoder = new com.google.zxing.microqrcode.decoder.Decoder(); //var res = decoder.decode(com.google.zxing.common.BitMatrix.FromByteMatrix(matrix)); //Console.WriteLine(res.Text); // Step 9. Make sure we have a valid QR Code. if (!qrCode.Valid) { throw new WriterException("Invalid QR code: " + qrCode.ToString()); } }
public static void encode(System.String content, ErrorCorrectionLevel ecLevel, System.Collections.Hashtable hints, QRCode qrCode) { System.String encoding = hints == null?null:(System.String)hints[EncodeHintType.CHARACTER_SET]; if (encoding == null) { encoding = DEFAULT_BYTE_MODE_ENCODING; } // Step 1: Choose the mode (encoding). Mode mode = chooseMode(content, encoding); // Step 2: Append "bytes" into "dataBits" in appropriate encoding. BitVector dataBits = new BitVector(); appendBytes(content, mode, dataBits, encoding); // Step 3: Initialize QR code that can contain "dataBits". int numInputBytes = dataBits.sizeInBytes(); initQRCode(numInputBytes, ecLevel, mode, qrCode); // Step 4: Build another bit vector that contains header and data. BitVector headerAndDataBits = new BitVector(); // Step 4.5: Append ECI message if applicable if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.Equals(encoding)) { CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding); if (eci != null) { appendECI(eci, headerAndDataBits); } } appendModeInfo(mode, headerAndDataBits); int numLetters = mode.Equals(Mode.BYTE)?dataBits.sizeInBytes():content.Length; appendLengthInfo(numLetters, qrCode.Version, mode, headerAndDataBits); headerAndDataBits.appendBitVector(dataBits); // Step 5: Terminate the bits properly. terminateBits(qrCode.NumDataBytes, headerAndDataBits); // Step 6: Interleave data bits with error correction code. BitVector finalBits = new BitVector(); interleaveWithECBytes(headerAndDataBits, qrCode.NumTotalBytes, qrCode.NumDataBytes, qrCode.NumRSBlocks, finalBits); // Step 7: Choose the mask pattern and set to "qrCode". ByteMatrix matrix = new ByteMatrix(qrCode.MatrixWidth, qrCode.MatrixWidth); qrCode.MaskPattern = chooseMaskPattern(finalBits, qrCode.ECLevel, qrCode.Version, matrix); // Step 8. Build the matrix and set it to "qrCode". MatrixUtil.buildMatrix(finalBits, qrCode.ECLevel, qrCode.Version, qrCode.MaskPattern, matrix); qrCode.Matrix = matrix; // Step 9. Make sure we have a valid QR Code. if (!qrCode.Valid) { throw new WriterException("Invalid QR code: " + qrCode.ToString()); } }