示例#1
0
        public void DataSenderTask()
        {
            Thread.Sleep(500);
            while (_alive)
            {
                try
                {
                    Img4kafka resultkafka = ResultImageQueue.GetFromQueue();
                    if (resultkafka == null)
                    {
                        Thread.Sleep(1000);
                        continue;
                    }
                    else
                    {
                        if (resultkafka.Picturepath != null && !string.IsNullOrEmpty(resultkafka.Tx1) && !string.IsNullOrEmpty(resultkafka.Rect))
                        {
                            string outputstr = "从队列接收数据-Tx1:" + resultkafka.Tx1 + ", Rect:" + resultkafka.Rect;
                            if (resultkafka.Picturepath.Length == 1)
                            {
                                outputstr += ", Picturepath0:" + resultkafka.Picturepath[0];
                            }
                            else if (resultkafka.Picturepath.Length == 2)
                            {
                                outputstr += ", Picturepath0:" + resultkafka.Picturepath[0] + ", Picturepath1:" + resultkafka.Picturepath[1];
                            }
                            Log4NetHelper.Instance.Debug(outputstr);

                            Bitmap bitmap   = null;
                            string errormsg = null;
                            bool   lbRet    = _processImager.GetImageByUrl(resultkafka.Tx1, out bitmap, out errormsg);
                            if (!lbRet)
                            {
                                Log4NetHelper.Instance.Info("图像数据不完整:" + resultkafka.Tx1 + "," + errormsg);
                                continue;
                            }

                            // 图片合成存储路径
                            string outputfile = Guid.NewGuid().ToString().Replace("-", "") + ".jpg";
                            string filedir    = _RecordFilePath + DateTime.Today.ToString("yyyy-M-d") + "\\";
                            if (!Directory.Exists(filedir))
                            {
                                Directory.CreateDirectory(filedir);
                            }

                            Rectangle cropRect = _processImager.GetRectangle(resultkafka.Rect);
                            if (cropRect.Width > 0 && cropRect.Height > 0)
                            {
                                // 根据rect进行截图处理,改为当前图像的1/2区域
                                var changeWidth = (int)((bitmap.Width / 2F - cropRect.Width) / 2F);
                                var changeHeight = (int)((bitmap.Height / 2F - cropRect.Height) / 2F);
                                int x1 = 0, y1 = 0;
                                if (cropRect.X > changeWidth)
                                {
                                    x1 = cropRect.X - changeWidth;
                                }
                                if (cropRect.Y > changeHeight)
                                {
                                    y1 = cropRect.Y - changeHeight;
                                }

                                // 判断是否越界,进行边界分析
                                if (x1 > (int)(bitmap.Width / 2F))
                                {
                                    x1 = (int)(bitmap.Width / 2F);
                                }
                                if (y1 > (int)(bitmap.Height / 2F))
                                {
                                    y1 = (int)(bitmap.Height / 2F);
                                }

                                Rectangle newcropRect = new Rectangle(x1, y1, (int)(bitmap.Width / 2F), (int)(bitmap.Height / 2F));
                                bool      lbCrop      = _processImager.CmdImage(bitmap, newcropRect, filedir + outputfile, out errormsg, _imageSpliceSort);
                                if (lbCrop)
                                {
                                    Log4NetHelper.Instance.Info("图像存储文件:" + outputfile);

                                    // 将数据进行组合后重新发送
                                    string outputPath = GetResultImageServerPath(filedir + outputfile);
                                    resultkafka.Tx1 = outputPath;
                                    if (resultkafka.PictureHttpPath != null && resultkafka.PictureHttpPath.Length > 0)
                                    {
                                        resultkafka.PictureHttpPath[0] = outputPath;
                                    }

                                    string resultjson = Newtonsoft.Json.JsonConvert.SerializeObject(resultkafka);
                                    ResultSpliceQueue.AddToQueue(resultjson);
                                }
                                else
                                {
                                    Log4NetHelper.Instance.Error("图像截取出现错误:" + resultkafka.Tx1 + "," + errormsg);
                                }
                            }
                            bitmap.Dispose();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log4NetHelper.Instance.Error("从队列接收数据错误:" + (ex.InnerException != null? ex.InnerException.Message : ex.Message));
                }
                Thread.Sleep(1000);
            }
        }
        public void Initial()
        {
            _task = Task.Run(() =>
            {
                var options1 = new KafkaOptions(new Uri(_kafkaAddr), new Uri(_kafkaAddr))
                {
                    Log = new ConsoleLog()
                };
                var consumer = new Consumer(new ConsumerOptions(_topicName, new BrokerRouter(options1))
                {
                    Log = new ConsoleLog()
                });

                // 从数据文件加载
                List <KafkaPartOffset> kafkaPartOffset;
                XmlDataControl.ReadConfig(out kafkaPartOffset);
                if (kafkaPartOffset != null && kafkaPartOffset.Count > 0)
                {
                    OffsetPosition[] offsetPositions = new OffsetPosition[kafkaPartOffset.Count];
                    for (int index = 0; index < kafkaPartOffset.Count; index++)
                    {
                        offsetPositions[index] = new OffsetPosition(kafkaPartOffset[index].PartitionId, kafkaPartOffset[index].Offset);
                    }
                    consumer.SetOffsetPosition(offsetPositions);
                }
                foreach (var data in consumer.Consume())
                {
                    if (_tokenSource.Token.IsCancellationRequested)
                    {
                        _tokenSource.Token.ThrowIfCancellationRequested();
                    }
                    else
                    {
                        Log4NetHelper.Instance.Debug("接收Kafka数据成功:" + data.Meta.PartitionId + "-" + data.Meta.Offset + ", data");
                        // 保存数据到配置文件
                        XmlDataControl.WriteConfig(data.Meta.PartitionId, data.Meta.Offset);

                        Send2Quere(data.Value);
                    }
                }
            }, _tokenSource.Token);

            _taskSend = Task.Run(() =>
            {
                var options2 = new KafkaOptions(new Uri(_kafkaAddr))
                {
                    Log = new ConsoleLog()
                };
                var producer = new Producer(new BrokerRouter(options2))
                {
                    BatchSize      = 10,
                    BatchDelayTime = TimeSpan.FromMilliseconds(2000)
                };
                while (true)
                {
                    try
                    {
                        string message = ResultSpliceQueue.GetFromQueue();
                        if (string.IsNullOrEmpty(message))
                        {
                            Thread.Sleep(1000);
                            continue;
                        }
                        else
                        {
                            producer.SendMessageAsync(_outputtopicName, new[] { new Message(message) }).Wait();
                            Console.WriteLine("Posted messages. AsyncCount:{0}", producer.AsyncCount);
                            Log4NetHelper.Instance.Debug("发送Kafka数据成功:" + message);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log4NetHelper.Instance.Error("从队列发送Kafka数据错误:" + (ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                    }
                    Thread.Sleep(1000);
                }
            }, _tokenSource.Token);
        }