Grab#

using Itala;
using Itala.GenApi;

/// <summary>
/// "Grab" example shows how to acquire images in a basic fashion after
///  a simple camera configuration is performed (exposure time, pixel format and 
///  image size). Images are acquired in loops where a wait for a new available
///  image is performed and some data is printed if the image is successfully grabbed.
///  When a single loop is over the current image is disposed, i.e. returned to
///  Itala so that its memory can be released and made available for newer
///  acquisitions. The grabbed image can be displayed by third party libraries
///  by accessing its width, height, format and data informations.
/// </summary>
internal class Program
{
  const int ACQUIRE_COUNT = 50;

  private static void Grab_Sample()
  {
    Console.WriteLine("######## Grab ########");
    ISystem system = SystemFactory.Create();
    List<DeviceInfo> deviceInfos = system.EnumerateDevices();

    if (deviceInfos.Count == 0)
      throw new ItalaRuntimeException("No devices found. Example canceled.");
    if (deviceInfos[0].AccessStatus != DeviceAccessStatus.AvailableReadWrite)
      throw new ItalaRuntimeException("Target device is unaccessible in RW mode. Example canceled.");
    IDevice device = system.CreateDevice(deviceInfos[0]);
    Console.WriteLine("First device initialized.");

    INodeMap deviceNodeaMap = device.GetNodeMap();

    // Start a default continuous acquisition. Internally, the runtime allocates a
    // queue of 16 image buffers thus a maximum of 16 images can be grabbed simultaneously
    // without being released. An additional overload function allows to set the
    // buffer and frame count.
    device.StartAcquisition();
    Console.WriteLine("Acquisition started.");

    IImage image;

    // Perform a continuous acquisition by looping ACQUIRE_COUNT times and
    // and grab a new image at every loop.
    for(int i = 0; i < ACQUIRE_COUNT; i++)
    {
      // Grab the first available image. If the specified
      // 1000ms timeout expires, a TimeoutException occurs. Don't forget to
      // dispose the image when it's no longer needed.
      image = device.GetNextImage(1000);

      // Check if the image data is incomplete. This may happen due to packets
      // lost during transmission. Packets could get lost for many of reasons
      // resulting from an incorrect network setup, bad camera configuration,
      // poor hardware quality or inadequate conditions of the environment.
      if(image.IsIncomplete)
      {
        // Report some info if the image is partially filled.
        Console.WriteLine("Icomplete image.");
        Console.WriteLine("\tBytes filled: "+ image.BytesFilled+"/"+image.PayloadSize);
      }
      else
      {
        Console.WriteLine("Image grabbed.");
      }

      Console.WriteLine("\tFrameID: "+image.FrameID);
      Console.WriteLine("\tTimestamp: "+image.Timestamp);

      // DO SOMETHING WITH THE IMAGE..

      // When the image is no longer required (e.g. when the current loop is over) 
      // it MUST be disposed by calling the dedicated function. When an image is
      // disposed, its memory is released and it can be reused for a new grab.
      image.Dispose();
    }

    device.StopAcquisition();
    Console.WriteLine("Acquisition stopped.");

    device.Dispose();
    Console.WriteLine("Device instance disposed.");
    system.Dispose();
    Console.WriteLine("System instance disposed.");
  }

  private static void Main(string[] args)
  {
    try
    {
      Grab_Sample();
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.ToString());
    }
  }
}