Example #1
0
 public string EncodeSvgImage(string val, DmtxImageEncoderOptions options)
 {
     DmtxEncode encode = new DmtxEncode();
     encode.ModuleSize = options.ModuleSize;
     encode.MarginSize = options.MarginSize;
     encode.SizeIdxRequest = options.SizeIdx;
     encode.Scheme = options.Scheme;
     byte[] valAsByteArray = Encoding.ASCII.GetBytes(val);
     encode.EncodeDataMatrix(options.ForeColor, options.BackColor, valAsByteArray);
     return EncodeSvgFile(encode, "", options.ModuleSize, options.MarginSize, options.ForeColor, options.BackColor);
 }
Example #2
0
 private Bitmap EncodeImage(string val, DmtxImageEncoderOptions options, bool isMosaic)
 {
     DmtxEncode encode = new DmtxEncode();
     encode.ModuleSize = options.ModuleSize;
     encode.MarginSize = options.MarginSize;
     encode.Scheme = options.Scheme;
     encode.SizeIdxRequest = options.SizeIdx;
     byte[] valAsByteArray = Encoding.ASCII.GetBytes(val);
     if (isMosaic)
     {
         encode.EncodeDataMosaic(valAsByteArray);
     }
     else
     {
         encode.EncodeDataMatrix(options.ForeColor, options.BackColor, valAsByteArray);
     }
     return CopyDataToBitmap(encode.Image.Pxl, encode.Image.Width, encode.Image.Height);
 }
Example #3
0
        internal bool EncodeDataMosaic(byte[] inputString)
        {
            int dataWordCount;
            int tmpInputSize;
            int inputStartIndex;
            int[] splitInputSize = new int[3];
            DmtxSymbolSize sizeIdx, sizeIdxRequest;
            DmtxSymbolSize splitSizeIdxAttempt, splitSizeIdxFirst, splitSizeIdxLast;
            List<byte[]> buf = new List<byte[]>(3);
            for (int i = 0; i < 3; i++)
            {
                buf.Add(new byte[4096]);
            }
            DmtxEncode encGreen, encBlue;
            int row, col, mappingRows, mappingCols;

            /* 1) count how many codewords it would take to encode the whole thing
             * 2) take ceiling N of codeword count divided by 3
             * 3) using minimum symbol size that can accomodate N codewords:
             * 4) create several barcodes over iterations of increasing numbers of
             *    input codewords until you go one too far
             * 5) if codewords remain after filling R, G, and B barcodes then go back
             *    to 3 and try with next larger size
             * 6) take the 3 different images you created and write out a new barcode
             */

            /* Encode full input string to establish baseline data codeword count */
            sizeIdx = sizeIdxRequest = this._sizeIdxRequest;
            /* XXX buf can be changed here to use all 3 buffers' length */
            dataWordCount = EncodeDataCodewords(buf[0], inputString, ref sizeIdx);
            if (dataWordCount <= 0)
                return false;

            /* Use 1/3 (ceiling) of inputSize establish input size target */
            tmpInputSize = (inputString.Length + 2) / 3;
            splitInputSize[0] = tmpInputSize;
            splitInputSize[1] = tmpInputSize;
            splitInputSize[2] = inputString.Length - (splitInputSize[0] + splitInputSize[1]);
            /* XXX clean up above lines later for corner cases */

            /* Use 1/3 (floor) of dataWordCount establish first symbol size attempt */
            splitSizeIdxFirst = FindCorrectSymbolSize(tmpInputSize, sizeIdxRequest);
            if (splitSizeIdxFirst == DmtxSymbolSize.DmtxSymbolShapeAuto)
                return false;

            /* Set the last possible symbol size for this symbol shape or specific size request */
            if (sizeIdxRequest == DmtxSymbolSize.DmtxSymbolSquareAuto)
            {
                splitSizeIdxLast = DmtxSymbolSize.DmtxSymbol144x144;
            }
            else if (sizeIdxRequest == DmtxSymbolSize.DmtxSymbolRectAuto)
            {
                splitSizeIdxLast = DmtxSymbolSize.DmtxSymbol16x48;
            }
            else
            {
                splitSizeIdxLast = splitSizeIdxFirst;
            }

            /* XXX would be nice if we could choose a size and then fill up each
               layer as we go, but this can cause problems with all data fits on
               first 2 layers.  Revisit this later after things look a bit cleaner. */

            /* Try increasing symbol sizes until 3 of them can hold all input values */
            byte[] tmpRed = new byte[splitInputSize[0]];
            for (int i = 0; i < splitInputSize[0]; i++)
            {
                tmpRed[i] = inputString[i];
            }
            byte[] tmpGreen = new byte[splitInputSize[1]];
            for (int i = splitInputSize[0]; i < splitInputSize[0] + splitInputSize[1]; i++)
            {
                tmpGreen[i - splitInputSize[0]] = inputString[i];
            }
            byte[] tmpBlue = new byte[splitInputSize[2]];
            for (int i = splitInputSize[0] + splitInputSize[1]; i < inputString.Length; i++)
            {
                tmpBlue[i - splitInputSize[0] - splitInputSize[1]] = inputString[i];
            }

            for (splitSizeIdxAttempt = splitSizeIdxFirst; splitSizeIdxAttempt <= splitSizeIdxLast; splitSizeIdxAttempt++)
            {
                /* RED LAYER */
                sizeIdx = splitSizeIdxAttempt;
                inputStartIndex = 0;
                EncodeDataCodewords(buf[0], tmpRed, ref sizeIdx);
                if (sizeIdx != splitSizeIdxAttempt)
                    continue;

                /* GREEN LAYER */
                sizeIdx = splitSizeIdxAttempt;
                inputStartIndex += splitInputSize[0];
                EncodeDataCodewords(buf[1], tmpGreen, ref sizeIdx);
                if (sizeIdx != splitSizeIdxAttempt)
                    continue;

                /* BLUE LAYER */
                sizeIdx = splitSizeIdxAttempt;
                inputStartIndex += splitInputSize[1];
                EncodeDataCodewords(buf[2], tmpBlue, ref sizeIdx);
                if (sizeIdx != splitSizeIdxAttempt)
                    continue;

                break;
            }

            this._sizeIdxRequest = splitSizeIdxAttempt;

            /* Now we have the correct lengths for splitInputSize, and they all fit into the desired size */
            encGreen = new DmtxEncode(this);
            encBlue = new DmtxEncode(this);

            /* First encode red to the main encode struct (image portion will be overwritten) */
            EncodeDataMatrix(null, null, tmpRed);
            encGreen.EncodeDataMatrix(null, null, tmpGreen);
            encBlue.EncodeDataMatrix(null, null, tmpBlue);

            mappingRows = DmtxCommon.GetSymbolAttribute(DmtxSymAttribute.DmtxSymAttribMappingMatrixRows, splitSizeIdxAttempt);
            mappingCols = DmtxCommon.GetSymbolAttribute(DmtxSymAttribute.DmtxSymAttribMappingMatrixCols, splitSizeIdxAttempt);
            for (int i = 0; i < this._region.MappingCols * this._region.MappingRows; i++)
            {
                this._message.Array[i] = 0;
            }
            DmtxDecode.ModulePlacementEcc200(this._message.Array, this._message.Code, this._region.SizeIdx, DmtxConstants.DmtxModuleOnRed);
            //string s = Encoding.ASCII.GetString(_array);
            //Console.WriteLine(s);
            /* Data Mosaic will traverse this array multiple times -- reset
               DmtxModuleAssigned and DMX_MODULE_VISITED bits before starting */
            for (row = 0; row < mappingRows; row++)
            {
                for (col = 0; col < mappingCols; col++)
                {
                    this._message.Array[row * mappingCols + col] &= (byte)(0xff ^ (DmtxConstants.DmtxModuleAssigned | DmtxConstants.DmtxModuleVisited));
                }
            }

            DmtxDecode.ModulePlacementEcc200(this._message.Array, encGreen.Message.Code, this._region.SizeIdx, DmtxConstants.DmtxModuleOnGreen);

            /* Data Mosaic will traverse this array multiple times -- reset
               DmtxModuleAssigned and DMX_MODULE_VISITED bits before starting */
            for (row = 0; row < mappingRows; row++)
            {
                for (col = 0; col < mappingCols; col++)
                {
                    this._message.Array[row * mappingCols + col] &= (byte)(0xff ^ (DmtxConstants.DmtxModuleAssigned | DmtxConstants.DmtxModuleVisited));
                }
            }

            DmtxDecode.ModulePlacementEcc200(this._message.Array, encBlue.Message.Code, this._region.SizeIdx, DmtxConstants.DmtxModuleOnBlue);

            PrintPattern(null, null);

            return true;
        }