Esempio n. 1
0
        /// <summary>
        /// 1bppビットマップファイルのバイトデータを返します。
        /// </summary>
        /// <param name="moduleSize">モジュールサイズ(px)</param>
        /// <param name="foreRgb">前景色</param>
        /// <param name="backRgb">背景色</param>
        private byte[] GetBitmap1bpp(int moduleSize, string foreRgb, string backRgb)
        {
            Color foreColor = ColorTranslator.FromHtml(foreRgb);
            Color backColor = ColorTranslator.FromHtml(backRgb);

            int[][] moduleMatrix = QuietZone.Place(GetModuleMatrix());

            int width, height;

            width = height = moduleSize * moduleMatrix.Length;

            int rowBytesLen = (width + 7) / 8;

            int pack8bit = 0;

            if (width % 8 > 0)
            {
                pack8bit = 8 - (width % 8);
            }

            int pack32bit = 0;

            if (rowBytesLen % 4 > 0)
            {
                pack32bit = 8 * (4 - (rowBytesLen % 4));
            }

            int rowSize = (width + pack8bit + pack32bit) / 8;

            byte[] bitmapData = new byte[rowSize * height];
            int    offset     = 0;

            for (int r = moduleMatrix.Length - 1; r >= 0; --r)
            {
                var bs = new BitSequence();

                foreach (int v in moduleMatrix[r])
                {
                    int color = Values.IsDark(v) ? 0 : 1;

                    for (int i = 1; i <= moduleSize; ++i)
                    {
                        bs.Append(color, 1);
                    }
                }
                bs.Append(0, pack8bit);
                bs.Append(0, pack32bit);

                byte[] bitmapRow = bs.GetBytes();

                for (int i = 1; i <= moduleSize; ++i)
                {
                    Array.Copy(bitmapRow, 0, bitmapData, offset, rowSize);
                    offset += rowSize;
                }
            }

            return(DIB.Build1bppDIB(bitmapData, width, height, foreColor, backColor));
        }
Esempio n. 2
0
        /// <summary>
        /// 行/列における1 : 1 : 3 : 1 : 1 比率パターンの失点を計算します。
        /// </summary>
        private static int CalcModuleRatio(int[][] moduleMatrix)
        {
            int[][] moduleMatrixTemp = QuietZone.Place(moduleMatrix);

            int penalty = 0;

            penalty += CalcModuleRatioInRow(moduleMatrixTemp);
            penalty += CalcModuleRatioInRow(ArrayUtil.Rotate90(moduleMatrixTemp));

            return(penalty);
        }
Esempio n. 3
0
        /// <summary>
        /// 24bppビットマップファイルのバイトデータを返します。
        /// </summary>
        /// <param name="moduleSize">モジュールサイズ(px)</param>
        /// <param name="foreRgb">前景色</param>
        /// <param name="backRgb">背景色</param>
        private byte[] GetBitmap24bpp(int moduleSize, string foreRgb, string backRgb)
        {
            Color foreColor = ColorTranslator.FromHtml(foreRgb);
            Color backColor = ColorTranslator.FromHtml(backRgb);

            int[][] moduleMatrix = QuietZone.Place(GetModuleMatrix());

            int width, height;

            width = height = moduleSize * moduleMatrix.Length;

            int rowSize = ((3 * width + 3) / 4) * 4;

            byte[] bitmapData = new byte[rowSize * height];
            int    offset     = 0;

            for (int r = moduleMatrix.Length - 1; r >= 0; --r)
            {
                byte[] bitmapRow = new byte[rowSize];
                int    index     = 0;

                foreach (int v in moduleMatrix[r])
                {
                    Color color = Values.IsDark(v) ? foreColor : backColor;

                    for (int i = 1; i <= moduleSize; ++i)
                    {
                        bitmapRow[index++] = color.B;
                        bitmapRow[index++] = color.G;
                        bitmapRow[index++] = color.R;
                    }
                }

                for (int i = 1; i <= moduleSize; ++i)
                {
                    Array.Copy(bitmapRow, 0, bitmapData, offset, rowSize);
                    offset += rowSize;
                }
            }

            return(DIB.Build24bppDIB(bitmapData, width, height));
        }
Esempio n. 4
0
        public string GetSvg(int moduleSize = DEFAULT_MODULE_SIZE,
                             string foreRgb = ColorCode.BLACK)
        {
            if (_dataBitCounter == 0)
            {
                throw new InvalidOperationException();
            }

            if (moduleSize < MIN_MODULE_SIZE)
            {
                throw new ArgumentOutOfRangeException(nameof(moduleSize));
            }

            if (ColorCode.IsWebColor(foreRgb) == false)
            {
                throw new FormatException(nameof(foreRgb));
            }

            int[][] moduleMatrix = QuietZone.Place(GetModuleMatrix());

            int width, height;

            width = height = moduleSize * moduleMatrix.Length;

            int[][] image = new int[height][];

            int r = 0;

            foreach (var row in moduleMatrix)
            {
                int[] imageRow = new int[width];
                int   c        = 0;

                foreach (var value in row)
                {
                    for (int j = 0; j < moduleSize; ++j)
                    {
                        imageRow[c] = value > Values.BLANK ? 1 : 0;
                        c++;
                    }
                }

                for (int i = 0; i < moduleSize; ++i)
                {
                    image[r] = imageRow;
                    r++;
                }
            }

            Point[][] gpPaths = GraphicPath.FindContours(image);
            var       buf     = new StringBuilder();
            string    indent  = new string(' ', 5);

            foreach (var gpPath in gpPaths)
            {
                buf.Append($"{indent}M ");

                foreach (var p in gpPath)
                {
                    buf.Append($"{p.X},{p.Y} ");
                }

                buf.AppendLine("Z");
            }

            string newLine = Environment.NewLine;
            string data    = buf.ToString().Trim();
            string svg     =
                $"<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"" + newLine +
                $"  width=\"{width}px\" height=\"{height}px\" viewBox=\"0 0 {width} {height}\">" + newLine +
                $"<path fill=\"{foreRgb}\" stroke=\"{foreRgb}\" stroke-width=\"1\"" + newLine +
                $"  d=\"{data}\" />" + newLine +
                $"</svg>";

            return(svg);
        }