最近,寫一個gige camera 的sample code

-------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TIS.Imaging;
using System.IO;
using TIS.Imaging.VCDHelpers;


namespace IC_CCD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private TIS.Imaging.VCDHelpers.VCDSimpleProperty VCDProp;

        private ICImagingControl icimagingcontrol = new ICImagingControl();//建立CCD
        private List<Device> devic = new List<Device>();//儲存所有CCD
        int pic_index = 0;
        bool live_record = false;
   
        TIS.Imaging.Device MyDevice = null;
 
        int timer_int = 500;


        private void Form1_Load(object sender, EventArgs e)
        {

            string Dir = @"d:\pic";
            timer_int = hScrollBar1.Value;

            if (Directory.Exists(Dir))
            {
                richTextBox1.AppendText("The directory " + Dir.ToString() + " already exists.\n");
            }
            else
            {
                Directory.CreateDirectory(Dir);
                richTextBox1.AppendText("The directory "+Dir.ToString()+" was created.\n");
            }

            foreach (Device my_device in icimagingcontrol.Devices)//搜尋所有可以連接上的CCD並儲存
            {
                devic.Add(my_device);
            }

            if (devic.Count > 0 && devic[0] != null)
            {
                
                icimagingcontrol.Device = devic[0];
                 
                MyDevice = icimagingcontrol.DeviceCurrent;

             
                if (MyDevice != null)
                {
                    setup();
                    SelectFormat();
                    //啟動 ImageAvailable,不可和MemorySnapImage 並用
                     icimagingcontrol.LiveCaptureContinuous = true;
                 
                    
                    //啟動取象後輸出至pictureBox
                    this.icimagingcontrol.ImageAvailable += new System.EventHandler<ICImagingControl.ImageAvailableEventArgs>(this.ImageAvailable);
                    icimagingcontrol.LiveStart();
                    richTextBox1.AppendText("Current Device is " + MyDevice.Name+"\n");                                    
                   richTextBox1.AppendText("Current frame rate is " + icimagingcontrol.DeviceFrameRate.ToString()+"\n");


                   
                   int  val = 1000 / timer_int;   
                   label1.Text = "sampe=" + val.ToString();
                   label2.Text = "Exposure=" + hScrollBar2.Value.ToString();
                   label3.Text = "FPS=" + icimagingcontrol.DeviceFrameRate.ToString();
                   label4.Text = "Gain=" + hScrollBar4.Value.ToString();
                   label5.Text = "WhiteBalanceB=" + hScrollBar5.Value.ToString();
                   label6.Text = "WhiteBalanceR=" + hScrollBar6.Value.ToString();
                }
                else
                    richTextBox1.AppendText("not device\n");
            }
            

            
        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            icimagingcontrol.LiveStop();
            icimagingcontrol.Display() ;
            timer1.Enabled = false;
        }
        private void Gige_OnImageReadyEventCallback()/* 圖像擷取後等待處理的事件 */
        {
            
            Bitmap Bmp = icimagingcontrol.ImageActiveBuffer.Bitmap as Bitmap;
            
            if (live_record == true)
            {
                string Path = "D:/Pic/CTN" + pic_index.ToString() + ".bmp";
                Bmp.Save(Path);
                pic_index++;
            }
         
               pictureBox1.Image = Bmp;
            
        }

        private void ImageAvailable(object sender, ICImagingControl.ImageAvailableEventArgs e)
        {
            
            try
            {
                Bitmap Bmp = icimagingcontrol.ImageActiveBuffer.Bitmap as Bitmap;

                // pictureBox1.Image = icimagingcontrol.ImageActiveBuffer.Bitmap;

                if (live_record == true)
                {
                    string Path = "D:/Pic/CTN" + pic_index.ToString() + ".bmp";
                    Bitmap Bmp02 = null;   
                    main(Bmp,ref Bmp02);  
                    SaveImage(Bmp02, Path);
                    pic_index++;
                }

                pictureBox1.Image = Bmp;

            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }
           

        }

        private void main(Bitmap Bmp,ref Bitmap Bmp02)
        {
            Imageprocess MyImage = new Imageprocess();
            int W = Bmp.Width;
            int H =Bmp.Height;
            byte[,] grayData = MyImage.ImatoGrayData(Bmp);
            byte[,] SobelData = MyImage.Sobel(grayData, 300);
            Bmp02 = MyImage.OutpuImage(grayData,Bmp);
            
        }
        private void timer1_Tick(object sender, EventArgs e)
        {

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Reset();
            sw.Start();   

            // Snap one image out of the video stream
            try
            {

                if (icimagingcontrol.LiveVideoRunning == false)
                {
                    icimagingcontrol.MemorySnapImage();//100 sec snap one image
                    Bitmap bmp02 = null;
                    string Pic_Path="D:/"+pic_index.ToString()+".bmp";
              
                    
                     Bitmap bmp = icimagingcontrol.ImageActiveBuffer.Bitmap;
                     main(bmp,ref bmp02);
 
                    string Path = "D:/Pic/snap_" + icimagingcontrol.ImageActiveBuffer.Bitmap.Width.ToString() + "X" + icimagingcontrol.ImageActiveBuffer.Bitmap.Height.ToString() + "_" + pic_index.ToString() + ".bmp";
                   
                    SaveImage(bmp02,Path);
                     

                    pic_index++;
                }
                

            }
            catch (ICException Ex)
            {
                MessageBox.Show(Ex.Message, "MemorySnapImage Error");
            }
            sw.Stop();
            string timestr = sw.Elapsed.Milliseconds.ToString();
            richTextBox1.AppendText(timestr + "msec\n");
            System.Threading.Thread.Sleep(timer_int);      
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (icimagingcontrol.DeviceValid==false)
            {
                setup();
            }

            if (icimagingcontrol.DeviceValid==true)
            {
                richTextBox1.Clear();

                
                if (icimagingcontrol.LiveVideoRunning==false)
                {
                  
                    icimagingcontrol.LiveCaptureContinuous = true;//啟動 ImageAvailable,不可和MemorySnapImage 並用
                    //啟動取象後輸出至pictureBox
                    this.icimagingcontrol.ImageAvailable += new System.EventHandler<ICImagingControl.ImageAvailableEventArgs>(this.ImageAvailable);                          
                    if (icimagingcontrol.LiveCaptureContinuous == false)
                        icimagingcontrol.LiveCaptureContinuous = true;
                    if (icimagingcontrol.LiveVideoRunning == false)
                        icimagingcontrol.LiveStart();
                  
                                   
                    richTextBox1.AppendText("live start\n");
 
                }
                else
                {
                    icimagingcontrol.LiveStop();
                    live_record = false;
                    richTextBox1.AppendText("live stop\n");
                }

            }
            else
            {
                MessageBox.Show("No Device Opened!");
                Close();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {


            if (icimagingcontrol.DeviceValid==false)
            {              
                setup();
            }
            if (icimagingcontrol.DeviceValid==true)
            {
                richTextBox1.Clear();
                icimagingcontrol.LiveCaptureContinuous = false;
                icimagingcontrol.LiveStop();
                live_record = false;

                timer1.Interval = timer_int;

                if (timer1.Enabled == false)
                {
                    timer1.Enabled =true;               
                    richTextBox1.AppendText("snap start\n");

                }
                else
                {
                    timer1.Enabled = false;
             
                    richTextBox1.AppendText("snap stop\n");
                }
            }

      
           

        }
 

        private void button3_Click(object sender, EventArgs e)
        {
            if (icimagingcontrol.DeviceValid && devic[0] != null)
            {
                if (icimagingcontrol.LiveCaptureContinuous == true)
                    icimagingcontrol.LiveCaptureContinuous = false;
                if (icimagingcontrol.LiveVideoRunning == true)
                    icimagingcontrol.LiveStop();
                if (timer1.Enabled == true)
                    timer1.Enabled = false;
            }
            setup();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            if (live_record == false)
            {
                live_record = true;
                richTextBox1.AppendText("live_record start\n");
            }
            else
            {
                live_record = false;
                richTextBox1.AppendText("live_record stop\n");
            }
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
           
            if (live_record == true)
                live_record = false;
            if (timer1.Enabled == true)
                timer1.Enabled = false;
            timer_int = hScrollBar1.Value;
            timer1.Interval = timer_int;
            int val;                                                                   
            val = 1000 / timer_int;
            label1.Text ="Sample="+ val.ToString();          
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (live_record == true)
                live_record = false;
            if (timer1.Enabled == true)
                timer1.Enabled = false;
            icimagingcontrol.LiveStop();
            icimagingcontrol.Display();
            icimagingcontrol.EndInit();
            this.Close();
        }
        private void button6_Click(object sender, EventArgs e)
        {
            string Dir=@"d:\pic";
            if (Directory.Exists(Dir))
            {
                richTextBox1.Clear();
                richTextBox1.AppendText("Stat to delete all files\n");
                string[] files = Directory.GetFiles(Dir);
                foreach (string file in files)
                {
                    File.SetAttributes(file,FileAttributes.Normal);
                    File.Delete(file);
                }
            }

        }

        private static void DeleteFile(string FileName)
        {
            if (File.Exists(FileName))
            {
                File.Delete(FileName);
            }
        }

        private void setup()
        {
            if (icimagingcontrol.DeviceValid==false)
                return;
            VCDProp = VCDSimpleModule.GetSimplePropertyContainer(icimagingcontrol.VCDPropertyItems);

            int max, min;
            if (VCDProp.AutoAvailable(VCDIDs.VCDID_Exposure))
            {
                VCDProp.Automation[VCDIDs.VCDID_Exposure]=false;
                max = VCDProp.RangeMax(VCDIDs.VCDID_Exposure);
                min = VCDProp.RangeMin(VCDIDs.VCDID_Exposure);
                
                richTextBox1.AppendText("Exposure=" + VCDProp.RangeMin(VCDIDs.VCDID_Exposure).ToString() + "\n");
                richTextBox1.AppendText("Exposure=" + VCDProp.RangeMax(VCDIDs.VCDID_Exposure).ToString() + "\n");
                hScrollBar2.Maximum = max;
                if (max > 10000)
                    max = 10000;
                hScrollBar2.Minimum=min;
                hScrollBar2.Value =(max+min)/2;
                VCDProp.RangeValue[VCDIDs.VCDID_Exposure] = hScrollBar2.Value;



            }
            if (VCDProp.AutoAvailable(VCDIDs.VCDID_Gain))
            {
                VCDProp.Automation[VCDIDs.VCDID_Gain] = false;
                max = VCDProp.RangeMax(VCDIDs.VCDID_Gain);
                min = VCDProp.RangeMin(VCDIDs.VCDID_Gain);
                richTextBox1.AppendText("Exposure=" + VCDProp.RangeMin(VCDIDs.VCDID_Gain).ToString() + "\n");
                richTextBox1.AppendText("Exposure=" + VCDProp.RangeMax(VCDIDs.VCDID_Gain).ToString() + "\n");
                hScrollBar4.Maximum = max;

                hScrollBar4.Minimum = min;
                hScrollBar4.Value = (max+min) / 2;
                VCDProp.RangeValue[VCDIDs.VCDID_Gain] = hScrollBar4.Value;
            }
            

        
            if (icimagingcontrol.DeviceValid && devic[0] != null)
            {
                int W, H;

               
                W = icimagingcontrol.ImageWidth;
                H = icimagingcontrol.ImageHeight;
                pictureBox1.Height = H;
                pictureBox1.Width = W;                
              
                richTextBox1.AppendText(W.ToString() + "x" + H.ToString() + "\n");
                 
            }
      
            

              
    
        }

        private void hScrollBar2_Scroll(object sender, ScrollEventArgs e)
        {
            icimagingcontrol.LiveStop();

            VCDProp = VCDSimpleModule.GetSimplePropertyContainer(icimagingcontrol.VCDPropertyItems);
            if (VCDProp.AutoAvailable(VCDIDs.VCDID_Exposure))
            {
                VCDProp.Automation[VCDIDs.VCDID_Exposure] = false;
                VCDProp.RangeValue[VCDIDs.VCDID_Exposure] = hScrollBar2.Value;
                label2.Text="Exposure="+VCDProp.RangeValue[VCDIDs.VCDID_Exposure].ToString();
            }
            icimagingcontrol.LiveStart();

        }

        private void hScrollBar3_Scroll(object sender, ScrollEventArgs e)
        {
            icimagingcontrol.LiveStop();
            if (live_record == true)
                live_record = false;
            if (timer1.Enabled == true)
                timer1.Enabled = false;
            icimagingcontrol.DeviceFrameRate = hScrollBar3.Value;
            label3.Text = "FPS=" + icimagingcontrol.DeviceFrameRate.ToString();
            richTextBox1.Clear();
            richTextBox1.AppendText("Current frame rate is " + icimagingcontrol.DeviceFrameRate.ToString() + "\n");
            icimagingcontrol.LiveStart();
        }

        private void hScrollBar4_Scroll(object sender, ScrollEventArgs e)
        {

            icimagingcontrol.LiveStop();

            VCDProp = VCDSimpleModule.GetSimplePropertyContainer(icimagingcontrol.VCDPropertyItems);
            if (VCDProp.AutoAvailable(VCDIDs.VCDID_Gain))
            {
                VCDProp.Automation[VCDIDs.VCDID_Gain] = false;
                VCDProp.RangeValue[VCDIDs.VCDID_Gain] = hScrollBar4.Value;
                label4.Text = "Gain=" + VCDProp.RangeValue[VCDIDs.VCDID_Gain].ToString();

            }
            icimagingcontrol.LiveStart();
        }

        private void hScrollBar5_Scroll(object sender, ScrollEventArgs e)
        {

            icimagingcontrol.LiveStop();
            VCDProp = VCDSimpleModule.GetSimplePropertyContainer(icimagingcontrol.VCDPropertyItems);
            if (VCDProp.AutoAvailable(VCDIDs.VCDElement_WhiteBalanceBlue))
            {
                VCDProp.Automation[(VCDIDs.VCDElement_WhiteBalanceBlue)] = false;
                VCDProp.RangeValue[(VCDIDs.VCDElement_WhiteBalanceBlue)] = hScrollBar5.Value;
                label5.Text = "WhiteBalanceB=" + VCDProp.RangeValue[(VCDIDs.VCDElement_WhiteBalanceBlue)].ToString();

            }
            icimagingcontrol.LiveStart();
        }


        private void hScrollBar6_Scroll(object sender, ScrollEventArgs e)
        {
            icimagingcontrol.LiveStop();
            VCDProp = VCDSimpleModule.GetSimplePropertyContainer(icimagingcontrol.VCDPropertyItems);
            if (VCDProp.AutoAvailable(VCDIDs.VCDElement_WhiteBalanceRed))
            {
                VCDProp.Automation[(VCDIDs.VCDElement_WhiteBalanceRed)] = false;
                VCDProp.RangeValue[(VCDIDs.VCDElement_WhiteBalanceRed)] = hScrollBar6.Value;
                label6.Text = "WhiteBalanceR=" + VCDProp.RangeValue[(VCDIDs.VCDElement_WhiteBalanceRed)].ToString();
            }
            icimagingcontrol.LiveStart();
        }
        private void SelectFormat()
        {
            if (MyDevice.Name == "GM750A-MI")
            {

                icimagingcontrol.VideoFormat = "Y800 (640x480)";
            }
            else
            {
                icimagingcontrol.VideoFormat = "Y800 (1280x960)";//GC1300S-MI  

            }
            icimagingcontrol.DeviceFrameRate = hScrollBar3.Value;
      }
        private void SaveImage(Bitmap bmp,String Path)
        {
            try
            {
                bmp.Save(Path, System.Drawing.Imaging.ImageFormat.Gif);
            }
            catch (ICException Ex)
            {
                MessageBox.Show(Ex.Message, "MemorySnapImage Error");
            }

             System.Threading.Thread.Sleep(20);
        }
        private void ImageProp()
        {
            if (VCDProp.AutoAvailable(VCDIDs.VCDID_Denoise))
            {
                richTextBox1.AppendText("Denoise_max=" + VCDProp.RangeMax(VCDIDs.VCDID_Denoise).ToString() + "\n");
                richTextBox1.AppendText("Denoise_min=" + VCDProp.RangeMin(VCDIDs.VCDID_Denoise).ToString() + "\n");
            }
            if (VCDProp.AutoAvailable(VCDIDs.VCDID_Brightness))
            {
                richTextBox1.AppendText("Brightness_max=" + VCDProp.RangeMax(VCDIDs.VCDID_Brightness).ToString() + "\n");
                richTextBox1.AppendText("Brightness_min=" + VCDProp.RangeMin(VCDIDs.VCDID_Brightness).ToString() + "\n");
            }
            if (VCDProp.AutoAvailable(VCDIDs.VCDID_Contrast))
            {
                richTextBox1.AppendText("Contrast_max=" + VCDProp.RangeMax(VCDIDs.VCDID_Contrast).ToString() + "\n");
                richTextBox1.AppendText("Contrast_min=" + VCDProp.RangeMin(VCDIDs.VCDID_Contrast).ToString() + "\n");
            }


            if (VCDProp.AutoAvailable(VCDIDs.VCDElement_WhiteBalanceCY))
            {
                richTextBox1.AppendText("WhitebalanceCY_max=" + VCDProp.RangeMax(VCDIDs.VCDElement_WhiteBalanceCY).ToString() + "\n");
                richTextBox1.AppendText("WhitebalanceCY_min=" + VCDProp.RangeMin(VCDIDs.VCDElement_WhiteBalanceCY).ToString() + "\n");
            }

            if (VCDProp.AutoAvailable(VCDIDs.VCDID_Focus))
            {
                richTextBox1.AppendText("Focus_max=" + VCDProp.RangeMax(VCDIDs.VCDID_Focus).ToString() + "\n");
                richTextBox1.AppendText("Focus_min=" + VCDProp.RangeMin(VCDIDs.VCDID_Focus).ToString() + "\n");
            }

            if (VCDProp.AutoAvailable(VCDIDs.VCDID_Saturation))
            {
                richTextBox1.AppendText("Saturation_max=" + VCDProp.RangeMax(VCDIDs.VCDID_Saturation).ToString() + "\n");
                richTextBox1.AppendText("Saturation_min=" + VCDProp.RangeMin(VCDIDs.VCDID_Saturation).ToString() + "\n");
            }

            if (VCDProp.AutoAvailable(VCDIDs.VCDID_Sharpness))
            {
                richTextBox1.AppendText("Sharpness_max=" + VCDProp.RangeMax(VCDIDs.VCDID_Sharpness).ToString() + "\n");
                richTextBox1.AppendText("Sharpness_min=" + VCDProp.RangeMin(VCDIDs.VCDID_Sharpness).ToString() + "\n");
            }
            if (VCDProp.AutoAvailable(VCDIDs.VCDID_ColorEnhancement))
            {
                richTextBox1.AppendText("ColorEnhancdement_max=" + VCDProp.RangeMax(VCDIDs.VCDID_ColorEnhancement).ToString() + "\n");
                richTextBox1.AppendText("ColorEnhancdement_min=" + VCDProp.RangeMin(VCDIDs.VCDID_ColorEnhancement).ToString() + "\n");
            }
            if (VCDProp.AutoAvailable(VCDIDs.VCDElement_WhiteBalanceRed))
            {
                richTextBox1.AppendText("WhiteBalanceR_max=" + VCDProp.RangeMax(VCDIDs.VCDElement_WhiteBalanceRed).ToString() + "\n");
                richTextBox1.AppendText("WhiteBalanceR_min=" + VCDProp.RangeMin(VCDIDs.VCDElement_WhiteBalanceRed).ToString() + "\n");
            }
            if (VCDProp.AutoAvailable(VCDIDs.VCDElement_WhiteBalanceGreen))
            {
                richTextBox1.AppendText("WhiteBalanceg_max=" + VCDProp.RangeMax(VCDIDs.VCDElement_WhiteBalanceGreen).ToString() + "\n");
                richTextBox1.AppendText("WhiteBalanceg_min=" + VCDProp.RangeMin(VCDIDs.VCDElement_WhiteBalanceGreen).ToString() + "\n");
            }
            if (VCDProp.AutoAvailable(VCDIDs.VCDElement_WhiteBalanceBlue))
            {
                richTextBox1.AppendText("WhiteBalanceB_max=" + VCDProp.RangeMax(VCDIDs.VCDElement_WhiteBalanceBlue).ToString() + "\n");
                richTextBox1.AppendText("WhiteBalanceB_min=" + VCDProp.RangeMin(VCDIDs.VCDElement_WhiteBalanceBlue).ToString() + "\n");
            }
            if (VCDProp.AutoAvailable(VCDIDs.VCDElement_AutoReference))
            {
                richTextBox1.AppendText("AutoReference_max=" + VCDProp.RangeMax(VCDIDs.VCDElement_AutoReference).ToString() + "\n");
                richTextBox1.AppendText("AutoReference_min=" + VCDProp.RangeMin(VCDIDs.VCDElement_AutoReference).ToString() + "\n");
            }
            if (VCDProp.AutoAvailable(VCDIDs.VCDID_BacklightCompensation))
            {
                richTextBox1.AppendText("BacklightCompensation_max=" + VCDProp.RangeMax(VCDIDs.VCDID_BacklightCompensation).ToString() + "\n");
                richTextBox1.AppendText("BacklightCompensation_min=" + VCDProp.RangeMin(VCDIDs.VCDID_BacklightCompensation).ToString() + "\n");
            }
            if (VCDProp.AutoAvailable(VCDIDs.VCDID_Hue))
            {
                richTextBox1.AppendText("Hue_max=" + VCDProp.RangeMax(VCDIDs.VCDID_Hue).ToString() + "\n");
                richTextBox1.AppendText("Hue_min=" + VCDProp.RangeMin(VCDIDs.VCDID_Hue).ToString() + "\n");
            }
        }
        private Bitmap ImageResize(Bitmap ORIBmp, int x, int y)
        {
            MemoryStream ms=new MemoryStream();
            ORIBmp.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
            Image myImage=Image.FromStream(ms);
            Bitmap bmp=new Bitmap(myImage,x,y);
            ms.Close();
            return bmp;
        }
    }
}

文章標籤
創作者介紹

colin異世界

colin 發表在 痞客邦 PIXNET 留言(0) 人氣()