Пример #1
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 void testRS()
        {
            //Sample from Annexe R in ISO/IEC 16022:2000(E)
            char[]     cw         = { (char)142, (char)164, (char)186 };
            SymbolInfo symbolInfo = SymbolInfo.lookup(3);
            String     s          = ErrorCorrection.encodeECC200(String.Join("", cw), symbolInfo);

            Assert.AreEqual("142 164 186 114 25 5 88 102", HighLevelEncodeTestCase.visualize(s));

            //"A" encoded (ASCII encoding + 2 padding characters)
            cw = new char[] { (char)66, (char)129, (char)70 };
            s  = ErrorCorrection.encodeECC200(String.Join("", cw), symbolInfo);
            Assert.AreEqual("66 129 70 138 234 82 82 95", HighLevelEncodeTestCase.visualize(s));
        }
Пример #3
0
        /// <summary>
        /// encodes the content to a BitMatrix
        /// </summary>
        /// <param name="contents"></param>
        /// <param name="format"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="hints"></param>
        /// <returns></returns>
        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 can't be negative: " + width + 'x' + height);
            }

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

            if (hints != null)
            {
                if (hints.ContainsKey(EncodeHintType.DATA_MATRIX_SHAPE))
                {
                    var requestedShape = hints[EncodeHintType.DATA_MATRIX_SHAPE];
                    if (requestedShape is SymbolShapeHint)
                    {
                        shape = (SymbolShapeHint)requestedShape;
                    }
                    else
                    {
                        if (Enum.IsDefined(typeof(SymbolShapeHint), requestedShape.ToString()))
                        {
                            shape = (SymbolShapeHint)Enum.Parse(typeof(SymbolShapeHint), requestedShape.ToString(), true);
                        }
                    }
                }
                var requestedMinSize = hints.ContainsKey(EncodeHintType.MIN_SIZE) ? hints[EncodeHintType.MIN_SIZE] as Dimension : null;
                if (requestedMinSize != null)
                {
                    minSize = requestedMinSize;
                }
                var requestedMaxSize = hints.ContainsKey(EncodeHintType.MAX_SIZE) ? hints[EncodeHintType.MAX_SIZE] as Dimension : null;
                if (requestedMaxSize != null)
                {
                    maxSize = requestedMaxSize;
                }
                if (hints.ContainsKey(EncodeHintType.DATA_MATRIX_DEFAULT_ENCODATION))
                {
                    var requestedDefaultEncodation = hints[EncodeHintType.DATA_MATRIX_DEFAULT_ENCODATION];
                    if (requestedDefaultEncodation != null)
                    {
                        defaultEncodation = Convert.ToInt32(requestedDefaultEncodation.ToString());
                    }
                }
            }


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

            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, width, height));
        }
Пример #4
0
        public void testSymbolInfo()
        {
            SymbolInfo info = SymbolInfo.lookup(3);

            Assert.AreEqual(5, info.errorCodewords);
            Assert.AreEqual(8, info.matrixWidth);
            Assert.AreEqual(8, info.matrixHeight);
            Assert.AreEqual(10, info.getSymbolWidth());
            Assert.AreEqual(10, info.getSymbolHeight());

            info = SymbolInfo.lookup(3, SymbolShapeHint.FORCE_RECTANGLE);
            Assert.AreEqual(7, info.errorCodewords);
            Assert.AreEqual(16, info.matrixWidth);
            Assert.AreEqual(6, info.matrixHeight);
            Assert.AreEqual(18, info.getSymbolWidth());
            Assert.AreEqual(8, info.getSymbolHeight());

            info = SymbolInfo.lookup(9);
            Assert.AreEqual(11, info.errorCodewords);
            Assert.AreEqual(14, info.matrixWidth);
            Assert.AreEqual(6, info.matrixHeight);
            Assert.AreEqual(32, info.getSymbolWidth());
            Assert.AreEqual(8, info.getSymbolHeight());

            info = SymbolInfo.lookup(9, SymbolShapeHint.FORCE_SQUARE);
            Assert.AreEqual(12, info.errorCodewords);
            Assert.AreEqual(14, info.matrixWidth);
            Assert.AreEqual(14, info.matrixHeight);
            Assert.AreEqual(16, info.getSymbolWidth());
            Assert.AreEqual(16, info.getSymbolHeight());

            try
            {
                SymbolInfo.lookup(1559);
                throw new AssertionException("There's no rectangular symbol for more than 1558 data codewords");
            }
            catch (ArgumentException)
            {
                //expected
            }
            try
            {
                SymbolInfo.lookup(50, SymbolShapeHint.FORCE_RECTANGLE);
                throw new AssertionException("There's no rectangular symbol for 50 data codewords");
            }
            catch (ArgumentException)
            {
                //expected
            }

            info = SymbolInfo.lookup(35);
            Assert.AreEqual(24, info.getSymbolWidth());
            Assert.AreEqual(24, info.getSymbolHeight());

            Dimension fixedSize = new Dimension(26, 26);

            info = SymbolInfo.lookup(35,
                                     SymbolShapeHint.FORCE_NONE, fixedSize, fixedSize, false);
            Assert.AreEqual(26, info.getSymbolWidth());
            Assert.AreEqual(26, info.getSymbolHeight());

            info = SymbolInfo.lookup(45,
                                     SymbolShapeHint.FORCE_NONE, fixedSize, fixedSize, false);
            Assert.IsNull(info);

            Dimension minSize = fixedSize;
            Dimension maxSize = new Dimension(32, 32);

            info = SymbolInfo.lookup(35,
                                     SymbolShapeHint.FORCE_NONE, minSize, maxSize, false);
            Assert.AreEqual(26, info.getSymbolWidth());
            Assert.AreEqual(26, info.getSymbolHeight());

            info = SymbolInfo.lookup(40,
                                     SymbolShapeHint.FORCE_NONE, minSize, maxSize, false);
            Assert.AreEqual(26, info.getSymbolWidth());
            Assert.AreEqual(26, info.getSymbolHeight());

            info = SymbolInfo.lookup(45,
                                     SymbolShapeHint.FORCE_NONE, minSize, maxSize, false);
            Assert.AreEqual(32, info.getSymbolWidth());
            Assert.AreEqual(32, info.getSymbolHeight());

            info = SymbolInfo.lookup(63,
                                     SymbolShapeHint.FORCE_NONE, minSize, maxSize, false);
            Assert.IsNull(info);
        }