FrmLearning.cs 11.1 KB
using Acc.ImageBox;
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OnlineStore.AutoCountClient
{
    public partial class FrmLearning : Form
    {
        public FrmLearning()
        {
            InitializeComponent();
        }
        private ImageBox currShowBox = null;
        private int CurrType = -1; 

        private string CurrFullFileName = "";
        private string CurrPN = "";
        internal string ImagePath = ParamManager.NoConfigPath;
        private List<Button> SelBtnList = new List<Button>();
   
        private string gouStr = "✔ ";
        private void FrmLearning_Load(object sender, EventArgs e)
        {
            SelBtnList.Add(buttonA);
            SelBtnList.Add(buttonB);
            SelBtnList.Add(buttonC);
            SelBtnList.Add(buttonD);
            SelBtnList.Add(buttonE);
            foreach(Button btn in SelBtnList)
            {
                btn.Tag = btn.Text;
            }
            LoadImgList();
        }

        private List<string> ImageList = new List<string>();
        private int currIndex = -1;
        private void LoadImgList()
        {
            if (!Directory.Exists(ImagePath))
            {
                MessageBox.Show("未找到文件夹:" + ImagePath);
                this.Close();
            }
            string[] fileList = Directory.GetFiles(ImagePath);

            lblFolder.Text = "当前文件夹:" + ImagePath;
            List<string> list = (from m in fileList orderby m ascending select m).ToList();

            foreach (string f in list)
            {
                string houzhui = Path.GetExtension(f);
                if (houzhui.Equals(".png"))
                {
                    ImageList.Add(f);
                }
            }

            if (ImageList.Count <= 0)
            {
                MessageBox.Show("未找到有效图片");
                this.Close();
            } 

            currIndex = -1;
            ShowNextImg();
            timer1_Tick(null, null);
        }

        public void ShowNextImg()
        {
            currIndex++;
            if (currIndex >= ImageList.Count)
            {
                btnNext.Enabled = false;
                MessageBox.Show("已经是最后一张图片");

                return;
            }
            string fullFName = ImageList[currIndex];

            string filename = Path.GetFileNameWithoutExtension(fullFName);


            string[] array = filename.Split('-');
            if (array.Length >= 3)
            {
                CurrPN = array[0];
            }
            groupBox4.Text = "[" + CurrPN + "] 请选择参数:";
            CurrFullFileName = fullFName;
            lblPnInfo.Text = fullFName;
            imgCurrImg.Image = KiLighten((Bitmap)ParamManager.FormImage(fullFName), 80);

            int winHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
            int winWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            imgCurrImg.Zoom = winWidth* 40 /1300;

            CountParam p = ParamManager.GetOptimalParamByPN(CurrPN);

            if (p != null)
            {
                numTh.Value = p.Threshold;
                numWSize.Value = p.WindowSize;
                ShowType(p.Sign);
            }
            else
            {
                ShowType(-1);
            }

            imgCurrImg.HorizontalScroll.Value = 173;

            if (currIndex >= ImageList.Count - 1)
            {
                btnNext.Enabled = false;
            }
            timer1.Start();
        }
         
        private void ShowType(int type)
        {
            if (CurrType.Equals(type))
            {
                return;
            }
            CurrType = type;
            int index = 0;
            btnTest.Text = "请选择算法";
            foreach (Button btn in SelBtnList)
            {
                if (index.Equals(type))
                {
                    btn.Text = gouStr + btn.Tag;
                    btnTest.Text = btn.Tag + "-点料测试";
                    btn.BackColor = Color.LightGreen;
                }
                else
                {
                    btn.Text = btn.Tag.ToString();
                    btn.BackColor = Color.White;
                }
                index++;
            } 
            bool isShowBtn = (type >= 0&&type<5);

            btnTest.Enabled = isShowBtn;
            btnNext.Enabled = isShowBtn;
            btnEnd.Enabled = true ; 

        } 

        private void btnTest_Click(object sender, EventArgs e)
        {
            int count = 1;
            if (imgCurrImg.Image == null || String.IsNullOrEmpty(CurrFullFileName)) 
            {
                MessageBox.Show("未加载图片,无法点料");
                return;
            }

            int index = 0;
            foreach (Button btn in SelBtnList)
            {
                if (index.Equals(CurrType))
                {
                    btnTest.Text = btn.Tag + "-点料测试";
                    break;
                }
                index++;
            }

            LogUtil.info( Name + "用户点击  "+btnTest.Text);
            try
            {

                btnTest.Enabled = false;
                this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
                int th = (int)numTh.Value;
                int wsize = (int)numWSize.Value;
                Asa.API.EyemImage tpDstImg;
                CountParam param = new CountParam("", th, wsize, CurrType);
                int outCount = RobotManager.robot.XrayBean.GetCountResult(CurrFullFileName, param, out tpDstImg);
                btnTest.Text +="      结果:"+ outCount.ToString();
                LogUtil.info(  "用户点击 "+ btnTest.Text + "【" + CurrFullFileName + "】【" + th + "】【" + wsize + "】 结果:" + outCount);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                LogUtil.error("点料测试出错:" + ex.ToString());
            }
            btnTest.Enabled = true;
            this.Cursor = System.Windows.Forms.Cursors.Default ;

        }
         

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Visible.Equals(false))
            {
                return;
            }
            lblFolder.Text = "当前文件夹:" + ImagePath;
            lblCountInfo.Text = "  第" + (currIndex + 1) + "张 / 共" + ImageList.Count + "张 "; 
            lblImgeInfo.Text = "Zoom:"+imgCurrImg.Zoom + ";" + imgCurrImg.HorizontalScroll.Value + ";" + imgCurrImg.VerticalScroll.Value;
        }

        private void  SaveCurrParam()
        {
            int th = (int)numTh.Value;
            int wsize = (int)numWSize.Value;
            CountParam p = new CountParam(CurrPN, th, wsize, CurrType, 0);
          
            ParamManager.UpdateParam(p);
        }
        private bool IsSave = false;
        private void btnSaveParam_Click(object sender, EventArgs e)
        {
            SaveCurrParam();
            int leftC = (ImageList.Count - currIndex - 1);
            if (leftC > 0)
            {
                DialogResult result = MessageBox.Show("当前第[" + (currIndex + 1) + "]张图片,还剩余[" + leftC + "]张图片未选择算法,是否结束学习?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
                if (result.Equals(DialogResult.Yes))
                {
                }
                else
                {
                    return;
                }
            }
            ParamManager.SaveMapToFile();
            IsSave = true;
            this.Close();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            btnNext.Enabled = false;
            this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                SaveCurrParam();
                ShowNextImg();
            }catch(Exception ex)
            {
                LogUtil.error("下一张 报错:" + ex.ToString());
            }
            btnNext.Enabled = true;
            this.Cursor = System.Windows.Forms.Cursors.Default;

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmLearning_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!IsSave)
            {
                DialogResult result = MessageBox.Show("是否保存参数更改?", "确认是否保存", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
                if (result.Equals(DialogResult.Yes))
                {
                    SaveCurrParam();
                    ShowNextImg();
                }
                else
                {
                    LogUtil.info("关闭界面时取消了 保存参数更改");
                }
            }
        }
 
        public static Bitmap KiLighten(Bitmap b, int degree)
        {
            if (b == null)
            {
                return null;
            }

            if (degree < -255) degree = -255;
            if (degree > 255) degree = 255;

            try
            {

                int width = b.Width;
                int height = b.Height;

                int pix = 0;

                BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                unsafe
                {
                    byte* p = (byte*)data.Scan0;
                    int offset = data.Stride - width * 3;
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            // 处理指定位置像素的亮度
                            for (int i = 0; i < 3; i++)
                            {
                                pix = p[i] + degree;

                                if (degree < 0) p[i] = (byte)Math.Max(0, pix);
                                if (degree > 0) p[i] = (byte)Math.Min(255, pix);

                            } // i
                            p += 3;
                        } // x
                        p += offset;
                    } // y
                }

                b.UnlockBits(data);

                return b;
            }
            catch
            {
                return null;
            }

        } // end of Lighten

        private void buttonA_Click(object sender, EventArgs e)
        {
            ShowType(0);
        }
        private void buttonB_Click(object sender, EventArgs e)
        {
            ShowType(1);
        }
        private void buttonC_Click(object sender, EventArgs e)
        {
            ShowType(2);
        }
        private void buttonD_Click(object sender, EventArgs e)
        {
            ShowType(3);
        }
        private void buttonE_Click(object sender, EventArgs e)
        {
            ShowType(4);
        }
    }
}