/// <summary> /// </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> /// <param name="hints">Additional parameters to supply to the encoder</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, IDictionary <EncodeHintType, object> hints) { if (format != BarcodeFormat.PDF_417) { throw new ArgumentException("Can only encode PDF_417, but got " + format); } var encoder = new Internal.PDF417(); if (hints != null) { if (hints.ContainsKey(EncodeHintType.PDF417_COMPACT)) { encoder.setCompact((Boolean)hints[EncodeHintType.PDF417_COMPACT]); } if (hints.ContainsKey(EncodeHintType.PDF417_COMPACTION)) { encoder.setCompaction((Compaction)hints[EncodeHintType.PDF417_COMPACTION]); } if (hints.ContainsKey(EncodeHintType.PDF417_DIMENSIONS)) { Dimensions dimensions = (Dimensions)hints[EncodeHintType.PDF417_DIMENSIONS]; encoder.setDimensions(dimensions.MaxCols, dimensions.MinCols, dimensions.MaxRows, dimensions.MinRows); } } return(bitMatrixFromEncoder(encoder, contents, width, height)); }
/// <summary> /// Takes encoder, accounts for width/height, and retrieves bit matrix /// </summary> private static BitMatrix bitMatrixFromEncoder(Internal.PDF417 encoder, String contents, int width, int height) { const int errorCorrectionLevel = 2; encoder.generateBarcodeLogic(contents, errorCorrectionLevel); const int lineThickness = 2; const int aspectRatio = 4; sbyte[][] originalScale = encoder.BarcodeMatrix.getScaledMatrix(lineThickness, aspectRatio * lineThickness); bool rotated = false; if ((height > width) ^ (originalScale[0].Length < originalScale.Length)) { originalScale = rotateArray(originalScale); rotated = true; } int scaleX = width / originalScale[0].Length; int scaleY = height / originalScale.Length; int scale; if (scaleX < scaleY) { scale = scaleX; } else { scale = scaleY; } if (scale > 1) { sbyte[][] scaledMatrix = encoder.BarcodeMatrix.getScaledMatrix(scale * lineThickness, scale * aspectRatio * lineThickness); if (rotated) { scaledMatrix = rotateArray(scaledMatrix); } return(bitMatrixFrombitArray(scaledMatrix)); } return(bitMatrixFrombitArray(originalScale)); }