Exemplo n.º 1
0
        static void Main(string[] args)
        {
            try
            {
                // Initialize StApi before using.
                using (CStApiAutoInit api = new CStApiAutoInit())

                    // Create a system object for device scan and connection.
                    using (CStSystem system = new CStSystem())

                        // Create a camera device object and connect to first detected device.
                        using (CStDevice device = system.CreateFirstStDevice())

#if ENABLED_ST_GUI
                            // If using GUI for display, create a display window here.
                            using (CStImageDisplayWnd wnd = new CStImageDisplayWnd())
#endif
                            // Create a DataStream object for handling image stream data.
                            using (CStDataStream dataStream = device.CreateStDataStream(0))
                            {
                                // Displays the DisplayName of the device.
                                Console.WriteLine("Device=" + device.GetIStDeviceInfo().DisplayName);

                                // Register callback method. Note that by different usage, we pass different kinds/numbers of parameters in.
#if ENABLED_ST_GUI
                                object[] param = { wnd };
                                dataStream.RegisterCallbackMethod(OnCallback, param);
#else
                                dataStream.RegisterCallbackMethod(OnCallback);
#endif
                                // Start the image acquisition of the host (local machine) side.
                                dataStream.StartAcquisition();

                                // Start the image acquisition of the camera side.
                                device.AcquisitionStart();

                                // Keep getting image until Enter key pressed.
                                Console.WriteLine("\r\nPress Enter to exit.");
                                Console.ReadLine();

                                // Stop the image acquisition of the camera side.
                                device.AcquisitionStop();

                                // Stop the image acquisition of the host side.
                                dataStream.StopAcquisition();
                            }
            }
            catch (Exception e)
            {
                // If any exception occurred, display the description of the error here.
                Console.Error.WriteLine("An exception occurred. \r\n" + e.Message);
            }
        }
        /// <summary>
        /// 非同步取得影像的OnCallback函數無法順利執行(目前不使用)
        /// </summary>
        /// <returns></returns>
        public Bitmap CaptureToImageAsyn()
        {
            try
            {
                stImage = null;
                bmp     = null;
                CStPixelFormatConverter m_Converter = new CStPixelFormatConverter();

                dataStream.RegisterCallbackMethod(OnCallback);
                dataStream.StartAcquisition(1);
                device.AcquisitionStart();

                if (SpinWait.SpinUntil(() => stImage != null, 10000))
                {
                    device.AcquisitionStop();
                    dataStream.StopAcquisition();

                    bool isColor = CStApiDotNet.GetIStPixelFormatInfo(stImage.ImagePixelFormat).IsColor;
                    if (isColor)
                    {
                        // Convert the image data to BGR8 format.
                        m_Converter.DestinationPixelFormat = eStPixelFormatNamingConvention.BGR8;
                    }
                    else
                    {
                        // Convert the image data to Mono8 format.
                        m_Converter.DestinationPixelFormat = eStPixelFormatNamingConvention.Mono8;
                    }
                    m_Converter.Convert(stImage, imageBuffer);
                    byte[]       imageData = imageBuffer.GetIStImage().GetByteArray();
                    MemoryStream ms        = new MemoryStream(imageData);
                    bmp = (Bitmap)Bitmap.FromStream(ms);
                }
                else
                {
                    throw new Exception("Camera(" + device.GetIStDeviceInfo().DisplayName + ") can not capture image!!");
                }
            }
            catch (Exception ex)
            {
                device.AcquisitionStop();
                dataStream.StopAcquisition();

                throw ex;
            }
            return(bmp);
        }