Halcom 发表于 2023-2-5 15:51:29

TDI线阵相机驱动Demo

using System;
using System.Drawing;
using System.Windows.Forms;

namespace live
{
    public partial class Form1 : Form
    {
      const int NB_BUFFERS = 50;

      Euresys.GenTL genTL = null;
      Euresys.FormatConverter.FormatConverter converter = null;
      Euresys.EGrabberCallbackOnDemand grabber = null;
      System.Drawing.Bitmap bitmap = null;

      UInt64 imgWidth;
      UInt64 imgHeight;
      String imgFormat;

      System.Threading.Thread thread;
      volatile bool refreshThreadRunning = false;

      Euresys.Buffer currentBuffer = null;

      public Form1()
      {
            InitializeComponent();
      }

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

      private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {
            timer1.Stop();
            refreshThreadRunning = false;
            if (grabber != null)
            {
                try
                {
                  grabber.stop();
                }
                catch (System.Exception exc)
                {
                  MessageBox.Show(exc.Message);
                }
                grabber.cancelPop();
                while (thread.IsAlive)
                {
                  System.Threading.Thread.Sleep(100);
                }
                grabber.Dispose();
                grabber = null;
            }
            if (converter != null)
            {
                converter.Dispose();
                converter = null;
            }
            if (genTL != null)
            {
                genTL.Dispose();
                genTL = null;
            }
      }

      private void Form1_Load(object sender, EventArgs e)
      {
            try
            {
                genTL = new Euresys.GenTL();
                converter = new Euresys.FormatConverter.FormatConverter(genTL);
                grabber = new Euresys.EGrabberCallbackOnDemand(genTL);
                grabber.runScript("./config.js");
                grabber.reallocBuffers(NB_BUFFERS);
                imgWidth = grabber.getWidth();
                imgHeight = grabber.getHeight();
                imgFormat = grabber.getPixelFormat();

                bitmap = new System.Drawing.Bitmap((int)imgWidth, (int)imgHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                pictureBox1.Image = bitmap;

                grabber.start();
                refreshThreadRunning = true;
                thread = new System.Threading.Thread(RefreshThread);
                thread.Start();

                timer1.Start();
            }
            catch (System.Exception exc)
            {
                MessageBox.Show(exc.Message);
                this.Close();
            }
      }

      delegate void InvalidateDelegate(bool invalidChildren);
      private void RefreshThread()
      {
            try
            {
                while (refreshThreadRunning && grabber != null)
                {
                  Euresys.Buffer buffer = new Euresys.Buffer(grabber.pop());
                  if (!refreshThreadRunning)
                  {
                        break;
                  }
                  if (currentBuffer == null)
                  {
                        currentBuffer = buffer;
                  }
                  else
                  {
                        if (grabber != null)
                        {
                            buffer.push(grabber);
                        }
                  }
                }
            }
            catch (Euresys.gentl_error exc)
            {
                if (exc.gc_err != Euresys.gc.GC_ERROR.GC_ERR_ABORT)
                  MessageBox.Show(exc.Message);
            }
            catch (System.Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
      }

      private void timer1_Tick(object sender, EventArgs e)
      {
            if (currentBuffer == null || grabber == null)
                return;

            if (currentBuffer == null || grabber == null)
                return;

            IntPtr imgPtr;
            currentBuffer.getInfo(grabber, Euresys.gc.BUFFER_INFO_CMD.BUFFER_INFO_BASE, out imgPtr);

            System.Drawing.Imaging.BitmapData bmpData = null;
            try
            {
                bmpData = bitmap.LockBits(new Rectangle(0, 0, (int)imgWidth, (int)imgHeight),
                  System.Drawing.Imaging.ImageLockMode.WriteOnly,
                  System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                converter.toBGR8(bmpData.Scan0, imgPtr, imgFormat, imgWidth, imgHeight);
            }
            finally
            {
                if (bmpData != null)
                {
                  bitmap.UnlockBits(bmpData);
                }
            }
            pictureBox1.Refresh();

            Euresys.Buffer bufferTmp = currentBuffer;
            currentBuffer = null;
            if (grabber != null) {
                bufferTmp.push(grabber);
            }
      }

    }
}

页: [1]
查看完整版本: TDI线阵相机驱动Demo