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);
            }
        }
Esempio n. 2
0
        private void DoPreview(Socket web_client, string[] routePathParams)
        {
            if (routePathParams.Length < 4)
            {
                return;
            }

            try
            {
                //预监路径输出路数索引
                string previewOutputChannel = routePathParams[2];

                Console.WriteLine("Data Request previewOutputChannel :" + previewOutputChannel);

                //报文头部
                string head      = Header();
                byte[] headBytes = Encoding.UTF8.GetBytes(head);
                web_client.Send(headBytes);

                //根据预监输出图像路数索引值去预监图像服务查找图像数据
                while (web_client.Connected)
                {
                    Bitmap screenPreviewBitmap = PreviewImageService.GetInstance().GetScreenPreviewBitmap(int.Parse(previewOutputChannel));
                    lock (screenPreviewBitmap)
                    {
                        if (screenPreviewBitmap == null)
                        {
                            web_client.Close();
                            return;
                        }

                        EncoderParameters encoderParameters = new EncoderParameters(1);
                        EncoderParameter  encoderParameter  = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 50L);
                        encoderParameters.Param[0] = encoderParameter;

                        MemoryStream ms = new MemoryStream();

                        screenPreviewBitmap.Save(ms, GetImageCodecInfo(ImageFormat.Jpeg), encoderParameters);

                        byte[] screenBitmapBytes = ms.GetBuffer();
                        ms.Close();
                        ms.Dispose();

                        byte[] payloadarray = Encoding.ASCII.GetBytes(PayloadHeader(screenBitmapBytes.Length));
                        web_client.Send(payloadarray);
                        //图像数据
                        web_client.Send(screenBitmapBytes);
                    }

                    Thread.Sleep(1000 / Program.frame);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("处理http请求断开" + Environment.NewLine + "\t" + ex.Message);
            }
            finally
            {
                web_client.Close(10);
                web_client.Dispose();

                ConsoleService.GetInstance().LogMsg("web_client.Close ", LogType.Log);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 接受处理http的请求
        /// </summary>
        /// <param name="ar"></param>
        private void OnAccept(IAsyncResult ar)
        {
            Socket web_client = null;

            try
            {
                Socket socket = ar.AsyncState as Socket;
                web_client = socket.EndAccept(ar);

                web_client.SendBufferSize = 1024;
                web_client.SendTimeout    = 10000;

                socket.BeginAccept(new AsyncCallback(OnAccept), socket);

                lock (_socketList)
                {
                    if (_socketList.IndexOf(web_client) == -1)
                    {
                        _socketList.Add(web_client);
                    }
                }

                byte[] recv_Buffer  = new byte[1024 * 640];
                int    recv_Count   = web_client.Receive(recv_Buffer);
                string recv_request = Encoding.UTF8.GetString(recv_Buffer, 0, recv_Count);
                Console.WriteLine("Data Request:" + recv_request); //将请求显示到界面

                ConsoleService.GetInstance().LogMsg("Data Request:" + recv_request, LogType.Log);

                string routePath = RouteHandle(recv_request);

                ConsoleService.GetInstance().LogMsg("Data Request routePath :" + routePath, LogType.Log);

                Console.WriteLine("Data Request routePath :" + routePath);

                //解析地址首字段 /preview/0/normal  , /setting/padding/0/left/right , /heart/

                string[] routePathParams = routePath.Split('/');
                //参数问题
                if (routePathParams.Length < 2)
                {
                    web_client.Close(10);
                }
                else
                {
                    string requestCmd = routePathParams[1];

                    if (requestCmd.Equals("preview"))
                    {
                        //预监
                        DoPreview(web_client, routePathParams);
                    }
                    else if (requestCmd.Equals("setting"))
                    {
                        //设置
                        DoSetting(web_client, routePathParams);
                    }
                    else if (requestCmd.Equals("heart"))
                    {
                        //设置
                        DoHeart(web_client, routePathParams);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("处理http请求断开" + Environment.NewLine + "\t" + ex.Message);
            }
            finally
            {
                lock (_socketList)
                {
                    if (_socketList.IndexOf(web_client) >= 0)
                    {
                        _socketList.Remove(web_client);
                    }
                }
                web_client.Close(10);
                web_client.Dispose();

                ConsoleService.GetInstance().LogMsg("web_client.Close ", LogType.Log);
            }
        }
Esempio n. 4
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();
            }
        }