FrmMain.cs 8.8 KB

using CodeLibrary;
using HalconDotNet;
using Common; 
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;

namespace CodeTest
{
    public partial class FrmMain : Form
    {
        private PylonCamera camera = new PylonCamera();
        private Stopwatch stopwatch = new Stopwatch();
        public FrmMain()
        {
            InitializeComponent();
        }
        public static string GetParamFilePathName(string codeType)
        {
            string appPath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.CodeParamPath);
            if (!Directory.Exists(appPath))
            {
                Directory.CreateDirectory(appPath);
            }

            string filePath = appPath + codeType + ".dcm";
            return filePath;
        }
        public static string GetCodeParamFilePath(string codeType)
        {
            string filePath = GetParamFilePathName(codeType);
            if (File.Exists(filePath))
            {
                return filePath;
            }
            else
            {
                return "";
            }
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            chbUseParam.Checked= HDCodeHelper.IsUseParam ;
            HDCodeLearnHelper.LoadConfig(ConfigAppSettings.GetValue(Setting_Init.CameraName), ConfigAppSettings.GetValue(Setting_Init.CodeType));
            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;
            }
            cmbCodeType.DataSource = HDCodeLearnHelper.codeTypeList;
            if (HDCodeLearnHelper.codeTypeList.Count > 0)
            {
                cmbCodeType.SelectedIndex = 0;
            }
            else
            {
                cmbCodeType.Items.Add("QR Code");
                cmbCodeType.SelectedIndex = 0;
            }
        }
        private void btnSelImage_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
            openDialog.Title = "选择二维码图片";
            openDialog.Filter = "图片(*.png)|*.png|图片(*.jpg)|*.jpg";
            //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;
        }
         
        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<string> list)
        {
            if (list.Count > 0)
            {
                txtResult.Text += "\r\n识别到二维码:";
                foreach (string str in list)
                {
                    txtResult.Text += "\r\n" + str;
                }
            }
            else
            {
                txtResult.Text += "\r\n未识别到二维码";
            }
        }

        private void btnHalconP_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null||txtPath.Text.Equals(""))
            {
                MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int count = cmbCount.SelectedIndex + 1;
            txtResult.Text = "";
            stopwatch.Restart();
            HDCodeHelper.HalconWindow = this.hWindowControl1.HalconWindow;
            List<string> result = HDCodeHelper.DecodeCode(txtPath.Text, count, cmbCodeType.Text);
            ShowCode(result); 
            txtResult.Text += "\r\n扫码结束耗时:" + stopwatch.Elapsed.ToString();
        }
        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 void button5_Click(object sender, EventArgs e)
        {
            FrmCodeLearn frm = new FrmCodeLearn();
            frm.ShowDialog();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int count = cmbCount.SelectedIndex + 1;
            txtResult.Text = "";
            stopwatch.Restart();

            Bitmap map = new Bitmap(pictureBox1.Image);
            HObject ho_image = HDCodeHelper.Bitmap2HObjectBpp24(map);

            txtResult.Text += "\r\n转换图片耗时:" + stopwatch.Elapsed.ToString();
            hWindowControl1.HalconWindow.SetPart(0, 0, map.Height, map.Width);
            HOperatorSet.DispObj(ho_image, hWindowControl1.HalconWindow);
            ShowImage(ho_image);
            HDCodeHelper.HalconWindow = this.hWindowControl1.HalconWindow;
            List<string> result = HDCodeHelper.DecodeCode(ho_image, count, cmbCodeType.Text);

            ShowCode(result);

            txtResult.Text += "\r\n扫码结束耗时:" + stopwatch.Elapsed.ToString();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            txtResult.Text = "";
        }

        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 pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void chbUseParam_CheckedChanged(object sender, EventArgs e)
        {
            HDCodeHelper.IsUseParam = chbUseParam.Checked;
        }
    }
}