1. 程式人生 > WINDOWS開發 >Dalsa 8K彩色相機Camera link C#採圖

Dalsa 8K彩色相機Camera link C#採圖

技術分享圖片

一個採圖工具,所以介面做的很簡單。

        private SapAcquisition m_Acquisition;
        private SapBuffer m_Buffers;
        private SapAcqToBuf m_Xfer;
        private SapView m_View;
        private SapColorConversion m_ColorConv;
        private SapProcessing m_Pro;
        private SapLocation m_ServerLocation;
        private String m_ConfigFileName;
        private String m_Executetime;
        private bool m_IsSignalDetected;
        private bool m_online;
public bool CreateNewObjects() { m_ServerLocation = new SapLocation("Xtium-CL_MX4_1",2);//第一位是顯示卡名稱,第二位是選擇模式的第幾項 m_ConfigFileName = "22N_No_Name_Default_Default.ccf";//配置檔案路徑 m_Acquisition = new SapAcquisition(m_ServerLocation,m_ConfigFileName); if (SapBuffer.IsBufferTypeSupported(m_ServerLocation,SapBuffer.MemoryType.ScatterGather)) m_Buffers = new SapBufferWithTrash(2,m_Acquisition,SapBuffer.MemoryType.ScatterGather); else m_Buffers = new SapBufferWithTrash(2,SapBuffer.MemoryType.ScatterGatherPhysical); m_Xfer = new SapAcqToBuf(m_Acquisition,m_Buffers); m_ColorConv = new SapColorConversion(m_Acquisition,m_Buffers); m_Xfer.Pairs[0].EventType = SapXferPair.XferEventType.EndOfFrame; m_Xfer.XferNotify += new SapXferNotifyHandler(xfer_XferNotify); m_Xfer.XferNotifyContext = this; m_Pro = new SapMyProcessing(m_Buffers,m_ColorConv,new SapProcessingDoneHandler(ProCallback),this); if (!CreateObjects()) { DestroyObjects(); DisposeObjects(); return false; } EnableSignalStatus(); return true; } //這項函式官方demo是用來顯示幀率的,但是我這裡不需要所以就沒寫功能,但是要加這個函式,否則連續採集的時候就只會拍兩張照片。 static void ProCallback(object sender,SapProcessingDoneEventArgs pInfo) { Form1 demo = pInfo.Context as Form1; } private bool CreateObjects() { // Create acquisition object if (m_Acquisition != null && !m_Acquisition.Initialized) { if (m_Acquisition.Create() == false) { DestroyObjects(); return false; } } // Create buffer object if (m_Buffers != null && !m_Buffers.Initialized) { if (m_Buffers.Create() == false) { DestroyObjects(); return false; } m_Buffers.Clear(); } // Create color conversion object if (m_ColorConv != null && !m_ColorConv.Initialized) { if (m_ColorConv.Create() == false) { DestroyObjects(); return false; } } // Create view object if (m_View != null && !m_View.Initialized) { if (m_ColorConv != null && m_ColorConv.Initialized) { // Set buffer to be viewed // When using hardware color decoder,view the acquired RGB buffer,// otherwise,for software color conversion,view the converted RGB buffer. SapBuffer convBuffer = m_ColorConv.OutputBuffer; if (convBuffer != null && convBuffer.Initialized) m_View.Buffer = convBuffer; else m_View.Buffer = m_Buffers; } if (m_View.Create() == false) { DestroyObjects(); return false; } } // Create Xfer object if (m_Xfer != null && !m_Xfer.Initialized) { if (m_Xfer.Create() == false) { DestroyObjects(); return false; } m_Xfer.AutoEmpty = false; } // Create processing object if (m_Pro != null && !m_Pro.Initialized) { if (!m_Pro.Create()) { DestroyObjects(); return false; } m_Pro.AutoEmpty = true; } return true; } private void DestroyObjects() { if (m_Xfer != null && m_Xfer.Initialized) m_Xfer.Destroy(); if (m_Pro != null && m_Pro.Initialized) m_Pro.Destroy(); if (m_View != null && m_View.Initialized) m_View.Destroy(); if (m_ColorConv != null && m_ColorConv.Initialized) m_ColorConv.Destroy(); if (m_Buffers != null && m_Buffers.Initialized) m_Buffers.Destroy(); if (m_Acquisition != null && m_Acquisition.Initialized) m_Acquisition.Destroy(); } private void DisposeObjects() { if (m_Xfer != null) { m_Xfer.Dispose(); m_Xfer = null; } if (m_Pro != null) { m_Pro.Dispose(); m_Pro = null; } if (m_View != null) { m_View.Dispose(); m_View = null; } if (m_ColorConv != null) { m_ColorConv.Dispose(); m_ColorConv = null; } if (m_Buffers != null) { m_Buffers.Dispose(); m_Buffers = null; } if (m_Acquisition != null) { m_Acquisition.Dispose(); m_Acquisition = null; } } private void EnableSignalStatus() { if (m_Acquisition != null) { m_IsSignalDetected = (m_Acquisition.SignalStatus != SapAcquisition.AcqSignalStatus.None); // if (m_IsSignalDetected == false) // StatusLabelInfo.Text = "Online... No camera signal detected"; // else // StatusLabelInfo.Text = "Online... Camera signal detected"; m_Acquisition.SignalNotifyEnable = true; } } static void xfer_XferNotify(object sender,SapXferNotifyEventArgs argsNotify) { Form1 demo = argsNotify.Context as Form1; // If grabbing in trash buffer,do not display the image,update the // appropriate number of frames on the status bar instead if (argsNotify.Trash) return; // Refresh view else { demo.TakeImage(); demo.m_Pro.ExecuteNext(); } }
        private void button1_Click(object sender,EventArgs e)
        {
            m_Xfer.Grab();
        }
        private void button2_Click(object sender,EventArgs e)
        {
            m_Xfer.Freeze();
        }
     private void TakeImage()
        {
            try
            {
                SapBuffer m_pBuffer;
                int m_StartFrame;
             //   SapBuffer.FileFormat[] m_StandardTypes;
                m_pBuffer = m_Buffers;
                m_StartFrame = m_pBuffer.Index; string m_Option = "-format jpeg -quality 30";
                m_Buffers.Save(textBox2.Text,m_Option,m_StartFrame,0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }


 

關於採圖,上面用的是官方的SDK方式,因為我採集的圖片畫素是8K*8K,原圖BMP格式有256MB,壓縮成30%質量的JPG也還有2.3MB左右。

還有一種方式是用Bitmap來獲取圖片地址,但是需要注意的一點。

    IntPtr addr;
    int Height = m_Buffers.Height;
    int Width = m_Buffers.Width;
    m_Buffers.GetAddress(out addr);       
    bitmap = new Bitmap(Width,Height,Width * 4,System.Drawing.Imaging.PixelFormat.Format32bppRgb,addr);
                        

  這裡的寬度是*4,原因是我的BMP原圖影象位深度是32位的。所以儲存用Bitmap的方式需要先確定原圖的位深度,否則儲存圖片會出現重影。