LabelingPosition.cs 2.9 KB
using Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeviceLibrary.AutoScanAndLabel
{
    public static class LabelingPosition
    {
        /// <summary>
        /// 顺时针旋转
        /// </summary>
        /// <param name="lable">标签坐标</param>
        /// <param name="center">中心点</param>
        /// <param name="angle">旋转角度</param>
        /// <returns></returns>
        public static Point ClockwiseRotation(Point lable, Point center, double angle)
        {           
            //angle += 83;
            double theta = angle * Math.PI / 180;
            double x_new = (lable.X - center.X) * Math.Cos(theta) - (lable.Y - center.Y) * Math.Sin(theta) + center.X;
            double y_new = (lable.X - center.X) * Math.Sin(theta) + (lable.Y - center.Y) * Math.Cos(theta) + center.Y;
            LogNet.log.Info($"原坐标:{lable},中心点:{center},顺时针旋转:{angle}度,新坐标:x={x_new},y={y_new};");
            return new Point((int)x_new, (int)y_new);
        }

        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>
                 /// 以指定点为中心,测算指定角度,指定长度位置的点
                 /// </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);
        }
        static void ImageSave(Bitmap bitmap)
        {
            string filepath = Application.StartupPath + "\\image\\Labeling\\";
            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
            }
            else if (Directory.GetFiles(filepath).Length > 100)
            {
                try
                {
                    Directory.Delete(filepath, true);
                    Directory.CreateDirectory(filepath);
                }
                catch (Exception)
                {
                    LogNet.log.Info($"删除失败:{filepath}");
                }
            }

            Bitmap bitmaps = (Bitmap)bitmap.Clone();
            bitmaps.Save(filepath + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + DateTime.Now.Millisecond + ".jpg");
            //LabelResult?.Invoke(new LabelResult() { Bitmap = (Bitmap)bitmap.Clone() });
            //bitmap.Dispose();
        }
    }
}