Hello Mat

 找回密码
 立即注册
查看: 727|回复: 0

TDI线阵相机驱动Demo

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2023-2-5 15:51:29 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;

  4. namespace live
  5. {
  6.     public partial class Form1 : Form
  7.     {
  8.         const int NB_BUFFERS = 50;

  9.         Euresys.GenTL genTL = null;
  10.         Euresys.FormatConverter.FormatConverter converter = null;
  11.         Euresys.EGrabberCallbackOnDemand grabber = null;
  12.         System.Drawing.Bitmap bitmap = null;

  13.         UInt64 imgWidth;
  14.         UInt64 imgHeight;
  15.         String imgFormat;

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

  18.         Euresys.Buffer currentBuffer = null;

  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }

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

  27.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  28.         {
  29.             timer1.Stop();
  30.             refreshThreadRunning = false;
  31.             if (grabber != null)
  32.             {
  33.                 try
  34.                 {
  35.                     grabber.stop();
  36.                 }
  37.                 catch (System.Exception exc)
  38.                 {
  39.                     MessageBox.Show(exc.Message);
  40.                 }
  41.                 grabber.cancelPop();
  42.                 while (thread.IsAlive)
  43.                 {
  44.                     System.Threading.Thread.Sleep(100);
  45.                 }
  46.                 grabber.Dispose();
  47.                 grabber = null;
  48.             }
  49.             if (converter != null)
  50.             {
  51.                 converter.Dispose();
  52.                 converter = null;
  53.             }
  54.             if (genTL != null)
  55.             {
  56.                 genTL.Dispose();
  57.                 genTL = null;
  58.             }
  59.         }

  60.         private void Form1_Load(object sender, EventArgs e)
  61.         {
  62.             try
  63.             {
  64.                 genTL = new Euresys.GenTL();
  65.                 converter = new Euresys.FormatConverter.FormatConverter(genTL);
  66.                 grabber = new Euresys.EGrabberCallbackOnDemand(genTL);
  67.                 grabber.runScript("./config.js");
  68.                 grabber.reallocBuffers(NB_BUFFERS);
  69.                 imgWidth = grabber.getWidth();
  70.                 imgHeight = grabber.getHeight();
  71.                 imgFormat = grabber.getPixelFormat();

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

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

  78.                 timer1.Start();
  79.             }
  80.             catch (System.Exception exc)
  81.             {
  82.                 MessageBox.Show(exc.Message);
  83.                 this.Close();
  84.             }
  85.         }

  86.         delegate void InvalidateDelegate(bool invalidChildren);
  87.         private void RefreshThread()
  88.         {
  89.             try
  90.             {
  91.                 while (refreshThreadRunning && grabber != null)
  92.                 {
  93.                     Euresys.Buffer buffer = new Euresys.Buffer(grabber.pop());
  94.                     if (!refreshThreadRunning)
  95.                     {
  96.                         break;
  97.                     }
  98.                     if (currentBuffer == null)
  99.                     {
  100.                         currentBuffer = buffer;
  101.                     }
  102.                     else
  103.                     {
  104.                         if (grabber != null)
  105.                         {
  106.                             buffer.push(grabber);
  107.                         }
  108.                     }
  109.                 }
  110.             }
  111.             catch (Euresys.gentl_error exc)
  112.             {
  113.                 if (exc.gc_err != Euresys.gc.GC_ERROR.GC_ERR_ABORT)
  114.                     MessageBox.Show(exc.Message);
  115.             }
  116.             catch (System.Exception exc)
  117.             {
  118.                 MessageBox.Show(exc.Message);
  119.             }
  120.         }

  121.         private void timer1_Tick(object sender, EventArgs e)
  122.         {
  123.             if (currentBuffer == null || grabber == null)
  124.                 return;

  125.             if (currentBuffer == null || grabber == null)
  126.                 return;

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

  129.             System.Drawing.Imaging.BitmapData bmpData = null;
  130.             try
  131.             {
  132.                 bmpData = bitmap.LockBits(new Rectangle(0, 0, (int)imgWidth, (int)imgHeight),
  133.                     System.Drawing.Imaging.ImageLockMode.WriteOnly,
  134.                     System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  135.                 converter.toBGR8(bmpData.Scan0, imgPtr, imgFormat, imgWidth, imgHeight);
  136.             }
  137.             finally
  138.             {
  139.                 if (bmpData != null)
  140.                 {
  141.                     bitmap.UnlockBits(bmpData);
  142.                 }
  143.             }
  144.             pictureBox1.Refresh();

  145.             Euresys.Buffer bufferTmp = currentBuffer;
  146.             currentBuffer = null;
  147.             if (grabber != null) {
  148.                 bufferTmp.push(grabber);
  149.             }
  150.         }

  151.     }
  152. }
复制代码


算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-29 00:57 , Processed in 0.215633 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表