-
Notifications
You must be signed in to change notification settings - Fork 1
Tutorial: Making a Module using RBG Imaging
Making a module that uses RGB imaging is a straightforward process. Follow these simple steps to add RGB image handling to your module.
If you haven't yet made your project, see [Creating Your First Module] (https://github.com/ColoradoSchoolOfMines/interface_sdk/wiki/Tutorial:-The-Undisputed-Step-by-Step-Guide-to-Developing-with-the-Interface-SDK#3-creating-your-first-module) to get started. Make sure that you've defined a proper package name and made a class that extends a module.
Your module should follow the format specified by the module type you're extending. Consult Extending Modules for more information on how to start your specific module type.
In order to use RGB imaging, you will need to add an input tag to your module manifest that looks like this:
<input input-type="rgbimage" />
This specifies that your module will need access to RGB image data, and this signals the [HardwareManager] (https://github.com/ColoradoSchoolOfMines/interface_sdk/wiki/API:-Hardware-Management) to give you permission to those devices.
You will also need to add a HardwareManager to your variables, and instantiate it like this:
private HardwareManager hardwareManager;
...
try {
hardwareManager = HardwareManager.getInstance();
} catch (HardwareManagerManifestException e) {
e.printStackTrace();
}
To get the driver itself, you will need to create a RGBImageInterface driver. The simple way to get the driver is to do this:
private RGBImageInterface rgbDriver;
...
try {
rgbDriver = (RGBImageInterface) hardwareManager.getInitialDriver("rgbimage");
} catch (BadFunctionalityRequestException e) {
e.printStackTrace();
} catch (InvalidConfigurationFileException e) {
e.printStackTrace();
} catch (UnknownDriverRequest e) {
e.printStackTrace();
}
getInitialDriver("rgbimage") will return the first driver that supports RGB imaging. If you would like to specify which driver you get data from, call getDevices("rgbimage") and store it as a list, then call inflateDriver(devicePath, "rgbimage"), where devicePath is one of the elements in the aforementioned list. Note that you must cast this driver as a RGBImageInterface as shown above.
To get an RGB image, use the following function:
ByteBuffer rgbBuffer = rgbDriver.getVisualData();
This will produce a ByteBuffer containing the RGB image data, which can be converted to a BufferedImage using the provided RGBImageUtilities:
BufferedImage img = RGBImageUtilities.byteBufferToImage(rgbBuffer,
rgbDriver.getRGBImageWidth(), rgbDriver.getRGBImageHeight());
Once you've retrieved the image, you can draw it as you would any BufferedImage. For Processing, use this function to convert a BufferedImage to a PImage:
public static PImage buffImagetoPImage(BufferedImage bimg) {
PImage img = new PImage(bimg.getWidth(), bimg.getHeight(), PConstants.ARGB);
bimg.getRGB(0, 0, img.width, img.height, img.pixels, 0, img.width);
return img;
}
From there, draw it as you would any other image.