I've seen some Groovy code that lets you combing images and text, but not images and images ...
Essentially, I need to overlay symbols at certain coordinates on maps: I have some maps and symbols as .png files. I can do the coordinate calcs no problem, so the issue is more a case of, given two transparent pngs how do I combine them without losing transparency? (Both the map and the symbol may need to retain their transparency).
Ideally I need a function, something like
combinePngImage(background_src, foreground_src, foreground_x, foreground_y)
that would return a png combination of the two, given the top left, top ri开发者_JAVA百科ght coordinates of the foreground image.
[Background: I have some maps and symbols as .png files stored in container fields in FileMaker that I need to combine within a FileMaker solution. I've tried looking in to doing it using ImageMagick and the command line, but it suddenly struck me that this may be something that could be done using ScriptMaster, which uses Groovy to create external functions. ]
Any pointers gratefully received!
Okay, for what it's worth - here's some cobbled together cookbook snippets that seem to do the job - any comments on the (poor) coding style greatfully received.
Given a ScriptMaster call to
combineImage ( backgroundImg ; foregroundImg ; x ; y )
the required code is:
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
// get container data
InputStream bgImgContainer
try{
bgImgContainer = fmpro.getContainerStream(backgroundImg)
}catch(e){
throw new Exception('Could not get data from background image container (make sure container field name is passed as text)')
}
// test if container field is empty
if( bgImgContainer == null ) {
throw new Exception('Background image container field is empty')
}
bgImgName = fmpro.getContainerFileName(backgroundImg);
InputStream fgImgContainer
try{
fgImgContainer = fmpro.getContainerStream(foregroundImg)
}catch(e){
throw new Exception('Could not get data from foreground image container (make sure container field name is passed as text)')
}
// test if container field is empty
if( fgImgContainer == null ) {
throw new Exception('Foreground image container field is empty')
}
fgImgName = fmpro.getContainerFileName(foregroundImg);
int xCoord = Integer.parseInt(x);
int yCoord = Integer.parseInt(y);
// load image from container data
BufferedImage result = ImageIO.read(bgImgContainer);
BufferedImage overlay = ImageIO.read(fgImgContainer);
int fgWidth = overlay.getWidth(null);
int fgHeight = overlay.getHeight(null);
Graphics2D graphics = result.createGraphics();
// overlay the foreground at given coordinates and actual size
graphics.drawImage(overlay, xCoord, yCoord, fgWidth, fgHeight, null);
graphics.dispose();
File output = File.createTempFile(bgImgName, ".png");
ImageIO.write(result, "png", output);
return output;
Rather than using the ImageMagick command-line, you could try using Im4java, a pure Java ImageMagick API.
精彩评论