# Using EGrabber Discovery

This code snippet shows how to go from an EGrabber buffer to an EGrabberImageBW8, compatible with eVision processing. It uses the eGrabber discovery feature to find the first available camera.

## C++

```cpp
#include <EGrabber.h>
#include <FormatConverter.h>
#include <eVision/EGrabberBridge.h>
```

```cpp
using namespace eVision;
using namespace Euresys;

// Construct the EGrabber objects.
// The FormatConverter is optional and will automatically convert the EGenTL buffer to
// the chosen eVision image type
EGenTL genTL;
FormatConverter converter(genTL);
EGrabberDiscovery discovery(genTL);

// Discover cameras and abort if no camera found
discovery.discover();

if (discovery.cameraCount() == 0)
  return;

// Instantiante grabber on first camera
EGrabber<CallbackOnDemand> grabber(discovery.cameras(0));

// Allocate one buffer
grabber.reallocBuffers(1);

// ...

// Start the grabber acquisition of one buffer
grabber.start(1);

// Get the acquired buffer
ScopedBuffer buffer(grabber);

// Convert the ScopedBuffer to an eVision data container
EGrabberBridge::EGrabberImageBW8 image(converter, buffer.getInfo());

// ...
```

## C#

```cs
using Euresys.EGrabber;
using eVision;
```

```cs
// Construct the EGrabber objects.
// WARNING: EGrabber and EGrabberDiscovery use an EGenTL instance.
// You must dispose them before disposing the EGenTL instance.
EGenTL genTL = new EGenTL();
EGrabberDiscovery discovery = new EGrabberDiscovery(genTL);

// Discover cameras and abort if no camera found
discovery.Discover();

if (discovery.CameraCount == 0)
  return;

// Instantiate grabber on first camera
EGrabber grabber = new EGrabber(discovery.Cameras[0]);

// Allocate one buffer
grabber.ReallocBuffers(1);

// ...

// Start the grabber acquisition of one buffer
grabber.Start(1);

// Get the acquired buffer
using (ScopedBuffer buffer = new ScopedBuffer(grabber))
{
  // Convert the ScopedBuffer to a ConvertedBuffer
  ConvertedBuffer converted = buffer.Convert("Mono8");

  EImageBW8 image = new EImageBW8();
  image.UseExternalBuffer(converted.Width, converted.Height, converted.Pixels, converted.LinePitch);

  // ...
}
```

## Python

```py
import ctypes
import eVision
import egrabber
import egentl
import discovery

# Construct the EGrabber objects.
# WARNING: EGrabber and EGrabberDiscovery use an EGenTL instance.
# You must dispose them before disposing the EGenTL instance.
genTL = egentl.EGenTL()
discovery =  discovery.EGrabberDiscovery(genTL)

# Discover cameras and abort if no camera found
discovery.discover()

if (len(discovery.cameras) == 0):
  return

# Instantiante grabber on first camera
grabber = egrabber.EGrabber(discovery.cameras[0])

# Allocate one buffer
grabber.realloc_buffers(1)

# ...

# Start the grabber acquisition of one buffer
grabber.start(1)

# Get the acquired buffer
with egrabber.Buffer(grabber) as buffer:
    converted = buffer.convert('Mono8')

    image = eVision.EImageBW8()
    data = ctypes.cast(converted.get_address(), ctypes.c_void_p)
    image.UseExternalBuffer(converted.width, converted.height, data, converted.line_pitch)

    # ...
```
