Example #1
0
        public static SymbolInfo lookup(int dataCodewords, bool allowRectangular, bool fail)
        {
            SymbolShapeHint shape = allowRectangular
                ? SymbolShapeHint.FORCE_NONE : SymbolShapeHint.FORCE_SQUARE;

            return(lookup(dataCodewords, shape, fail));
        }
Example #2
0
        public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, IDictionary <EncodeHintType, object> hints)
        {
            if (String.IsNullOrEmpty(contents))
            {
                throw new ArgumentException("Found empty contents", contents);
            }

            if (format != BarcodeFormat.DATA_MATRIX)
            {
                throw new ArgumentException("Can only encode DATA_MATRIX, but got " + format);
            }

            if (width < 0 || height < 0)
            {
                throw new ArgumentException("Requested dimensions are too small: " + width + 'x' + height);
            }

            // Try to get force shape & min / max size
            SymbolShapeHint shape   = SymbolShapeHint.FORCE_NONE;
            Dimension       minSize = null;
            Dimension       maxSize = null;

            if (hints != null)
            {
                var requestedShape = hints.ContainsKey(EncodeHintType.DATA_MATRIX_SHAPE) ? (SymbolShapeHint?)hints[EncodeHintType.DATA_MATRIX_SHAPE] : null;
                if (requestedShape != null)
                {
                    shape = requestedShape.Value;
                }
                var requestedMinSize = hints.ContainsKey(EncodeHintType.MIN_SIZE) ? (Dimension)hints[EncodeHintType.MIN_SIZE] : null;
                if (requestedMinSize != null)
                {
                    minSize = requestedMinSize;
                }
                var requestedMaxSize = hints.ContainsKey(EncodeHintType.MAX_SIZE) ? (Dimension)hints[EncodeHintType.MAX_SIZE] : null;
                if (requestedMaxSize != null)
                {
                    maxSize = requestedMaxSize;
                }
            }


            //1. step: Data encodation
            String encoded = HighLevelEncoder.encodeHighLevel(contents, shape, minSize, maxSize);

            SymbolInfo symbolInfo = SymbolInfo.lookup(encoded.Length, shape, minSize, maxSize, true);

            //2. step: ECC generation
            String codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo);

            //3. step: Module placement in Matrix
            var placement =
                new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(), symbolInfo.getSymbolDataHeight());

            placement.place();

            //4. step: low-level encoding
            return(encodeLowLevel(placement, symbolInfo));
        }
        public static Datamatrix.Encoder.SymbolShapeHint?ToZXing(this SymbolShapeHint other)
        {
            switch (other)
            {
            case SymbolShapeHint.FORCE_SQUARE:
                return(Datamatrix.Encoder.SymbolShapeHint.FORCE_SQUARE);

            case SymbolShapeHint.FORCE_RECTANGLE:
                return(Datamatrix.Encoder.SymbolShapeHint.FORCE_RECTANGLE);

            case SymbolShapeHint.FORCE_NONE:
            default:
                return(Datamatrix.Encoder.SymbolShapeHint.FORCE_NONE);
            }
        }
Example #4
0
        public void TestEncodeDecode(String data, SymbolShapeHint shape)
        {
            var writer  = new DataMatrixWriter();
            var options = new DatamatrixEncodingOptions {
                SymbolShape = shape
            };
            var matrix = writer.encode(data, BarcodeFormat.DATA_MATRIX, 0, 0, options.Hints);

            Assert.That(matrix, Is.Not.Null);

            var res = new Internal.Decoder().decode(matrix);

            Assert.That(res, Is.Not.Null);
            Assert.That(res.Text, Is.EqualTo(data));
        }
Example #5
0
 public static SymbolInfo lookup(int dataCodewords,
                                 SymbolShapeHint shape,
                                 Dimension minSize,
                                 Dimension maxSize,
                                 bool fail)
 {
     foreach (SymbolInfo symbol in symbols)
     {
         if (shape == SymbolShapeHint.FORCE_SQUARE && symbol.rectangular)
         {
             continue;
         }
         if (shape == SymbolShapeHint.FORCE_RECTANGLE && !symbol.rectangular)
         {
             continue;
         }
         if (minSize != null &&
             (symbol.getSymbolWidth() < minSize.Width ||
              symbol.getSymbolHeight() < minSize.Height))
         {
             continue;
         }
         if (maxSize != null &&
             (symbol.getSymbolWidth() > maxSize.Width ||
              symbol.getSymbolHeight() > maxSize.Height))
         {
             continue;
         }
         if (dataCodewords <= symbol.dataCapacity)
         {
             return(symbol);
         }
     }
     if (fail)
     {
         throw new ArgumentException(
                   "Can't find a symbol arrangement that matches the message. Data codewords: "
                   + dataCodewords);
     }
     return(null);
 }
 public EncoderContext(String msg)
 {
     //From this point on Strings are not Unicode anymore!
      var msgBinary = Encoding.GetEncoding("ISO-8859-1").GetBytes(msg);
      var sb = new StringBuilder(msgBinary.Length);
      var c = msgBinary.Length;
      for (int i = 0; i < c; i++)
      {
     // TODO: does it works in .Net the same way?
     char ch = (char)(msgBinary[i] & 0xff);
     if (ch == '?' && msg[i] != '?')
     {
        throw new ArgumentException("Message contains characters outside ISO-8859-1 encoding.");
     }
     sb.Append(ch);
      }
      this.msg = sb.ToString(); //Not Unicode here!
      shape = SymbolShapeHint.FORCE_NONE;
      this.codewords = new StringBuilder(msg.Length);
      newEncoding = -1;
 }
Example #7
0
        public EncoderContext(String msg)
        {
            //From this point on Strings are not Unicode anymore!
            var msgBinary = encoding.GetBytes(msg);
            var sb        = new StringBuilder(msgBinary.Length);
            var c         = msgBinary.Length;

            for (int i = 0; i < c; i++)
            {
                // TODO: does it works in .Net the same way?
                var ch = (char)(msgBinary[i] & 0xff);
                if (ch == '?' && msg[i] != '?')
                {
                    throw new ArgumentException("Message contains characters outside " + encoding.WebName + " encoding.");
                }
                sb.Append(ch);
            }
            this.msg       = sb.ToString(); //Not Unicode here!
            shape          = SymbolShapeHint.FORCE_NONE;
            this.codewords = new StringBuilder(msg.Length);
            newEncoding    = -1;
        }
Example #8
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());
        }
Example #9
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>
        /// <param name="defaultEncodation">encoding mode to start with</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 &&
                encodingMode != Encodation.ASCII &&
                encodingMode != Encodation.BASE256 &&
                encodingMode != Encodation.EDIFACT)
            {
                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());
        }
Example #10
0
 public static SymbolInfo lookup(int dataCodewords,
                                 SymbolShapeHint shape,
                                 Dimension minSize,
                                 Dimension maxSize,
                                 bool fail)
 {
    foreach (SymbolInfo symbol in symbols)
    {
       if (shape == SymbolShapeHint.FORCE_SQUARE && symbol.rectangular)
       {
          continue;
       }
       if (shape == SymbolShapeHint.FORCE_RECTANGLE && !symbol.rectangular)
       {
          continue;
       }
       if (minSize != null
           && (symbol.getSymbolWidth() < minSize.Width
           || symbol.getSymbolHeight() < minSize.Height))
       {
          continue;
       }
       if (maxSize != null
           && (symbol.getSymbolWidth() > maxSize.Width
           || symbol.getSymbolHeight() > maxSize.Height))
       {
          continue;
       }
       if (dataCodewords <= symbol.dataCapacity)
       {
          return symbol;
       }
    }
    if (fail)
    {
       throw new ArgumentException(
           "Can't find a symbol arrangement that matches the message. Data codewords: "
               + dataCodewords);
    }
    return null;
 }
Example #11
0
 private static SymbolInfo lookup(int dataCodewords, SymbolShapeHint shape, bool fail)
 {
    return lookup(dataCodewords, shape, null, null, fail);
 }
Example #12
0
 public static SymbolInfo lookup(int dataCodewords, SymbolShapeHint shape)
 {
    return lookup(dataCodewords, shape, true);
 }
 public void setSymbolShape(SymbolShapeHint shape)
 {
     this.shape = shape;
 }
Example #14
0
 public static SymbolInfo lookup(int dataCodewords, SymbolShapeHint shape)
 {
     return(lookup(dataCodewords, shape, true));
 }
      /// <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();
        }
Example #17
0
 public void setSymbolShape(SymbolShapeHint shape)
 {
     this.shape = shape;
 }
Example #18
0
 private static SymbolInfo lookup(int dataCodewords, SymbolShapeHint shape, bool fail)
 {
     return(lookup(dataCodewords, shape, null, null, fail));
 }