UsrCustomlabeling.cs 9.2 KB
using Asa.FaceControl;
using BLL;
using DeviceLibrary.AutoScanAndLabel;
using DocumentFormat.OpenXml.Wordprocessing;
using MemoryRead;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace SmartScan.PlusSettingFrm
{
    public partial class UsrCustomlabeling : UserControl, ISetMenu
    {
        public static List<LabelingData> labelings = new List<LabelingData>();
        private List<MaterialTemplate> mateCopy;
        private int mateIndex = -1;
        string flie = Application.StartupPath + "\\LabelingFiles";
        public UsrCustomlabeling()
        {
            InitializeComponent();
            mateCopy = BLLCommon.mateEdit.ToCopy();
            LstMate.Items.AddRange(BLLCommon.mateEdit.Name);
            InitializedData();
            Language.SetLanguage(this);
        }

        static string BitmapToBase64(Bitmap bitmap)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                // 将Bitmap保存到MemoryStream中
                bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                // 获取字节数组
                byte[] byteArray = memoryStream.ToArray();

                // 将字节数组转换为Base64编码的字符串
                return Convert.ToBase64String(byteArray);
            }
        }
        public void InitializedData()
        {
            if (!File.Exists(flie))
            {
                //File.Create(flie);
                System.IO.Directory.CreateDirectory(flie);
            }
            var files = Directory.GetFiles(flie, "*.txt", SearchOption.TopDirectoryOnly);
            foreach (var file in files)
            {
                try
                {
                    string fileContent = File.ReadAllText(file);
                    LabelingData labelingData = JsonConvert.DeserializeObject<LabelingData>(fileContent);
                    if (labelingData != null)
                    {
                        labelings.Add(labelingData);
                    }
                }
                catch (Exception e)
                {
                    LogNet.log.Info(file + "加载贴标数据错误:" + e.Message);
                }
            }
        }
        public FacePanel GetPanel()
        {
            //BitmapData bitmapData = new BitmapData();
            //bitmapData.IsIDCamera = true;
            //bitmapData.X = 512; bitmapData.Y = 512;
            //Bitmap bitmap = new Bitmap("D:\\Desktop\\项目\\ns100\\测试条码\\7inch\\Kingbright2 - 50x70.jpg");
            ////bitmapData.ImageData = BitmapToBase64(bitmap);
            //List<BarcodeInfos> barcodeInfos = new List<BarcodeInfos>();
            //bitmapData.PlateW = 15;
            //var list = Camera.GetBarCode(bitmap);
            //list.ForEach(b => { barcodeInfos.Add(new BarcodeInfos { Angle = b.Angle, Text = b.Text, X = (int)b.Center.X, Y = (int)b.Center.Y }); });
            //bitmapData.BarCodeList = barcodeInfos;
            //var a = JsonConvert.SerializeObject(bitmapData);
            tableLayoutPanel1.Width = facePanel1.Width - 10;
            tableLayoutPanel1.Height = facePanel1.Height - 10;
            return facePanel1;
        }

        public void Save()
        {
            if (!File.Exists(flie))
            {
                System.IO.Directory.CreateDirectory(flie);
            }
            foreach (var item in labelings)
            {
                string json = JsonConvert.SerializeObject(item);
                string pathfile = flie + $"\\{item.Name}.txt";
                File.WriteAllText(pathfile, json);
            }
        }

        private void UsrCustomlabeling_Load(object sender, EventArgs e)
        {

        }

        private void LstMate_SelectedIndexChanged(object sender, EventArgs e)
        {
            LabelRotationAngle.Text = null;
            IsOverlaylabel.Checked = false;
            Xoffset.Text = null;
            Yoffset.Text = null;
            IscIockwise.Checked = false;
            IsCounterclockwise.Checked = false;
            RotationAngle.Text = null;
            LabelRotationAngle.Text = null;
            if (LstMate.SelectedIndex == -1) return;
            mateIndex = LstMate.SelectedIndex;
            BLLCommon.mateMaxCodeID = mateCopy[mateIndex].GetMaxID();
            string iamgepath = mateCopy[mateIndex].ImagePath;
            if (!string.IsNullOrEmpty(iamgepath))
            {
                if (System.IO.File.Exists(iamgepath))
                {
                    ImgShow.Image = ObjConversion.ReadImageFile(iamgepath);
                }
            }
            //ImgShow.Image = mateCopy[mateIndex].Image;
            ImgShow.CodeCenterClear();
            ImgShow.AddCodeCenter(LabelCenter());
            string name = mateCopy[mateIndex].Name;
            if (labelings == null)
            {
                return;
            }
            var lable = labelings.Where(a => a.Name == name).FirstOrDefault();
            if (lable != null)
            {
                IsOverlaylabel.Checked = lable.IsOverlaylabel;
                LabelRotationAngle.Text = lable.LabelRotationAngle.ToString();
                Xoffset.Text = lable.Xoffset.ToString();
                Yoffset.Text = lable.Yoffset.ToString();
                if (!IsOverlaylabel.Checked)
                {
                    IscIockwise.Checked = lable.IscIockwise;
                    IsCounterclockwise.Checked = lable.IsCounterclockwise;
                    RotationAngle.Text = lable.RotationAngle.ToString();
                }
            }
        }

        private PointF[] LabelCenter()
        {
            List<PointF> item = new();
            float x, y;
            for (int i = 0; i < mateCopy[mateIndex].Code.Count; i++)
            {
                x = mateCopy[mateIndex].Code[i].CenterX;
                y = mateCopy[mateIndex].Code[i].CenterY;
                item.Add(new PointF(x, y));
            }
            return item.ToArray();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string a = " True;匹配成功条码xy:X=1498;Y=1934,角度=120";
            //string name = "1";
            //AMatch aMatch = new AMatch();
            //aMatch.Angle = 120;
            //aMatch.Points = new Point(1498, 1934);
            //Point cenl = new Point(2000, 1800);
            //LabelingAngle_New(name, aMatch, cenl, true, 7, out int angle);
            if (mateIndex == -1)
            {
                return;
            }
            LabelingData labeling = new LabelingData();
            if (labelings != null)
            {
                var list = labelings.Where(a => a.Name == mateCopy[mateIndex].Name).ToList();
                foreach (var item in list) { labelings.Remove(item); }
            }


            labeling.Name = mateCopy[mateIndex].Name;
            labeling.IsOverlaylabel = IsOverlaylabel.Checked;
            labeling.Xoffset = Xoffset.Text != "" ? int.Parse(Xoffset.Text) : 0;
            labeling.Yoffset = Yoffset.Text != "" ? int.Parse(Yoffset.Text) : 0;
            if (!labeling.IsOverlaylabel)
            {
                labeling.IscIockwise = IscIockwise.Checked ? true : false;
                labeling.IsCounterclockwise = IsCounterclockwise.Checked ? true : false;
                labeling.RotationAngle = RotationAngle.Text != "" ? int.Parse(RotationAngle.Text) : 0;
                labeling.LabelRotationAngle = LabelRotationAngle.Text != "" ? int.Parse(LabelRotationAngle.Text) : 0;
            }        
            labelings.Add(labeling);
        }

        /// <summary>
        /// 获取以点为中心的角度,-180~180
        /// </summary>
        /// <param name="pt1"></param>
        /// <param name="pt2"></param>
        /// <returns></returns>
        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 int OldAngleAlgo(Point lable, Point center, int PlateW)
        {
            int labelAngle = 0;
            var a = (int)getAngle(lable, center);
            int angle = a < 0 ? 360 + a : a;
            if (PlateW == 15)
            {
                labelAngle = angle + 120;
            }
            else if (PlateW == 13)
            {
                labelAngle = angle + 180;
            }
            else
            {
                labelAngle = angle + 180;
            }
            labelAngle += 83;
            labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
            return labelAngle;
            //if (labelAngle <= 180)
            //{
            //    labelAngle = Label_R_Angle_Diff - (180 - labelAngle);
            //}
            //else
            //{
            //    labelAngle = (labelAngle - 180) + Label_R_Angle_Diff;
            //}
        }
    }
}