1. 程式人生 > 實用技巧 >csharp: use custom fonts in Emgu.CV

csharp: use custom fonts in Emgu.CV

            // from https://stackoverflow.com/questions/35381238/how-to-use-custom-fonts-in-emgucv
            string text = "塗聚文(Geovin Du)";
            // 下面定義一個矩形區域
            int rectWidth = text.Length * (fontSize + 10);
            int rectHeight = fontSize + 10;


            //Draw detected area
            foreach (Rectangle face1 in faces)
            {
                // 宣告矩形域
               // Rectangle textArea = new Rectangle(face1.X + face1.Width, face1.Y, rectWidth, rectHeight);
                //grapPic.DrawString(text, font, whiteBrush, textArea);
                //image.Draw(textArea, new Bgr(Color.Red), 2);
                image.Draw(face1, new Bgr(Color.Red), 2); //


                //中文亂碼
                CvInvoke.PutText(image, "geovindu", new System.Drawing.Point(face1.X-25, face1.Y-25), Emgu.CV.CvEnum.FontFace.HersheyScriptComplex,2, new MCvScalar(255, 255, 0), 2);
                System.Drawing.Bitmap bmp;
                bmp = new System.Drawing.Bitmap(200, 45);
                Graphics g = Graphics.FromImage(bmp);
                Font drawFont = new Font("宋體", 24, FontStyle.Bold);
                g.DrawString(text, drawFont, Brushes.Red, new PointF(0, 0));
                g.Save();
                for (int i = 0; i < 200; i++)
                {
                    for (int j = 0; j < 45; j++)
                    {
                        Color c = bmp.GetPixel(i, j);
                        if (c.R > 0 || c.B > 0 || c.G > 0)
                        {
                            CvInvoke.cvSet2D(image, j + 10 + 200, i + 200, new MCvScalar(c.B, c.G, c.R)); //修改對應畫素值
                        }
                    }
                }
                //自定義字型 Geovin Du
                // 'PrivateFontCollection' is in the 'System.Drawing.Text' namespace
                var foo = new PrivateFontCollection();
                // 
                string fonfile =Environment.CurrentDirectory+ @"/font/迷你繁篆書.ttf";
                foo.AddFontFile(fonfile);
                var myCustomFont = new Font((FontFamily)foo.Families[0], 36f);
                //Emgu.CV.CvEnum.FontFace.HersheyScriptComplex
                System.Drawing.Bitmap bmpd;
                bmpd = new System.Drawing.Bitmap(200, 45);
                Graphics gg = Graphics.FromImage(bmpd);
                gg.DrawString("塗聚文 Geovin Du", myCustomFont, Brushes.Red, new PointF(0, 0));
                gg.Save();
                //根據圖片的大小
                for (int i = 0; i < 200; i++)
                {
                    for (int j = 0; j < 45; j++)
                    {
                        Color c = bmpd.GetPixel(i, j);
                        if (c.R > 0 || c.B > 0 || c.G > 0)
                        {
                            CvInvoke.cvSet2D(image, j + 10 + 500, i +500, new MCvScalar(c.B, c.G, c.R)); //修改對應畫素值
                        }
                    }
                }



            }
            foreach (Rectangle eye1 in eyes)
            { 
                image.Draw(eye1, new Bgr(Color.Blue), 2);
            }

  

https://www.nuget.org/packages/EmguCV