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);
            }

  csharp: use custom fonts in Emgu.CV_第1张图片

 

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

你可能感兴趣的:(csharp: use custom fonts in Emgu.CV)