FrmCodeSplicing.cs 12.0 KB

using CodeLibrary;
using HalconDotNet; 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace CodeSplicing
{
    public partial class FrmCodeSplicing : Form
    {
        private List<CodeInfo> CodeResult = new List<CodeInfo>();
        private String SplicingStr = "";
        private HObject Ho_Image = null;
        private PylonCamera camera = new PylonCamera();
        private Stopwatch stopwatch = new Stopwatch();
        public FrmCodeSplicing()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {

            cmbCount.SelectedIndex = 0;
            string[] camerName = camera.GetName();
            cmbCamera.Items.Clear();
            foreach (string str in camerName)
            {
                cmbCamera.Items.Add(str);
            }
            if (camerName.Length > 0)
            {
                cmbCamera.SelectedIndex = 0;
            }
            HDLogUtil.logBox = this.richTextBox1;

        }
        private void btnSelImage_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
            openDialog.Title = "选择二维码图片";
            openDialog.Filter = "图片(*.jpg)|*.jpg|图片(*.png)|*.png";
            //openDialog.DefaultExt = "png";
            System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            txtPath.Text = openDialog.FileName;
            string filename = txtPath.Text;
            if (string.IsNullOrEmpty(filename))
            {
                MessageBox.Show("获取二维码图片为空");
            }
            pictureBox1.Image = null;
            //读取图片内容
            Image img = (Image)Image.FromFile(filename).Clone();
            pictureBox1.Image = img;
            Ho_Image = HDCodeHelper.Bitmap2HObjectBpp24(new Bitmap(img));
            ShowImage(Ho_Image);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null || txtPath.Text.Equals(""))
            {
                MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Bitmap map = new Bitmap(pictureBox1.Image);
            Bitmap newMap = ImageHelper.ConvertTo1Bpp1(map);
            pictureBox1.Image = newMap;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null || txtPath.Text.Equals(""))
            {
                MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Bitmap map = new Bitmap(pictureBox1.Image);
            Bitmap newMap = ImageHelper.ToGray(map);
            pictureBox1.Image = newMap;
        }


        private void ShowCode(List<CodeInfo> list)
        { 
            list = (from m in list orderby m.Y descending select m).ToList<CodeInfo>();
            SplicingStr = "RealID:";
            if (list.Count > 0)
            {
                HDLogUtil.info("耗时:" + stopwatch.Elapsed.ToString()+"识别到二维码:");
                foreach (CodeInfo code in list)
                {
                    string codeStr = code.CodeStr;
                   HDLogUtil.info(  " (X:" + code.X + ",Y:" + code.Y + ") " + codeStr);
                    SplicingStr += code.CodeStr + "*";

                    if (codeStr.Contains("-") || codeStr.Contains(" "))
                    {
                        txtPn.Text = codeStr;
                    }
                    else if (Regex.IsMatch(code.CodeStr, "^\\+?[1-9][0-9]*$"))
                    {
                        if (code.CodeStr.Length < 18)
                        {
                            txtQTY.Text = codeStr;
                        }
                        else
                        {
                            txtDate.Text = codeStr;
                        }
                    }
                    else if (codeStr.Length > 2 && Regex.IsMatch(codeStr.Substring(0, 2), "^[A-Za-z]+$"))
                    {
                        txtLOT.Text = codeStr;
                    }
                }
            }
            else
            {
               HDLogUtil.info("未识别到二维码");
                SplicingStr = "";
            }
            if (SplicingStr.EndsWith("*"))
            {
                SplicingStr = SplicingStr.Substring(0, SplicingStr.Length - 1);
            }
            lblResult.Text = SplicingStr;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            HDLogUtil.ClearLog();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int index = cmbCamera.SelectedIndex;
            if (index < 0)
            {
                MessageBox.Show("请先选择相机");
                return;
            }
            camera.Open(index);
            Bitmap bit = camera.syncShot();
            if (bit != null)
            {
                pictureBox1.Image = bit;
            }
            camera.Close();
        }

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

        private void button1_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null || txtPath.Text.Equals(""))
            {
                MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Bitmap map = new Bitmap(pictureBox1.Image);
            Bitmap newMap = ImageHelper.KiLighten(map, 10);
            pictureBox1.Image = newMap;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null || txtPath.Text.Equals(""))
            {
                MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Bitmap map = new Bitmap(pictureBox1.Image);
            Bitmap newMap = ImageHelper.KiLighten(map, -10);
            pictureBox1.Image = newMap;
        }

        private void cmbCodeType_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void btnHalconP_Click(object sender, EventArgs e)
        {
            if (Ho_Image == null)
            {
                MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            txtRealID.Text = "";
            txtPn.Text = "";
            txtMPN.Text = "";
            txtQTY.Text = "";
            txtDate.Text = "";
            txtLOT.Text = "";
            lblResult.Text = "";
            int count = cmbCount.SelectedIndex + 1;
            HDLogUtil.ClearLog();
            stopwatch.Restart();
            HDCodeHelper.HalconWindow = this.hWindowControl1.HalconWindow;
            CodeResult = HDCodeHelper.DecodeBarCode(Ho_Image);
            ShowCode(CodeResult);

            if (chbIsAuto.Checked&& SplicingStr.Equals("").Equals(false))
            {
                btnSplicing_Click(null, null);
            }
         
        }
        public void ShowImage(HObject ho_Image)
        {
            HTuple width, height;
            HOperatorSet.GetImageSize(ho_Image, out width, out height);

            int dWidth = (int)width.D;
            int dHeight = (int)height.D;
            this.hWindowControl1.HalconWindow.SetPart(0, 0, dHeight, dWidth);
            HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
        }
        private Stopwatch createStopWarch = new Stopwatch();
        private bool isInProcess = false;
        private void btnSplicing_Click(object sender, EventArgs e)
        {
            string currStr = SplicingStr;
            string filePath = Application.StartupPath + @"\codeImage\";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            if (currStr.Equals(""))
            {
                MessageBox.Show("请先解析条码!", "提示");
                return;
            }
            if (isInProcess)
            {
                MessageBox.Show("请等待上个二维码生成成功后再操作!", "提示");
                return;
            }
            isInProcess = true;
           //HDLogUtil.info(  "试图生成二维码:" + currStr);
            lblImageText.Text = "生成中:" + currStr;

            Task.Factory.StartNew(delegate ()
            {
                createStopWarch.Restart();
                picCodeResult.Image = null;
                Bitmap bitmap = CodeCreate.DMCode(currStr, 10);
                picCodeResult.Image = (Bitmap)bitmap.Clone();
                lblImageText.Text = "" + currStr;

               HDLogUtil.info(  "生成:" + currStr + "  结束,耗时:" + createStopWarch.Elapsed.ToString());
                string imageName= ReplaceName(currStr + ".jpg");
                string imageFile = filePath + imageName;
                try
                {
              
                    if (File.Exists(imageFile))
                    {
                        File.Delete(imageFile);
                    }
                    bitmap.Save(imageFile);
                   HDLogUtil.info( "保存二维码到文件:"+ imageName);
                }
                catch (Exception ex)
                {
                   HDLogUtil.info("保存二维码到文件:" + imageName + "失败:"+ex.ToString()); 
                }
                isInProcess = false;
            });

        }
        public string ReplaceName(string filename)
        {
            string str = filename;
            str = str.Replace("\\", string.Empty);
            str = str.Replace("/", string.Empty);
            str = str.Replace(":", string.Empty);
            str = str.Replace("*", string.Empty);
            str = str.Replace("?", string.Empty);
            str = str.Replace("\"", string.Empty);
            str = str.Replace("<", string.Empty);
            str = str.Replace(">", string.Empty);
            str = str.Replace("|", string.Empty);
            str = str.Replace(" ", string.Empty);    //前面的替换会产生空格,最后将其一并替换掉
            return str;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string filePath = Application.StartupPath + @"\codeImage\A00\";
            for (int i = 1; i <= 50; i++)
            {
                string currStr = "A000" + i.ToString().PadLeft(2, '0');

                Bitmap bitmap = CodeCreate.ZXingCode(currStr, 240,ZXing.BarcodeFormat.QR_CODE);
                picCodeResult.Image = (Bitmap)bitmap.Clone();
                lblImageText.Text = "" + currStr;

                HDLogUtil.info("生成:" + currStr + "  结束,耗时:" + createStopWarch.Elapsed.ToString());
                string imageName = ReplaceName(currStr + ".jpg");
                string imageFile = filePath + imageName;
                try
                {

                    if (File.Exists(imageFile))
                    {
                        File.Delete(imageFile);
                    }
                    bitmap.Save(imageFile);
                    HDLogUtil.info("保存二维码到文件:" + imageName);
                }
                catch (Exception ex)
                {
                    HDLogUtil.info("保存二维码到文件:" + imageName + "失败:" + ex.ToString());
                }
            }
        }
    }
}