Ejemplo n.º 1
0
        /// <summary>
        /// シンボルをSVG形式でファイルに保存します。
        /// </summary>
        /// <param name="fileName">ファイル名</param>
        /// <param name="moduleSize">モジュールサイズ(px)</param>
        /// <param name="foreRgb">前景色</param>
        public void SaveSvg(string fileName,
                            int moduleSize = DEFAULT_MODULE_SIZE,
                            string foreRgb = ColorCode.BLACK)
        {
            if (_dataBitCounter == 0)
            {
                throw new InvalidOperationException();
            }

            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }

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

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

            string newLine = Environment.NewLine;

            string svg = GetSvg(moduleSize, foreRgb) + newLine;

            File.WriteAllText(fileName, svg);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// ビットマップファイルのバイトデータを返します。
        /// </summary>
        /// <param name="moduleSize">モジュールサイズ(px)</param>
        /// <param name="monochrome">1bitカラーはtrue、24bitカラーはfalseを設定します。</param>
        /// <param name="foreRgb">前景色</param>
        /// <param name="backRgb">背景色</param>
        public byte[] GetBitmap(int moduleSize  = DEFAULT_MODULE_SIZE,
                                bool monochrome = false,
                                string foreRgb  = ColorCode.BLACK,
                                string backRgb  = ColorCode.WHITE)
        {
            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));
            }

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

            if (monochrome)
            {
                return(GetBitmap1bpp(moduleSize, foreRgb, backRgb));
            }
            else
            {
                return(GetBitmap24bpp(moduleSize, foreRgb, backRgb));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// シンボルをBMP形式でファイルに保存します。
        /// </summary>
        /// <param name="fileName">ファイル名</param>
        /// <param name="moduleSize">モジュールサイズ(px)</param>
        /// <param name="monochrome">1bitカラーはtrue、24bitカラーはfalseを設定します。</param>
        /// <param name="foreRgb">前景色</param>
        /// <param name="backRgb">背景色</param>
        public void SaveBitmap(string fileName,
                               int moduleSize  = DEFAULT_MODULE_SIZE,
                               bool monochrome = false,
                               string foreRgb  = ColorCode.BLACK,
                               string backRgb  = ColorCode.WHITE)
        {
            if (_dataBitCounter == 0)
            {
                throw new InvalidOperationException();
            }

            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }

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

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

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

            byte[] dib;

            if (monochrome)
            {
                dib = GetBitmap1bpp(moduleSize, foreRgb, backRgb);
            }
            else
            {
                dib = GetBitmap24bpp(moduleSize, foreRgb, backRgb);
            }

            File.WriteAllBytes(fileName, dib);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// シンボルのImageオブジェクトを返します。
        /// </summary>
        /// <param name="moduleSize">モジュールサイズ(px)</param>
        /// <param name="monochrome">1bitカラーはtrue、24bitカラーはfalseを設定します。</param>
        /// <param name="foreRgb">前景色</param>
        /// <param name="backRgb">背景色</param>
        public System.Drawing.Image GetImage(int moduleSize  = DEFAULT_MODULE_SIZE,
                                             bool monochrome = false,
                                             string foreRgb  = ColorCode.BLACK,
                                             string backRgb  = ColorCode.WHITE)
        {
            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));
            }

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

            byte[] dib;

            if (monochrome)
            {
                dib = GetBitmap1bpp(moduleSize, foreRgb, backRgb);
            }
            else
            {
                dib = GetBitmap24bpp(moduleSize, foreRgb, backRgb);
            }

            ImageConverter converter = new ImageConverter();

            return((System.Drawing.Image)converter.ConvertFrom(dib));
        }
Ejemplo n.º 5
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);
        }