Axeda® Console

Return binary data from Custom Objects

A custom object can be configured to return binary or non-binary data. Custom objects can be defined to return data as strings, images, sounds, compressed files, and so forth. Refer to the documentation that accompanies the SDK if you need more assistance. The following source code shows an example object that will create and return the following JPEG image:

 

Source Code:

import java.awt.Color;

import java.awt.Font;

import java.awt.FontMetrics;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import javax.imageio.ImageIO;

int width = 300

int height = 100

def message = "ACME Company logo"

def bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

def graphics2d = bi.createGraphics();

def font = new Font("TimesRoman", Font.BOLD, 20);

graphics2d.setFont(font);

def fontMetrics = graphics2d.getFontMetrics();

int stringWidth = fontMetrics.stringWidth(message);

int stringHeight = fontMetrics.getAscent();

graphics2d.drawString(message, (int)((width - stringWidth) / 2), (int)(height / 2 + stringHeight / 4));

def baos = new ByteArrayOutputStream();

ImageIO.write( bi, "jpg", baos );

return ['Content-Type':'image/jpeg', 'Content': baos.toByteArray()]