C# Haclon 相机识别拍照回显图片控件

C# Haclon 相机识别拍照回显图片控件

using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace DHuaDemo
{
    public partial class test2 : Form
    {
        public test2()
        {
            InitializeComponent();
            // 取消线程校验 不加会导致在线程中访问主线程的控件异常
            CheckForIllegalCrossThreadCalls = false;
        }

        //一个变量
        HObject ho_Image = null;
        //图像获取设备的句柄
        HTuple hv_AcqHandle = null;
        //定义抓拍控制变量
        int snapcontorl = 0;
        //线程标志
        Thread thread1 = null;
        //用于获取图片的大小
        HTuple hv_Width = new HTuple();
        HTuple hv_Height = new HTuple();
        HTuple hv_WindowHandle = new HTuple();

        //按钮一:打开相机
        private void button1_Click(object sender, EventArgs e)
        {
            // 调用第一个相机拍照事件
            CameraRecognition1();

        }

        //用于控制相机
        private void cameracontrol1()
        {
            while (true)
            {
                // HalconDotNet.HOperatorException:“HALCON error #0: No error message available for this error code (0) in operator disp_obj”
                1
                ho_Image.Dispose();
                HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
                    if (snapcontorl == 1)
                    {
                        //保存图片
                        //将图片ho_Image,以jpg格式,值默认0,存放在D盘
                        HOperatorSet.WriteImage(ho_Image, "jpg", 0, @"D:\");
                        //textBox1.Text =  "照片已保存";
                        snapcontorl = 0;
                        Bitmap m;
                        pictureBox1.Image = HObject2Bitmap8(ho_Image, out m);
                    }
                }
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetDraw(hWindowControl1.HalconWindow, "margin");
                    HOperatorSet.SetLineWidth(hWindowControl1.HalconWindow, 3);
                    // 这个不可用,会导致画面展示时模糊
                    //HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, hv_Width, hv_Height);
                    HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height, hv_Width);
                }
            }
        }
        //按钮二:抓拍图像
        private void button2_Click(object sender, EventArgs e)
        {
            snapcontorl = 1;
        }

        private bool result = true;
        //按钮三:关闭相机
        private void button3_Click(object sender, EventArgs e)
        {
            // 调用关闭相机方法
            FormClose();
            result = false;
        }

        #region //  Halcon识别灰度图转换为图片
        public Bitmap HObject2Bitmap8(HObject image, out Bitmap res)
        {
            HTuple hpoint, type, width, height;
            const int Alpha = 255;
            HOperatorSet.GetImagePointer1(image, out hpoint, out type, out width, out height);
            res = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
            ColorPalette pal = res.Palette;
            for (int i = 0; i <= 255; i++)
            { pal.Entries[i] = Color.FromArgb(Alpha, i, i, i); }

            res.Palette = pal; Rectangle rect = new Rectangle(0, 0, width, height);
            BitmapData bitmapData = res.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
            int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
            IntPtr ptr1 = bitmapData.Scan0;
            IntPtr ptr2 = hpoint; int bytes = width * height;
            byte[] rgbvalues = new byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(ptr2, rgbvalues, 0, bytes);
            System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr1, bytes);
            res.UnlockBits(bitmapData);
            return res;
        }
        #endregion

        #region  // 用于三项图识别转换图片
        public static Bitmap Honject2Bitmap24(HObject hObject)
        {
            // HalconDotNet.HOperatorException:“HALCON error #9009: Image is no color image with three channels in operator interleave_channels”

            //获取图像尺寸
            HTuple width0 = new HTuple();
            HTuple height0 = new HTuple();
            HTuple Pointer = new HTuple();
            HTuple type = new HTuple();
            HTuple width = new HTuple();
            HTuple height = new HTuple();
            HObject InterImage = new HObject();
            //HOperatorSet.GetImageSize(hObject, out width0, out height0);
            HOperatorSet.GetImageSize(hObject, out width0, out height0);
            //创建交错格式图像
            HOperatorSet.InterleaveChannels(hObject, out InterImage, "rgb", 4 * width0, 0);
            //获取交错格式图像指针
            HOperatorSet.GetImagePointer1(InterImage, out Pointer, out type, out width, out height);
            IntPtr ptr = Pointer;
            //构建新Bitmap图像
            Bitmap bitmap = new Bitmap(width / 4, height, width, PixelFormat.Format24bppRgb, ptr);
            return bitmap;
        }
        #endregion



        private void test2_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (result)
            {
                // 调用关闭相机方法
                FormClose();
            }
        }

        public void FormClose()
        {
            if (thread1 != null)
            {
                //关闭线程
                thread1.Abort();
            }

            if (hv_AcqHandle != null)
            {
                //关闭图像采集设备的句柄。
                HOperatorSet.CloseFramegrabber(hv_AcqHandle);
            }
        }


        #region // 启动相机拍照事件
        public void CameraRecognition1()
        {
            if (string.IsNullOrEmpty(Properties.Settings.Default.videoName1))
            {
                MessageBox.Show("请设置摄像机的型号参数!");
                return;
            }

            HOperatorSet.GenEmptyObj(out ho_Image);
            //HOperatorSet.GenEmptyObj(out ho_Image2);
            // Image Acquisition 01: Code generated by Image Acquisition 01
            HOperatorSet.OpenFramegrabber("GigEVision2", 0, 0, 0, 0, 0, 0, "progressive", -1, "default", -1, "false", "default", "38af29c7c8b7_DahuaTechnologyn_A3A20MG8pHi", 0, -1, out hv_AcqHandle);
            HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.CloseWindow(HDevWindowStack.Pop());
            }
            HOperatorSet.SetWindowAttr("background_color", "black");
            HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureMode", "Timed");
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTime", 4500.0);
            MessageBox.Show("相机已打开!");

            thread1 = new Thread(cameracontrol1);
            thread1.IsBackground = true;
            thread1.Start();
        }
        #endregion
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值