public RestfulScreenData GetRestfulScreenData(int screenId)
        {
            RestfulScreenData screenData = null;

            foreach (var restfulScreenData in screenDataList)
            {
                if (int.Parse(restfulScreenData.id) == screenId)
                {
                    screenData = restfulScreenData;
                    break;
                }
            }

            return(screenData);
        }
        private void CaptureScreenTask()
        {
            while (running)
            {
                //检查是否有连接
                lock (Program.mjpegServer.SocketList)
                {
                    if (Program.mjpegServer.SocketList.Count == 0)
                    {
                        Thread.Sleep(1000 / Program.frame);

                        continue;
                    }
                }

                int previewGridCount = Program.gridX * Program.gridY;

                int previewTotal = 0;
                lock (screenDataList)
                {
                    previewTotal = ((screenDataList.Count - 1) / previewGridCount) + 1;
                }

                for (int outputChannel = 0; outputChannel < previewTotal; outputChannel++)
                {
                    Bitmap outputPreviewBitmap = GetScreenPreviewBitmap(outputChannel);
                    lock (outputPreviewBitmap)
                    {
                        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(outputPreviewBitmap);

                        //清空画布并以透明背景色填充
                        g.Clear(System.Drawing.Color.Transparent);

                        for (int oid = 1; oid <= previewGridCount; oid++)
                        {
                            int screenId = oid + outputChannel * previewGridCount;

                            RestfulScreenData restfulScreenData = GetRestfulScreenData(screenId);

                            if (restfulScreenData == null)
                            {
                                continue;
                            }

                            int previewStartId = ((screenId - 1) - outputChannel * previewGridCount);
                            int targetX        = previewStartId % Program.gridX;
                            int targetY        = previewStartId / Program.gridX;

                            //左右偏移
                            Rectangle captuRectangle = new Rectangle(int.Parse(restfulScreenData.x) - int.Parse(restfulScreenData.paddingLeft),
                                                                     int.Parse(restfulScreenData.y),
                                                                     int.Parse(restfulScreenData.width) + int.Parse(restfulScreenData.paddingLeft) + int.Parse(restfulScreenData.paddingRight),
                                                                     int.Parse(restfulScreenData.height));

                            try
                            {
                                Bitmap screenBitmap = ImageHelper.CaptureScreen(captuRectangle);

                                //设置高质量插值法
                                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;

                                //设置高质量,低速度呈现平滑程度
                                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                                Rectangle targetRectangle = new System.Drawing.Rectangle(
                                    targetX * Program.previewWidth / Program.gridX,
                                    targetY * Program.previewheight / Program.gridY, Program.previewWidth / Program.gridX,
                                    Program.previewheight / Program.gridY);

                                ImageHelper.MakeThumbnail(targetRectangle, screenBitmap, g);

                                Console.WriteLine("###CaptureScreenTask previewStartId " + previewStartId + " targetRectangle.x " + targetRectangle.X + " targetRectangle.y " + targetRectangle.Y);

                                //是否分屏幕图片内存
                                screenBitmap.Dispose();
                            }
                            catch (Exception e)
                            {
                                ConsoleService.GetInstance().LogMsg(" CaptureScreenTask " + e.StackTrace, LogType.Error);
                            }
                        }

                        g.Dispose();
                    }
                }

                Thread.Sleep(1000 / Program.frame);
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            ConsoleService.GetInstance().Init();
            ConsoleService.GetInstance().LogMsg(" Start Mjpeg Server", LogType.Log);

            string configXml = "./AppScreensSettings.xml";

            if (args.Length >= 7)
            {
                port          = int.Parse(args[0]);
                gridX         = int.Parse(args[1]);
                gridY         = int.Parse(args[2]);
                previewWidth  = int.Parse(args[3]);
                previewheight = int.Parse(args[4]);
                frame         = int.Parse(args[5]);
                configXml     = args[6];

                ConsoleService.GetInstance().LogMsg(" Start Mjpeg Server args " + args[0] + " " +
                                                    args[1] + " " +
                                                    args[2] + " " +
                                                    args[3] + " " +
                                                    args[4] + " " +
                                                    args[5] + " " +
                                                    args[6], LogType.Log);
            }
            else
            {
                //warnning
                ConsoleService.GetInstance().LogMsg(" Start Mjpeg Server without args ", LogType.Log);
            }

            ConsoleService.GetInstance().LogMsg(" XmlDocument load " + configXml, LogType.Log);

            try
            {
                var xmlDoc = new XmlDocument();
                xmlDoc.Load(configXml);
                XmlElement  root = xmlDoc.DocumentElement;
                XmlNodeList xmlScreenNodeList = root.SelectNodes("/AppSettings/Screes/Screen");
                foreach (XmlNode screenNode in xmlScreenNodeList)
                {
                    XmlNode curXmlNode = screenNode.FirstChild;
                    string  id         = curXmlNode.InnerText;
                    curXmlNode = curXmlNode.NextSibling;

                    string x = curXmlNode.InnerText;
                    curXmlNode = curXmlNode.NextSibling;

                    string y = curXmlNode.InnerText;
                    curXmlNode = curXmlNode.NextSibling;

                    string width = curXmlNode.InnerText;
                    curXmlNode = curXmlNode.NextSibling;

                    string height = curXmlNode.InnerText;

                    RestfulScreenData screenData = new RestfulScreenData();
                    screenData.id           = id;
                    screenData.x            = x;
                    screenData.y            = y;
                    screenData.width        = width;
                    screenData.height       = height;
                    screenData.paddingRight = "0";
                    screenData.paddingLeft  = "0";
                    screenNodeList.Add(screenData);
                }

                //启动预监服务器
                mjpegServer = new MjpegServer(port);
                mjpegServer.Start();

                ConsoleService.GetInstance().LogMsg(" Start PreviewImageService", LogType.Log);

                //启动预监画面服务
                PreviewImageService.GetInstance().Init(screenNodeList);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);

                ConsoleService.GetInstance().LogMsg("Start Error " + e.Message, LogType.Error);
            }

            string cmd = Console.ReadLine();

            while (!cmd.Equals("bye"))
            {
                cmd = Console.ReadLine();
            }
        }