What's the best way to fill or paint around an image in Java?
I have a set of images that I'm combining into a single image mosaic using JAI's MosaicDescriptor.
Most of the images are the same size, but some are smaller. I'd like to fill in the missing space with white - by default, the MosaicDescriptor is using black. I tried setting the the double[] background parameter to { 255 }, and that fills in the missing space with white, but it also introduces some discoloration in some of the other full-sized images.
I'm open to any method - there are probably many ways to do this, but the documentation is difficult to navigate. I am considering converting any smaller images to a BufferedImage and calling setRGB() on the empty areas (though I am unsure what to use for the scansize on the batch setRGB() method).
My question is essentially:
- What is the best way to take an image (in JAI, or BufferedImage) and fill / add padding to a certain size?
- Is there a way to accomplish this in the MosaicDescriptor call without side-effects?
For reference, here is the code that creates the mosaic:
for (int i = 0; i < images.length; i++) {
images[i] = JPEGDescriptor.create(new ByteArraySeekableStream(images[i]), null);
if (i 开发者_StackOverflow社区!= 0) {
images[i] = TranslateDescriptor.create(image, (float) (width * i), null, null, null);
}
}
RenderedOp finalImage = MosaicDescriptor.create(ops, MosaicDescriptor.MOSAIC_TYPE_OVERLAY, null, null, null, null, null);
To answer part of my question, this can be accomplished with Graphics2D and BufferedImages without using JAI at all:
final BufferedImage montageImage = new BufferedImage(montageSize, montageSize, BufferedImage.TYPE_INT_RGB);
final Graphics2D g2 = montageImage.createGraphics();
g2.setPaint(Color.WHITE);
g2.fillRect(0, 0, montageSize, montageSize);
for (int i = 0; i < imageData.length; i++) {
final BufferedImage inputImage = ImageIO.read(new ByteArrayInputStream(imageData[i]));
g2.drawImage(inputImage, i * size, 0, null);
}
g2.dispose();
Essentially, an image is created of the desired full size, and filled with white. Then the smaller image(s) are drawn over in the correct place(s).
精彩评论