Common.cs 8.2 KB
using CodeLibrary;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    public class Common
    {
        /**获取角度*/
        // 求3点形成的夹角
        public static float getAngle2(Point pt1, Point pt2, Point pt0)
        {
            double ma_x = pt1.X - pt0.X;
            double ma_y = pt1.Y - pt0.Y;
            double mb_x = pt2.X - pt0.X;
            double mb_y = pt2.Y - pt0.Y;
            double k = (ma_x * mb_y - mb_x * ma_y);

            if (k != 0)
            {
                k = (k / Math.Sqrt(k * k));
            }
            else
            {
                k = 1;
            }

            double v1 = (ma_x * mb_x) + (ma_y * mb_y);
            double ma_val = Math.Sqrt(ma_x * ma_x + ma_y * ma_y);
            double mb_val = Math.Sqrt(mb_x * mb_x + mb_y * mb_y);
            double cosM = v1 / (ma_val * mb_val);

            return (float)(Math.Acos(cosM) * 180 / Math.PI * k);
        }
        public static double getAngle(Point pt1, Point pt2)
        {
            double mb_x = pt2.X - pt1.X;
            double mb_y = pt2.Y - pt1.Y;
            double rotation = Math.Atan2(mb_y, mb_x);
           

            rotation = rotation / Math.PI * 180;
            return rotation;
            //double angle = 360 - rotation + 90;

            //angle = angle < 0 ? 360 + angle : angle;
            //angle = angle % 360;
            //return Math.Floor(angle / 360 * 24);
        }

        public static double distance(Point p1, Point p2)
        {
            double result;
            result = Math.Sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y));
            return result;
        }

        /// <summary>
        /// 以中心点旋转Angle角度
        /// </summary>
        /// <param name="origin">中心点</param>
        /// <param name="reRotate">待旋转的点</param>
        /// <param name="angle">旋转角度, 逆时针</param>
        public static Point PointRotate(Point origin, Point rePoint, double angle)
        {

            double x = (rePoint.X - origin.X) * Math.Cos(angle) - (rePoint.Y - origin.Y) * Math.Sin(angle) + origin.X;
            double y = (rePoint.X - origin.X) * Math.Sin(angle) + (rePoint.Y - origin.Y) * Math.Cos(angle) + origin.Y;

            //rePoint.X = x;
            //rePoint.Y = y;
            return new Point((int)x, (int)y);
        }
        /// <summary>
        /// 以指定点为中心,测算指定角度,指定长度位置的点
        /// </summary>
        /// <param name="p1">原点</param>
        /// <param name="angle">角度</param>
        /// <param name="distance">距离</param>
        /// <returns></returns>
        public static Point PointWithAngle(Point p1, double angle, double distance) {
            var x2 = p1.X + distance * Math.Cos(angle / 180 * Math.PI);
            var y2 = p1.Y + distance * Math.Sin(angle / 180 * Math.PI);
            return new Point((int)x2, (int)y2);
        }

        public static Point CalcLabelPoint(LabelParam labelParam, Point org,int Label_R_Offset, out int labelAngle) {

            SolidBrush blue = new SolidBrush(Color.BlueViolet);
            SolidBrush red = new SolidBrush(Color.Red);
            SolidBrush yeelow = new SolidBrush(Color.Yellow);
            SolidBrush AliceBlue = new SolidBrush(Color.AliceBlue);
            Font font = new Font(FontFamily.GenericSansSerif, 60);
            Pen Greenpen = new Pen(Color.Green, 10);
            
            Bitmap srcbitmap;
            if (File.Exists(labelParam.bitmapfilename))
            {
                srcbitmap = new Bitmap(labelParam.bitmapfilename);
            }
            else {
                srcbitmap = new Bitmap(5472,3648);
            }
            

            Bitmap bitmap = new Bitmap(srcbitmap.Width, srcbitmap.Height, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(bitmap);

            g.DrawImage(srcbitmap, 0, 0);
            srcbitmap.Dispose();

            Size pointsize = new Size(30, 30);
            int widthOffset = 0;
            //Point Right_Batch_Point = new Point(RobotManage.Config.Right_Batch_X, RobotManage.Config.Right_Batch_Y);
            Point Right_Batch_Point = org;
            g.FillEllipse(blue, new Rectangle(Right_Batch_Point, pointsize));
            List<int> angles = new List<int>();
            labelParam.codeInfos.ForEach((c) => {
                //Point op = Common.PointRotate(Right_Batch_Point, new Point(c.X, c.Y), 83d / 180 * Math.PI);
                //c.X = op.X;
                //c.Y = op.Y;
                Point op = new Point(c.X, c.Y);
                g.FillEllipse(blue, new Rectangle(op, pointsize));
                var a = (int)getAngle(Right_Batch_Point, op);
                //a += ConfigHelper.Config.Get<int>("AngleChange");
                a = a < 0 ? 360 + a : a;

                //g.FillEllipse(red, new Rectangle(PointRotate(org, op, a), pointsize));
                angles.Add(a);
                widthOffset = (int)Common.distance(org, op);
            });
            angles.Sort();
            //if (angles.Count > 1) {
                angles.Add(360 + angles[0]);
            //}
            int biglen = 0;
            int bigangles = 0;
            for (int i = 0; i < angles.Count; i++) {
                if (angles.Count() - i < 2)
                    break;

                var x = angles[i+1] - angles[i];
                if (x > biglen)
                {
                    biglen = x;
                    bigangles = angles[i];
                }
            }
            if (labelParam.PlateW > 7 && angles.Count==2) {
                labelAngle = angles[0] + 120;
            }
            else
            {
                labelAngle = bigangles + biglen / 2;
            }
            labelAngle = labelAngle > 360 ? labelAngle- 360 : labelAngle;

            //g.FillEllipse(red, new Rectangle(PointRotate(org, op, labelAngle), pointsize));

            //贴标坐标
            var p2 = PointWithAngle(org, labelAngle, widthOffset);

            //贴标偏移
            var p3 = PointWithAngle(p2, labelAngle+180, Label_R_Offset);
            //贴标角度,右侧为0度,+180转换到左侧为0度
            labelAngle += 180;
            labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;

            g.FillEllipse(yeelow, new Rectangle(p2, pointsize));
            g.FillEllipse(red, new Rectangle(p3, pointsize));
            g.Save();
            g.Dispose();
            string dir = "/image/labeldebug/";
            Directory.CreateDirectory(dir);

            bitmap.Save(dir+DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") +".jpg",ImageFormat.Jpeg);
            bitmap.Dispose();

            return p3;
        }
        public T DeepClone<T>(T _object)
        {
            T dstobject;
            using (MemoryStream mStream = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(mStream, _object);
                mStream.Seek(0, SeekOrigin.Begin);//指定当前流的位置为流的开头。
                dstobject = (T)bf.Deserialize(mStream);
                mStream.Close();
            }
            return dstobject;
        }
    }
    public class Msg
    {
        public static List<Msg> msg = new List<Msg>(); 
        public string msgtxt;
        public MsgLevel msgLevel;
        public DateTime datetime;

        public static List<Msg> get() {
            if (_setlogones) {
                _setlogones = false;
                foreach (var m in msg)
                    LogUtil.info(m.msgtxt);
            }
            List<Msg> mm = new List<Msg>(msg);
            return mm;
        }
        public static void add(string m, MsgLevel ml)
        {
            msg.Add(new Msg { msgtxt = m, msgLevel = ml, datetime=DateTime.Now });
        }
        static bool _setlogones = false;
        internal static void setlogones()
        {
            _setlogones = true;
        }

        public static void clear()
        {
            msg.Clear();
        }
    }
    public enum MsgLevel {

        warning,
        info
    }
}