用 jai 创建马赛克
我正在尝试将多个图像串联成一个,但遇到了问题。为了隔离这个问题,我创建了以下 POC,我正在尝试解决该问题,以便了解如何使用 JAI API。
在 POC 中,我有两个图像“image1.gif”和“image2.gif”,它们都是 256x256。目标是将它们水平连接成一个 512x256 的图像“image3.png”。这是我使用 JAI 为此编写的代码。
try {
InputStream stream1 = new FileInputStream("D:\\poc\\image1.gif");
InputStream stream2 = new FileInputStream("D:\\poc\\image2.gif");
RenderedImage image1 = ImageIO.read(stream1);
RenderedImage image2 = ImageIO.read(stream2);
ImageLayout imageLayout = new ImageLayout(0,0,512,256);
RenderingHints renderingHints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, imageLayout);
RenderedImage finalImage = MosaicDescriptor.create(new RenderedImage[]{image1, image2}, MosaicDescriptor.MOSAIC_TYPE_OVERLAY, null, null, null, null, renderingHints);
FileStoreDescriptor.create(finalImage, "D:\\poc\\image3.png", "PNG", null, null, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
我得到的是正确尺寸 512x256 的图像,第一个图像(图像 1)完整地位于图像的左侧,而图像的右侧完全是黑色的。换句话说,就像我将 image1.gif 与 256x256 黑色方块连接起来。
我尝试将 MosaicType 更改为 MosaicDescriptor.MOSAIC_TYPE_BLEND,但这只会创建一个类似的图像,仅左半部分由覆盖在 image2 顶部的 image1 组成(反之亦然),而右半部分仍然完全黑色。
我还尝试更改 ImageLayout 构造函数的前两个参数。但这也不起作用 - 例如,我可以将 ImageLayout 的第一个参数设置为 -256,但随后 image1 显示在图像的右半部分,而图像的左半部分为黑色。
请帮忙!
I am trying to create a concatenation of multiple images into one, but have been having problems. To isolate this I have created the following POC which I am trying to solution in order that I understand how to use the JAI API.
In the POC, I have two images "image1.gif" and "image2.gif" which are both 256x256. The objective is to concatenate them horizontally into a single image "image3.png" which is 512x256. Here is the code I wrote for this using JAI.
try {
InputStream stream1 = new FileInputStream("D:\\poc\\image1.gif");
InputStream stream2 = new FileInputStream("D:\\poc\\image2.gif");
RenderedImage image1 = ImageIO.read(stream1);
RenderedImage image2 = ImageIO.read(stream2);
ImageLayout imageLayout = new ImageLayout(0,0,512,256);
RenderingHints renderingHints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, imageLayout);
RenderedImage finalImage = MosaicDescriptor.create(new RenderedImage[]{image1, image2}, MosaicDescriptor.MOSAIC_TYPE_OVERLAY, null, null, null, null, renderingHints);
FileStoreDescriptor.create(finalImage, "D:\\poc\\image3.png", "PNG", null, null, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
What I get is an image of correct dimensions 512x256, with the first image (image1) in its entirety on the left side of the image, and the right hand side of the image completely black. In other words, it is like I have concatenated the image1.gif with a 256x256 black square.
I have tried changing the MosaicType to MosaicDescriptor.MOSAIC_TYPE_BLEND, but this just creates a similar image only the left half consists of image1 overlaid on top of image2 (or vice-versa) with the right half still completely black.
I have also tried changing the first two parameters of the ImageLayout constructor. This doesn't work either though - for example, I can set the first parameter of ImageLayout to -256, but then image1 is shown on the right half of the image, with the left half of the image black.
Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我提出的最终解决方案 - 这些通用方法创建两个图像的马赛克来进行说明。您只需将它们映射到一个公共坐标空间 - 首先创建一个图像的马赛克(以增加整体大小,因为 ScaleDescriptor 由于某种原因不起作用),然后翻译第二个图像,以便它可以与第一的。
Here is the final solution I came up with - these generic methods create mosaics of two images to illustrate. You just have to map them to a common co-ordinate space - first create a mosaic of one image (to increase the overall size, as ScaleDescriptor does not work for some reason) and then translate the second one so it can be overlaid with the first.
这是使用 JAI 拼接两个图像的更简洁的方法。基本过程是将源图像转换(或扭曲)到马赛克的公共图像空间中。然后您可以将所有翻译后的图像提供给马赛克操作。无需执行两次马赛克操作。由于所有输入图像的图像空间的并集与所需的马赛克输出图像空间相对应,因此您不需要提供 ImageLayout 作为 RenderingHint。在此示例中,输入图像是在内存中创建的,但可以像原始帖子中那样从文件中读取它们。
Here's a more concise way to mosaic two images using JAI. The basic process is to translate (or warp) the source images into the common image space of the mosaic. Then you can supply all the translated images to the mosaic operation. There's no need to perform two mosaic operations. Since the union of the image spaces of all the input images corresponds with the desired output image space of the mosaic, you do not need to supply an ImageLayout as a RenderingHint. In this example the input images are created in memory, but they could be read in from a file as is done in the original post.