private static BitMatrix renderResult(AztecCode code, int width, int height) { var input = code.Matrix; if (input == null) { throw new InvalidOperationException("No input code matrix"); } int inputWidth = input.Width; int inputHeight = input.Height; int outputWidth = Math.Max(width, inputWidth); int outputHeight = Math.Max(height, inputHeight); int multiple = Math.Min(outputWidth / inputWidth, outputHeight / inputHeight); int leftPadding = (outputWidth - (inputWidth * multiple)) / 2; int topPadding = (outputHeight - (inputHeight * multiple)) / 2; var output = new BitMatrix(outputWidth, outputHeight); for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) { // Write the contents of this row of the barcode for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) { if (input[inputX, inputY]) { output.setRegion(outputX, outputY, multiple, multiple); } } } return(output); }
public void testUserSpecifiedLayers() { byte[] alphabet = LATIN_1.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); AztecCode aztec = Internal.Encoder.encode(alphabet, 25, -2); Assert.AreEqual(2, aztec.Layers); Assert.IsTrue(aztec.isCompact); aztec = Internal.Encoder.encode(alphabet, 25, 32); Assert.AreEqual(32, aztec.Layers); Assert.IsFalse(aztec.isCompact); try { Internal.Encoder.encode(alphabet, 25, 33); Assert.Fail("Encode should have failed. No such thing as 33 layers"); } catch (ArgumentException) { } try { Internal.Encoder.encode(alphabet, 25, -1); Assert.Fail("Encode should have failed. Text can't fit in 1-layer compact"); } catch (ArgumentException) { } }
public void testBorderCompact4Case() { // Compact(4) con hold 608 bits of information, but at most 504 can be data. Rest must // be error correction const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // encodes as 26 * 5 * 4 = 520 bits of data const string alphabet4 = alphabet + alphabet + alphabet + alphabet; byte[] data = LATIN_1.GetBytes(alphabet4); try { Internal.Encoder.encode(data, 0, -4); Assert.Fail("Encode should have failed. Text can't fit in 1-layer compact"); } catch (ArgumentException) { } // If we just try to encode it normally, it will go to a non-compact 4 layer AztecCode aztecCode = Internal.Encoder.encode(data, 0, Internal.Encoder.DEFAULT_AZTEC_LAYERS); Assert.IsFalse(aztecCode.isCompact); Assert.AreEqual(4, aztecCode.Layers); // But shortening the string to 100 bytes (500 bits of data), compact works fine, even if we // include more error checking. aztecCode = Internal.Encoder.encode(LATIN_1.GetBytes(alphabet4.Substring(0, 100)), 10, Internal.Encoder.DEFAULT_AZTEC_LAYERS); Assert.IsTrue(aztecCode.isCompact); Assert.AreEqual(4, aztecCode.Layers); }
// Helper routines private static void testEncode(String data, bool compact, int layers, String expected) { AztecCode aztec = Internal.Encoder.encode(LATIN_1.GetBytes(data), 33); Assert.AreEqual(compact, aztec.isCompact, "Unexpected symbol format (compact)"); Assert.AreEqual(layers, aztec.Layers, "Unexpected nr. of layers"); BitMatrix matrix = aztec.Matrix; Assert.AreEqual(expected, matrix.ToString(), "encode() failed"); }
private static BitMatrix encode(String contents, BarcodeFormat format, Encoding charset, int eccPercent) { if (format != BarcodeFormat.AZTEC) { throw new ArgumentException("Can only encode AZTEC code, but got " + format); } AztecCode aztec = Internal.Encoder.encode(charset.GetBytes(contents), eccPercent); return(aztec.Matrix); }
public void doTestUserSpecifiedLayers(int userSpecifiedLayers) { var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; AztecCode aztec = Internal.Encoder.encode(alphabet, 25, -2); Assert.AreEqual(2, aztec.Layers); Assert.IsTrue(aztec.isCompact); aztec = Internal.Encoder.encode(alphabet, 25, 32); Assert.AreEqual(32, aztec.Layers); Assert.IsFalse(aztec.isCompact); Internal.Encoder.encode(alphabet, 25, userSpecifiedLayers); }
private static List <Detector.Point> getOrientationPoints(AztecCode code) { var center = code.Matrix.Width / 2; var offset = code.isCompact ? 5 : 7; var result = new List <Detector.Point>(); for (var xSign = -1; xSign <= 1; xSign += 2) { for (var ySign = -1; ySign <= 1; ySign += 2) { result.Add(new Detector.Point(center + xSign * offset, center + ySign * offset)); result.Add(new Detector.Point(center + xSign * (offset - 1), center + ySign * offset)); result.Add(new Detector.Point(center + xSign * offset, center + ySign * (offset - 1))); } } return(result); }
public void testBorderCompact4Case() { // Compact(4) con hold 608 bits of information, but at most 504 can be data. Rest must // be error correction const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // encodes as 26 * 5 * 4 = 520 bits of data const string alphabet4 = alphabet + alphabet + alphabet + alphabet; // If we just try to encode it normally, it will go to a non-compact 4 layer AztecCode aztecCode = Internal.Encoder.encode(alphabet4, 0, Internal.Encoder.DEFAULT_AZTEC_LAYERS); Assert.IsFalse(aztecCode.isCompact); Assert.AreEqual(4, aztecCode.Layers); // But shortening the string to 100 bytes (500 bits of data), compact works fine, even if we // include more error checking. aztecCode = Internal.Encoder.encode(alphabet4.Substring(0, 100), 10, Internal.Encoder.DEFAULT_AZTEC_LAYERS); Assert.IsTrue(aztecCode.isCompact); Assert.AreEqual(4, aztecCode.Layers); }
private static void testEncodeDecode(String data, bool compact, int layers) { AztecCode aztec = Internal.Encoder.encode(LATIN_1.GetBytes(data), 25); Assert.AreEqual(compact, aztec.isCompact, "Unexpected symbol format (compact)"); Assert.AreEqual(layers, aztec.Layers, "Unexpected nr. of layers"); BitMatrix matrix = aztec.Matrix; AztecDetectorResult r = new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact, aztec.CodeWords, aztec.Layers); DecoderResult res = new Internal.Decoder().decode(r); Assert.AreEqual(data, res.Text); // Check error correction by introducing a few minor errors Random random = getPseudoRandom(); matrix.flip(random.Next(matrix.Width), random.Next(2)); matrix.flip(random.Next(matrix.Width), matrix.Height - 2 + random.Next(2)); matrix.flip(random.Next(2), random.Next(matrix.Height)); matrix.flip(matrix.Width - 2 + random.Next(2), random.Next(matrix.Height)); r = new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact, aztec.CodeWords, aztec.Layers); res = new Internal.Decoder().decode(r); Assert.AreEqual(data, res.Text); }
/// <summary> /// Encode a barcode using the default settings. /// </summary> /// <param name="contents">The contents to encode in the barcode</param> /// <param name="format">The barcode format to generate</param> /// <param name="width">The preferred width in pixels</param> /// <param name="height">The preferred height in pixels</param> /// <returns> /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white) /// </returns> public BitMatrix encode(String contents, BarcodeFormat format, int width, int height) { AztecCode aztec = Internal.Encoder.encode(LATIN_1.GetBytes(contents), 30); return(aztec.Matrix); }