FrmLearning.cs 13.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
using Acc.ImageBox;
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
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 int CurrType = -2; 

        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;
            }


            if (LoadCSVLibrary.CSVBomManager.allComMap.ContainsKey(CurrPN))
            {
                ComponetInfo com = CSVBomManager.allComMap[CurrPN];
                lblDes.Text = "描述:" + com.Describe;
                lblFeng.Text = "封装:" + com.Encapsulations;
                lblPN.Text = "元器件编号:" + com.PartNum;
                lblProName.Text = "产品:" + com.ProductName;
            }
            else
            {
                lblPN.Text = "元器件编号:" + CurrPN;
                lblDes.Text = "--";
                lblFeng.Text = "--";
                lblProName.Text = "--";
            }

            timer1.Start();
        }
         
        private void ShowType(int type)
        {
            if (CurrType.Equals(type))
            {
                return;
            }
            CurrType = type;
            int index = 0;
            btnTest.Text = "请选择算法";
            bool findsuanfa = false;
            foreach (Button btn in SelBtnList)
            {
                if (index.Equals(type))
                {
                    findsuanfa = true;
                    btn.Text = gouStr + btn.Tag;
                    btnTest.Text = btn.Tag + "-点料测试";
                    btn.BackColor = Color.LightGreen;
                }
                else
                {
                    btn.Text = btn.Tag.ToString();
                    btn.BackColor = Color.White;
                }
                index++;
            } 

            btnTest.Enabled = findsuanfa;
            btnNext.Enabled = findsuanfa;  
        } 

        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 + "】 参数 " + param.ToStr() + "  结果:" + 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();

            DialogResult result2 = MessageBox.Show("保存成功!是否自动清理已配置算法的图片?\r\n" +
                "点击’确定‘:删除已配置算法的物料图片\r\n" +
                "点击’取消‘:暂不删除", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);

            if (result2.Equals(DialogResult.OK))
            {
                ClearImage();
            }
            IsSave = true;
            this.Close();
        }
        private void ClearImage()
        {
            try
            {
                if (!Directory.Exists(ImagePath))
                {
                    return;
                }
                string targetPath = ImagePath + @"\back\";
                if (!Directory.Exists(targetPath))
                {
                    Directory.CreateDirectory(targetPath);
                }
                string[] fileList = Directory.GetFiles(ImagePath);
                List<string> list = (from m in fileList orderby m ascending select m).ToList();
                foreach (string f in list)
                {
                    string houzhui = Path.GetExtension(f);
                    string filename = Path.GetFileNameWithoutExtension(f);
                    string[] array = filename.Split('-');
                    if (houzhui.Equals(".png") && array.Length >= 3)
                    {
                        string pn = array[0];
                        CountParam p = ParamManager.GetOptimalParamByPN(pn);
                        if (p != null&&p.Sign>=0)
                        {
                            string targetFile = targetPath + filename + houzhui;
                            File.Copy(f, targetPath + filename + houzhui, true);
                            File.Delete(f);
                            LogUtil.info("算法配置后清理:备份文件【" + f + "】到【" + targetFile + "】,并删除原文件");
                        }
                    } 
                }
            }catch(Exception ex)
            {
                LogUtil.error("ClearImage 出错:" + ex.ToString());
            }
        }
        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);
        }
    }
}