I don’t work with MySQL, but here is the code I use for getting images out of PostgreSQL (stored as bytea, which is pretty much the same as BLOB in MySQL).
You should be able to use this without any modifications.
For the generated cell below, I display either the entity’s name, or a picture of it is available.
In my case every Entity object is wrapped into an EntityWrapper in order to display data properly and enable proper sorting. This has no impact on the image generation code, of course.
If anyone has comments on how to make the code better, this will be much appreciated. But it works perfectly as-is.
addGeneratedColumn(“entityName”, new ColumnGenerator() { @Override
public Component generateCell(Table table, Object itemId, Object columnId) {
EntityWrapper item = (EntityWrapper) itemId;
Component component;
Entity entity = item.getEntityData();
final byte img = entity.getImg();
String name = entity.getEntityName();
if (img == null) {
component = new Label(name);
} else {
StreamResource.StreamSource imageSource = new StreamResource.StreamSource() { @Override
public InputStream getStream() {
return new ByteArrayInputStream(img);
}
};
StreamResource imageResource = new StreamResource(imageSource, name + “.png”, getApplication());
imageResource.setCacheTime(0);
Embedded image = new Embedded(name, imageResource);
image.requestRepaint();
SimpleDateFormat df = new SimpleDateFormat(“yyyyMMddHHmmssSSS”);
String filename = name + df.format(new Date()) + “.png”;
imageResource.setFilename(filename);
component = image;
}
return component;
}
});