Top-level class for the logic part of the PDF417 implementation.
Пример #1
0
        public BitMatrix encode(String contents,
                                BarcodeFormat format,
                                int width,
                                int height,
                                IDictionary hints)
        {
            if (format != BarcodeFormat.PDF_417)
            {
                throw new ArgumentException("Can only encode PDF_417, but got " + format);
            }

            PDF417 encoder = new PDF417();

            if (hints != null)
            {
                if (hints.Contains(EncodeHintType.PDF417_COMPACT))
                {
                    encoder.setCompact((Boolean)hints[EncodeHintType.PDF417_COMPACT]);
                }
                if (hints.Contains(EncodeHintType.PDF417_COMPACTION))
                {
                    encoder.setCompaction((Compaction)hints[EncodeHintType.PDF417_COMPACTION]);
                }
                if (hints.Contains(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));
        }
Пример #2
0
      /// <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);
      }
Пример #3
0
        /// <summary>
        /// Takes encoder, accounts for width/height, and retrieves bit matrix
        /// </summary>
        private static BitMatrix bitMatrixFromEncoder(Internal.PDF417 encoder,
                                                      String contents,
                                                      int errorCorrectionLevel,
                                                      int width,
                                                      int height,
                                                      int margin,
                                                      int aspectRatio)
        {
            if (width >= height)
            {
                encoder.generateBarcodeLogic(contents, errorCorrectionLevel, width, height, ref aspectRatio);
            }
            else
            {
                encoder.generateBarcodeLogic(contents, errorCorrectionLevel, height, width, ref aspectRatio);
            }

            sbyte[][] originalScale = encoder.BarcodeMatrix.getScaledMatrix(1, aspectRatio);
            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, scale * aspectRatio);
                if (rotated)
                {
                    scaledMatrix = rotateArray(scaledMatrix);
                }
                return(bitMatrixFromBitArray(scaledMatrix, margin));
            }
            return(bitMatrixFromBitArray(originalScale, margin));
        }
Пример #4
0
        /// <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,
                                                      int margin,
                                                      int errorCorrectionLevel,
                                                      float PREFERRED_RATIO)
        {
            encoder.generateBarcodeLogic(contents, errorCorrectionLevel, PREFERRED_RATIO);

            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, margin));
            }
            return(bitMatrixFrombitArray(originalScale, margin));
        }
Пример #5
0
        /// <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();
            var margin  = WHITE_SPACE;

            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);
                }
                if (hints.ContainsKey(EncodeHintType.MARGIN))
                {
                    margin = (int)(hints[EncodeHintType.MARGIN]);
                }
            }

            return(bitMatrixFromEncoder(encoder, contents, width, height, margin));
        }
Пример #6
0
        /// <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();
            var margin  = WHITE_SPACE;
            var errorCorrectionLevel = 2;

            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))
                {
                    var dimensions = (Dimensions)hints[EncodeHintType.PDF417_DIMENSIONS];
                    encoder.setDimensions(dimensions.MaxCols,
                                          dimensions.MinCols,
                                          dimensions.MaxRows,
                                          dimensions.MinRows);
                }
                if (hints.ContainsKey(EncodeHintType.MARGIN))
                {
                    margin = (int)(hints[EncodeHintType.MARGIN]);
                }
                if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
                {
                    var value = hints[EncodeHintType.ERROR_CORRECTION];
                    if (value is PDF417ErrorCorrectionLevel ||
                        value is int)
                    {
                        errorCorrectionLevel = (int)value;
                    }
                }
                if (hints.ContainsKey(EncodeHintType.CHARACTER_SET))
                {
#if !SILVERLIGHT || WINDOWS_PHONE
                    var encoding = (String)hints[EncodeHintType.CHARACTER_SET];
                    if (encoding != null)
                    {
                        encoder.setEncoding(encoding);
                    }
#else
                    // Silverlight supports only UTF-8 and UTF-16 out-of-the-box
                    encoder.setEncoding("UTF-8");
#endif
                }
                if (hints.ContainsKey(EncodeHintType.DISABLE_ECI))
                {
                    encoder.setDisableEci((bool)hints[EncodeHintType.DISABLE_ECI]);
                }
            }

            return(bitMatrixFromEncoder(encoder, contents, width, height, margin, errorCorrectionLevel));
        }
Пример #7
0
        /// <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,
                                int width,
                                int height,
                                IDictionary <EncodeHintType, object> hints)
        {
            var encoder = new Internal.PDF417();
            var margin  = WHITE_SPACE;
            var errorCorrectionLevel = 2;

            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))
                {
                    var dimensions = (Dimensions)hints[EncodeHintType.PDF417_DIMENSIONS];
                    encoder.setDimensions(dimensions.MaxCols,
                                          dimensions.MinCols,
                                          dimensions.MaxRows,
                                          dimensions.MinRows);
                }
                if (hints.ContainsKey(EncodeHintType.MARGIN))
                {
                    margin = (int)(hints[EncodeHintType.MARGIN]);
                }
                if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
                {
                    var value = hints[EncodeHintType.ERROR_CORRECTION];
                    if (value is PDF417ErrorCorrectionLevel ||
                        value is int)
                    {
                        errorCorrectionLevel = (int)value;
                    }
                }
                if (hints.ContainsKey(EncodeHintType.CHARACTER_SET))
                {
#if !SILVERLIGHT || WINDOWS_PHONE
                    var encoding = (String)hints[EncodeHintType.CHARACTER_SET];
                    if (encoding != null)
                    {
                        encoder.setEncoding(encoding);
                    }
#else
                    // Silverlight supports only UTF-8 and UTF-16 out-of-the-box
                    encoder.setEncoding("UTF-8");
#endif
                }
                if (hints.ContainsKey(EncodeHintType.DISABLE_ECI))
                {
                    encoder.setDisableEci((bool)hints[EncodeHintType.DISABLE_ECI]);
                }

                // evi1m3: MACRO implementation
                if (hints.ContainsKey(EncodeHintType.PDF417_MACROENABLE) && hints.ContainsKey(EncodeHintType.PDF417_MACROSegmentIndex))
                {
                    /*
                     * // ISO 15438
                     * // \MI segment index
                     * // \MF file ID
                     * // \MZ number of ???
                     * // \MY end of macro block
                     *
                     * // setup with segment count
                     * string sMacro="\\MI" + ((int)hints[EncodeHintType.PDF417_MACROSegmentIndex]).ToString( "D5" );
                     *
                     * // add file id
                     * if( hints.ContainsKey(EncodeHintType.PDF417_MACROFileID ) )
                     * sMacro+="\\MF" + hints[ EncodeHintType.PDF417_MACROFileID ].ToString();
                     * // add segment count
                     * if( hints.ContainsKey(EncodeHintType.PDF417_MACROSegmentCount ) )
                     * //sMacro+="\\MZ" + ((int)hints[EncodeHintType.PDF417_MACROSegmentCount]).ToString( "D5" );
                     * sMacro+="\\MO1" + ((int)hints[EncodeHintType.PDF417_MACROSegmentCount]).ToString( "D5" );
                     *
                     * // add end block
                     * sMacro += "\\MY";
                     *
                     *
                     * // prepend macro to contents
                     * contents=sMacro + contents;
                     * /**/


                    // \92800000\001\899\017\923\00100003
                    // \92800001\001\899\017\923\00100003
                    // \92800002\001\899\017\923\00100003
                    // setup with segment count
                    string sMacro = "\\928" + ((int)hints[EncodeHintType.PDF417_MACROSegmentIndex]).ToString("D5");

                    // add file id
                    if (hints.ContainsKey(EncodeHintType.PDF417_MACROFileID))
                    {
                        string sFileId = hints[EncodeHintType.PDF417_MACROFileID].ToString();
                        while ((sFileId.Length % 3) > 0)
                        {
                            sFileId = "0" + sFileId;
                        }
                        //
                        while (sFileId != "")
                        {
                            string s = sFileId.Substring(0, 3);;

                            // what to do in this case ???
                            if (int.Parse(s) > 899)
                            {
                                s = "899";
                            }

                            //
                            sMacro += "\\" + s;
                            sFileId = sFileId.Substring(3);
                        }
                    }

                    // add segment count
                    if (hints.ContainsKey(EncodeHintType.PDF417_MACROSegmentCount))
                    {
                        sMacro += "\\923\\001" + ((int)hints[EncodeHintType.PDF417_MACROSegmentCount]).ToString("D5");
                    }


                    // add macro to content end
                    contents += sMacro;
                }
            }
            // evi1m3: Y2X ratio
            float fRatio = 3;
            if (hints.ContainsKey(EncodeHintType.PDF417_Y2XRatio))
            {
                fRatio = ((float)hints[EncodeHintType.PDF417_Y2XRatio]);
            }
            //
            return(bitMatrixFromEncoder(encoder, contents, width, height, margin, errorCorrectionLevel, fRatio));
        }
Пример #8
0
      /// <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();
         var margin = WHITE_SPACE;
         var errorCorrectionLevel = DEFAULT_ERROR_CORRECTION_LEVEL;

         if (hints != null)
         {
            if (hints.ContainsKey(EncodeHintType.PDF417_COMPACT) && hints[EncodeHintType.PDF417_COMPACT] != null)
            {
               encoder.setCompact(Convert.ToBoolean(hints[EncodeHintType.PDF417_COMPACT].ToString()));
            }
            if (hints.ContainsKey(EncodeHintType.PDF417_COMPACTION) && hints[EncodeHintType.PDF417_COMPACTION] != null)
            {
               Compaction compactionEnum;
               if (Enum.TryParse(hints[EncodeHintType.PDF417_COMPACTION].ToString(), out compactionEnum))
                  encoder.setCompaction(compactionEnum);
            }
            if (hints.ContainsKey(EncodeHintType.PDF417_DIMENSIONS))
            {
               var dimensions = (Dimensions) hints[EncodeHintType.PDF417_DIMENSIONS];
               encoder.setDimensions(dimensions.MaxCols,
                                     dimensions.MinCols,
                                     dimensions.MaxRows,
                                     dimensions.MinRows);
            }
            if (hints.ContainsKey(EncodeHintType.MARGIN) && hints[EncodeHintType.MARGIN] != null)
            {
               margin = Convert.ToInt32(hints[EncodeHintType.MARGIN].ToString());
            }
            if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION) && hints[EncodeHintType.ERROR_CORRECTION] != null)
            {
               var value = hints[EncodeHintType.ERROR_CORRECTION];
               if (value is PDF417ErrorCorrectionLevel ||
                   value is int)
               {
                  errorCorrectionLevel = (int)value;
               }
               else
               {
                  PDF417ErrorCorrectionLevel errorCorrectionLevelEnum;
                  if (Enum.TryParse(value.ToString(), out errorCorrectionLevelEnum))
                     errorCorrectionLevel = (int)errorCorrectionLevelEnum;
               }
            }
            if (hints.ContainsKey(EncodeHintType.CHARACTER_SET))
            {
#if !SILVERLIGHT || WINDOWS_PHONE
               var encoding = (String)hints[EncodeHintType.CHARACTER_SET];
               if (encoding != null)
               {
                  encoder.setEncoding(encoding);
               }
#else
               // Silverlight supports only UTF-8 and UTF-16 out-of-the-box
               encoder.setEncoding("UTF-8");
#endif
            }
            if (hints.ContainsKey(EncodeHintType.DISABLE_ECI) && hints[EncodeHintType.DISABLE_ECI] != null)
            {
               encoder.setDisableEci(Convert.ToBoolean(hints[EncodeHintType.DISABLE_ECI].ToString()));
            }
         }

         return bitMatrixFromEncoder(encoder, contents, errorCorrectionLevel, width, height, margin);
      }
Пример #9
0
        /// <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();
            var margin  = WHITE_SPACE;
            var errorCorrectionLevel = DEFAULT_ERROR_CORRECTION_LEVEL;
            var aspectRatio          = DEFAULT_ASPECT_RATIO;

            if (hints != null)
            {
                if (hints.ContainsKey(EncodeHintType.PDF417_COMPACT) && hints[EncodeHintType.PDF417_COMPACT] != null)
                {
                    encoder.setCompact(Convert.ToBoolean(hints[EncodeHintType.PDF417_COMPACT].ToString()));
                }
                if (hints.ContainsKey(EncodeHintType.PDF417_COMPACTION) && hints[EncodeHintType.PDF417_COMPACTION] != null)
                {
                    if (Enum.IsDefined(typeof(Compaction), hints[EncodeHintType.PDF417_COMPACTION].ToString()))
                    {
                        var compactionEnum = (Compaction)Enum.Parse(typeof(Compaction), hints[EncodeHintType.PDF417_COMPACTION].ToString(), true);
                        encoder.setCompaction(compactionEnum);
                    }
                }
                if (hints.ContainsKey(EncodeHintType.PDF417_DIMENSIONS))
                {
                    var dimensions = (Dimensions)hints[EncodeHintType.PDF417_DIMENSIONS];
                    encoder.setDimensions(dimensions.MaxCols,
                                          dimensions.MinCols,
                                          dimensions.MaxRows,
                                          dimensions.MinRows);
                }
                if (hints.ContainsKey(EncodeHintType.MARGIN) && hints[EncodeHintType.MARGIN] != null)
                {
                    margin = Convert.ToInt32(hints[EncodeHintType.MARGIN].ToString());
                }
                if (hints.ContainsKey(EncodeHintType.PDF417_ASPECT_RATIO) && hints[EncodeHintType.PDF417_ASPECT_RATIO] != null)
                {
                    var value = hints[EncodeHintType.PDF417_ASPECT_RATIO];
                    if (value is PDF417AspectRatio ||
                        value is int)
                    {
                        aspectRatio = (int)value;
                    }
                    else
                    {
                        if (Enum.IsDefined(typeof(PDF417AspectRatio), value.ToString()))
                        {
                            var aspectRatioEnum = (PDF417AspectRatio)Enum.Parse(typeof(PDF417AspectRatio), value.ToString(), true);
                            aspectRatio = (int)aspectRatioEnum;
                        }
                    }
                }
                if (hints.ContainsKey(EncodeHintType.PDF417_IMAGE_ASPECT_RATIO) && hints[EncodeHintType.PDF417_IMAGE_ASPECT_RATIO] != null)
                {
                    var value = hints[EncodeHintType.PDF417_IMAGE_ASPECT_RATIO];
                    try
                    {
                        encoder.setDesiredAspectRatio(Convert.ToSingle(value));
                    }
                    catch
                    {
                        // User passed in something that wasn't convertible to single.
                    }
                }
                if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION) && hints[EncodeHintType.ERROR_CORRECTION] != null)
                {
                    var value = hints[EncodeHintType.ERROR_CORRECTION];
                    if (value is PDF417ErrorCorrectionLevel ||
                        value is int)
                    {
                        errorCorrectionLevel = (int)value;
                    }
                    else
                    {
                        if (Enum.IsDefined(typeof(PDF417ErrorCorrectionLevel), value.ToString()))
                        {
                            var errorCorrectionLevelEnum = (PDF417ErrorCorrectionLevel)Enum.Parse(typeof(PDF417ErrorCorrectionLevel), value.ToString(), true);
                            errorCorrectionLevel = (int)errorCorrectionLevelEnum;
                        }
                    }
                }
                if (hints.ContainsKey(EncodeHintType.CHARACTER_SET))
                {
                    encoder.setEncoding(StringUtils.UTF8);
                }
                if (hints.ContainsKey(EncodeHintType.DISABLE_ECI) && hints[EncodeHintType.DISABLE_ECI] != null)
                {
                    encoder.setDisableEci(Convert.ToBoolean(hints[EncodeHintType.DISABLE_ECI].ToString()));
                }

                // Check for PDF417 Macro options
                if (hints.ContainsKey(EncodeHintType.PDF417_MACRO_META_DATA))
                {
                    encoder.setMetaData((PDF417MacroMetadata)hints[EncodeHintType.PDF417_MACRO_META_DATA]);
                }
            }

            return(bitMatrixFromEncoder(encoder, contents, errorCorrectionLevel, width, height, margin, aspectRatio));
        }
Пример #10
0
        /// <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();
            var margin  = WHITE_SPACE;
            var errorCorrectionLevel = DEFAULT_ERROR_CORRECTION_LEVEL;

            if (hints != null)
            {
                if (hints.ContainsKey(EncodeHintType.PDF417_COMPACT) && hints[EncodeHintType.PDF417_COMPACT] != null)
                {
                    encoder.setCompact(Convert.ToBoolean(hints[EncodeHintType.PDF417_COMPACT].ToString()));
                }
                if (hints.ContainsKey(EncodeHintType.PDF417_COMPACTION) && hints[EncodeHintType.PDF417_COMPACTION] != null)
                {
                    if (Enum.IsDefined(typeof(Compaction), hints[EncodeHintType.PDF417_COMPACTION].ToString()))
                    {
                        var compactionEnum = (Compaction)Enum.Parse(typeof(Compaction), hints[EncodeHintType.PDF417_COMPACTION].ToString(), true);
                        encoder.setCompaction(compactionEnum);
                    }
                }
                if (hints.ContainsKey(EncodeHintType.PDF417_DIMENSIONS))
                {
                    var dimensions = (Dimensions)hints[EncodeHintType.PDF417_DIMENSIONS];
                    encoder.setDimensions(dimensions.MaxCols,
                                          dimensions.MinCols,
                                          dimensions.MaxRows,
                                          dimensions.MinRows);
                }
                if (hints.ContainsKey(EncodeHintType.MARGIN) && hints[EncodeHintType.MARGIN] != null)
                {
                    margin = Convert.ToInt32(hints[EncodeHintType.MARGIN].ToString());
                }
                if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION) && hints[EncodeHintType.ERROR_CORRECTION] != null)
                {
                    var value = hints[EncodeHintType.ERROR_CORRECTION];
                    if (value is PDF417ErrorCorrectionLevel ||
                        value is int)
                    {
                        errorCorrectionLevel = (int)value;
                    }
                    else
                    {
                        if (Enum.IsDefined(typeof(PDF417ErrorCorrectionLevel), value.ToString()))
                        {
                            var errorCorrectionLevelEnum = (PDF417ErrorCorrectionLevel)Enum.Parse(typeof(PDF417ErrorCorrectionLevel), value.ToString(), true);
                            errorCorrectionLevel = (int)errorCorrectionLevelEnum;
                        }
                    }
                }
                if (hints.ContainsKey(EncodeHintType.CHARACTER_SET))
                {
#if !SILVERLIGHT || WINDOWS_PHONE
                    var encoding = (String)hints[EncodeHintType.CHARACTER_SET];
                    if (encoding != null)
                    {
                        encoder.setEncoding(encoding);
                    }
#else
                    // Silverlight supports only UTF-8 and UTF-16 out-of-the-box
                    encoder.setEncoding("UTF-8");
#endif
                }
                if (hints.ContainsKey(EncodeHintType.DISABLE_ECI) && hints[EncodeHintType.DISABLE_ECI] != null)
                {
                    encoder.setDisableEci(Convert.ToBoolean(hints[EncodeHintType.DISABLE_ECI].ToString()));
                }
            }

            return(bitMatrixFromEncoder(encoder, contents, errorCorrectionLevel, width, height, margin));
        }