Exemple #1
0
        /// <summary>
        /// Performs message encoding of a DataMatrix message using the algorithm described in annex P
        /// of ISO/IEC 16022:2000(E).
        /// </summary>
        /// <param name="msg">the message</param>
        /// <param name="shape">requested shape. May be {@code SymbolShapeHint.FORCE_NONE},{@code SymbolShapeHint.FORCE_SQUARE} or {@code SymbolShapeHint.FORCE_RECTANGLE}.</param>
        /// <param name="minSize">the minimum symbol size constraint or null for no constraint</param>
        /// <param name="maxSize">the maximum symbol size constraint or null for no constraint</param>
        /// <returns>the encoded message (the char values range from 0 to 255)</returns>
        public static String encodeHighLevel(String msg,
                                             SymbolShapeHint shape,
                                             Dimension minSize,
                                             Dimension maxSize)
        {
            //the codewords 0..255 are encoded as Unicode characters
            Encoder[] encoders =
            {
                new ASCIIEncoder(), new C40Encoder(),     new TextEncoder(),
                new X12Encoder(),   new EdifactEncoder(), new Base256Encoder()
            };

            var context = new EncoderContext(msg);

            context.setSymbolShape(shape);
            context.setSizeConstraints(minSize, maxSize);

            if (msg.StartsWith(MACRO_05_HEADER) && msg.EndsWith(MACRO_TRAILER))
            {
                context.writeCodeword(MACRO_05);
                context.setSkipAtEnd(2);
                context.Pos += MACRO_05_HEADER.Length;
            }
            else if (msg.StartsWith(MACRO_06_HEADER) && msg.EndsWith(MACRO_TRAILER))
            {
                context.writeCodeword(MACRO_06);
                context.setSkipAtEnd(2);
                context.Pos += MACRO_06_HEADER.Length;
            }

            int encodingMode = ASCII_ENCODATION; //Default mode

            while (context.HasMoreCharacters)
            {
                encoders[encodingMode].encode(context);
                if (context.NewEncoding >= 0)
                {
                    encodingMode = context.NewEncoding;
                    context.resetEncoderSignal();
                }
            }
            int len = context.Codewords.Length;

            context.updateSymbolInfo();
            int capacity = context.SymbolInfo.dataCapacity;

            if (len < capacity)
            {
                if (encodingMode != ASCII_ENCODATION && encodingMode != BASE256_ENCODATION)
                {
                    context.writeCodeword('\u00fe'); //Unlatch (254)
                }
            }
            //Padding
            StringBuilder codewords = context.Codewords;

            if (codewords.Length < capacity)
            {
                codewords.Append(PAD);
            }
            while (codewords.Length < capacity)
            {
                codewords.Append(randomize253State(PAD, codewords.Length + 1));
            }

            return(context.Codewords.ToString());
        }
      /// <summary>
      /// Performs message encoding of a DataMatrix message using the algorithm described in annex P
      /// of ISO/IEC 16022:2000(E).
      /// </summary>
      /// <param name="msg">the message</param>
      /// <param name="shape">requested shape. May be {@code SymbolShapeHint.FORCE_NONE},{@code SymbolShapeHint.FORCE_SQUARE} or {@code SymbolShapeHint.FORCE_RECTANGLE}.</param>
      /// <param name="minSize">the minimum symbol size constraint or null for no constraint</param>
      /// <param name="maxSize">the maximum symbol size constraint or null for no constraint</param>
      /// <returns>the encoded message (the char values range from 0 to 255)</returns>
      public static String encodeHighLevel(String msg,
                                           SymbolShapeHint shape,
                                           Dimension minSize,
                                           Dimension maxSize,
                                           int defaultEncodation)
      {
         //the codewords 0..255 are encoded as Unicode characters
         Encoder[] encoders =
            {
               new ASCIIEncoder(), new C40Encoder(), new TextEncoder(),
               new X12Encoder(), new EdifactEncoder(), new Base256Encoder()
            };

         var context = new EncoderContext(msg);
         context.setSymbolShape(shape);
         context.setSizeConstraints(minSize, maxSize);

         if (msg.StartsWith(MACRO_05_HEADER) && msg.EndsWith(MACRO_TRAILER))
         {
            context.writeCodeword(MACRO_05);
            context.setSkipAtEnd(2);
            context.Pos += MACRO_05_HEADER.Length;
         }
         else if (msg.StartsWith(MACRO_06_HEADER) && msg.EndsWith(MACRO_TRAILER))
         {
            context.writeCodeword(MACRO_06);
            context.setSkipAtEnd(2);
            context.Pos += MACRO_06_HEADER.Length;
         }

         int encodingMode = defaultEncodation; //Default mode
         switch (encodingMode)
         {
            case Encodation.BASE256:
               context.writeCodeword(HighLevelEncoder.LATCH_TO_BASE256);
               break;
            case Encodation.C40:
               context.writeCodeword(HighLevelEncoder.LATCH_TO_C40);
               break;
            case Encodation.X12:
               context.writeCodeword(HighLevelEncoder.LATCH_TO_ANSIX12);
               break;
            case Encodation.TEXT:
               context.writeCodeword(HighLevelEncoder.LATCH_TO_TEXT);
               break;
            case Encodation.EDIFACT:
               context.writeCodeword(HighLevelEncoder.LATCH_TO_EDIFACT);
               break;
            case Encodation.ASCII:
               break;
            default:
               throw new InvalidOperationException("Illegal mode: " + encodingMode);
         }
         while (context.HasMoreCharacters)
         {
            encoders[encodingMode].encode(context);
            if (context.NewEncoding >= 0)
            {
               encodingMode = context.NewEncoding;
               context.resetEncoderSignal();
            }
         }
         int len = context.Codewords.Length;
         context.updateSymbolInfo();
         int capacity = context.SymbolInfo.dataCapacity;
         if (len < capacity)
         {
            if (encodingMode != Encodation.ASCII && encodingMode != Encodation.BASE256)
            {
               context.writeCodeword('\u00fe'); //Unlatch (254)
            }
         }
         //Padding
         StringBuilder codewords = context.Codewords;
         if (codewords.Length < capacity)
         {
            codewords.Append(PAD);
         }
         while (codewords.Length < capacity)
         {
            codewords.Append(randomize253State(PAD, codewords.Length + 1));
         }

         return context.Codewords.ToString();
      }
Exemple #3
0
        /// <summary>
        /// Performs message encoding of a DataMatrix message using the algorithm described in annex P
        /// of ISO/IEC 16022:2000(E).
        /// </summary>
        /// <param name="msg">the message</param>
        /// <param name="shape">requested shape. May be {@code SymbolShapeHint.FORCE_NONE},{@code SymbolShapeHint.FORCE_SQUARE} or {@code SymbolShapeHint.FORCE_RECTANGLE}.</param>
        /// <param name="minSize">the minimum symbol size constraint or null for no constraint</param>
        /// <param name="maxSize">the maximum symbol size constraint or null for no constraint</param>
        /// <returns>the encoded message (the char values range from 0 to 255)</returns>
        public static String encodeHighLevel(String msg,
                                             SymbolShapeHint shape,
                                             Dimension minSize,
                                             Dimension maxSize,
                                             int defaultEncodation)
        {
            //the codewords 0..255 are encoded as Unicode characters
            Encoder[] encoders =
            {
                new ASCIIEncoder(), new C40Encoder(),     new TextEncoder(),
                new X12Encoder(),   new EdifactEncoder(), new Base256Encoder()
            };

            var context = new EncoderContext(msg);

            context.setSymbolShape(shape);
            context.setSizeConstraints(minSize, maxSize);

            if (msg.StartsWith(MACRO_05_HEADER) && msg.EndsWith(MACRO_TRAILER))
            {
                context.writeCodeword(MACRO_05);
                context.setSkipAtEnd(2);
                context.Pos += MACRO_05_HEADER.Length;
            }
            else if (msg.StartsWith(MACRO_06_HEADER) && msg.EndsWith(MACRO_TRAILER))
            {
                context.writeCodeword(MACRO_06);
                context.setSkipAtEnd(2);
                context.Pos += MACRO_06_HEADER.Length;
            }

            int encodingMode = defaultEncodation; //Default mode

            switch (encodingMode)
            {
            case Encodation.BASE256:
                context.writeCodeword(HighLevelEncoder.LATCH_TO_BASE256);
                break;

            case Encodation.C40:
                context.writeCodeword(HighLevelEncoder.LATCH_TO_C40);
                break;

            case Encodation.X12:
                context.writeCodeword(HighLevelEncoder.LATCH_TO_ANSIX12);
                break;

            case Encodation.TEXT:
                context.writeCodeword(HighLevelEncoder.LATCH_TO_TEXT);
                break;

            case Encodation.EDIFACT:
                context.writeCodeword(HighLevelEncoder.LATCH_TO_EDIFACT);
                break;

            case Encodation.ASCII:
                break;

            default:
                throw new InvalidOperationException("Illegal mode: " + encodingMode);
            }
            while (context.HasMoreCharacters)
            {
                encoders[encodingMode].encode(context);
                if (context.NewEncoding >= 0)
                {
                    encodingMode = context.NewEncoding;
                    context.resetEncoderSignal();
                }
            }
            int len = context.Codewords.Length;

            context.updateSymbolInfo();
            int capacity = context.SymbolInfo.dataCapacity;

            if (len < capacity)
            {
                if (encodingMode != Encodation.ASCII && encodingMode != Encodation.BASE256)
                {
                    context.writeCodeword('\u00fe'); //Unlatch (254)
                }
            }
            //Padding
            StringBuilder codewords = context.Codewords;

            if (codewords.Length < capacity)
            {
                codewords.Append(PAD);
            }
            while (codewords.Length < capacity)
            {
                codewords.Append(randomize253State(PAD, codewords.Length + 1));
            }

            return(context.Codewords.ToString());
        }
        /// <summary>
        /// Performs message encoding of a DataMatrix message using the algorithm described in annex P
        /// of ISO/IEC 16022:2000(E).
        /// </summary>
        /// <param name="msg">the message</param>
        /// <param name="shape">requested shape. May be {@code SymbolShapeHint.FORCE_NONE},{@code SymbolShapeHint.FORCE_SQUARE} or {@code SymbolShapeHint.FORCE_RECTANGLE}.</param>
        /// <param name="minSize">the minimum symbol size constraint or null for no constraint</param>
        /// <param name="maxSize">the maximum symbol size constraint or null for no constraint</param>
        /// <returns>the encoded message (the char values range from 0 to 255)</returns>
        public static String encodeHighLevel(String msg,
                                           SymbolShapeHint shape,
                                           Dimension minSize,
                                           Dimension maxSize)
        {
            //the codewords 0..255 are encoded as Unicode characters
             Encoder[] encoders = {
            new ASCIIEncoder(), new C40Encoder(), new TextEncoder(),
            new X12Encoder(), new EdifactEncoder(),  new Base256Encoder()
            };

             var context = new EncoderContext(msg);
             context.setSymbolShape(shape);
             context.setSizeConstraints(minSize, maxSize);

             if (msg.StartsWith(MACRO_05_HEADER) && msg.EndsWith(MACRO_TRAILER))
             {
            context.writeCodeword(MACRO_05);
            context.setSkipAtEnd(2);
            context.Pos += MACRO_05_HEADER.Length;
             }
             else if (msg.StartsWith(MACRO_06_HEADER) && msg.EndsWith(MACRO_TRAILER))
             {
            context.writeCodeword(MACRO_06);
            context.setSkipAtEnd(2);
            context.Pos += MACRO_06_HEADER.Length;
             }

             int encodingMode = ASCII_ENCODATION; //Default mode
             while (context.HasMoreCharacters)
             {
            encoders[encodingMode].encode(context);
            if (context.NewEncoding >= 0)
            {
               encodingMode = context.NewEncoding;
               context.resetEncoderSignal();
            }
             }
             int len = context.Codewords.Length;
             context.updateSymbolInfo();
             int capacity = context.SymbolInfo.dataCapacity;
             if (len < capacity)
             {
            if (encodingMode != ASCII_ENCODATION && encodingMode != BASE256_ENCODATION)
            {
               context.writeCodeword('\u00fe'); //Unlatch (254)
            }
             }
             //Padding
             StringBuilder codewords = context.Codewords;
             if (codewords.Length < capacity)
             {
            codewords.Append(PAD);
             }
             while (codewords.Length < capacity)
             {
            codewords.Append(randomize253State(PAD, codewords.Length + 1));
             }

             return context.Codewords.ToString();
        }