我想在Java中合并4张图片一起 [英] I want to merge 4 pictures together in Java

查看:597
本文介绍了我想在Java中合并4张图片一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力理解如何合并4张图片一起在Java中,我想每个图像复制到合并后的图像,在50%的混合合并重叠的20个像素。得到合并图像20的像素分界是各图像的适当部分的混合。

I'm struggling to understand how to merge 4 pictures together in java, I want to copy each image to the merged image with the overlapping 20 pixels blended in a 50% merge. To give the merged image a 20 pixel boundary that is a blend of the appropriate portion of each image.

因此​​,与20个像素混合到对方的图像的4影像框。不知道,因为它是非常混乱,我应该如何使用图像的宽度和高度。

So a 4 image box with the images blended into each other by 20 pixels. Not sure how I should use the width and height of the images as it is very confusing.

喜欢的东西这个。怎么办呢?

Something like this. How to do it?

推荐答案

我得到了所有我的信息来自:的 的AlphaComposite ,的合成图形,的串联图像的。

I got all of my info from: AlphaComposite, Compositing Graphics, Concatenating Images.

以下程序被提高。它采用两种方法: joinHorizo​​ntal joinVertical 加入的图像。里面的方法,将出现以下情况

The following program is improved. It uses two methods: joinHorizontal and joinVertical to join the images. Inside the methods, the following happens


  • 第二图像被复制,但只有重叠部分

  • 复制的图像设置为半alpha(透明度)

  • 了返回图像',第一图像是画的画布,接着由第二无重叠部分上

  • 复制的图像画到画布上。

  • 图像,则返回

为什么我只设置半阿尔法一个图像,而不是两者兼而有之?

Why do I only set one image at half alpha and not both?

照片清晰,玻璃窗:

油漆随机点红这样的窗口中有一半是覆盖着红色。现在,对待窗口的红点作为新的画布。

Paint random points red so that half of the window is covered with red. Now, treat the window with the red dots as your new canvas.

油漆随机点蓝,使新的画布上是成功的一半覆盖着蓝色。该窗口将不会被完全覆盖;您仍然可以通过它看到的。

Paint random points blue so that the new "canvas" is half covered with blue. The window won't be completely covered; you will still be able to see through it.

但让我们想象,我们先画的窗口红色,再涂上了一半蓝色。现在,将一半的蓝色和半红,但不是透明的。

But let's imagine that we first painted the window red, and then painted half of it blue. Now, it will be half blue and half red, but not transparent at all.

public class ImageMerger {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        BufferedImage img1 = //some code here
        BufferedImage img2 = //some code here
        BufferedImage img3 = //some code here
        BufferedImage img4 = //some code here

        int mergeWidth = 20; // pixels to merge.
        BufferedImage merge =  ImageMerger.joinVertical(
                ImageMerger.joinHorizontal(img1, img2, mergeWidth),
                ImageMerger.joinHorizontal(img3, img4, mergeWidth),mergeWidth);

        //do whatever you want with merge. gets here in about 75 milliseconds

    }

    public static BufferedImage joinHorizontal(BufferedImage i1, BufferedImage i2, int mergeWidth){
        if (i1.getHeight() != i2.getHeight()) throw new IllegalArgumentException("Images i1 and i2 are not the same height");

        BufferedImage imgClone = new BufferedImage(mergeWidth, i2.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D cloneG = imgClone.createGraphics();
        cloneG.drawImage(i2, 0, 0, null);
        cloneG.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN, 0.5f));
        cloneG.drawImage(i2, 0, 0, null);

        BufferedImage result = new BufferedImage(i1.getWidth() + i2.getWidth() 
                - mergeWidth, i1.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = result.createGraphics();
        g.drawImage(i1, 0, 0, null);
        g.drawImage(i2.getSubimage(mergeWidth, 0, i2.getWidth() - mergeWidth,
                i2.getHeight()), i1.getWidth(), 0, null);
        g.drawImage(imgClone, i1.getWidth() - mergeWidth, 0, null);

        return result;
    }
    public static BufferedImage joinVertical(BufferedImage i1, BufferedImage i2, int mergeWidth){
        if (i1.getWidth() != i2.getWidth()) throw new IllegalArgumentException("Images i1 and i2 are not the same width");

        BufferedImage imgClone = new BufferedImage(i2.getWidth(), mergeWidth, BufferedImage.TYPE_INT_ARGB);
        Graphics2D cloneG = imgClone.createGraphics();
        cloneG.drawImage(i2, 0, 0, null);
        cloneG.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN, 0.5f));
        cloneG.drawImage(i2, 0, 0, null);

        BufferedImage result = new BufferedImage(i1.getWidth(), 
                i1.getHeight() + i2.getHeight() - mergeWidth, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = result.createGraphics();
        g.drawImage(i1, 0, 0, null);
        g.drawImage(i2.getSubimage(0, mergeWidth, i2.getWidth(),
                i2.getHeight() - mergeWidth), 0, i1.getHeight(), null);
        g.drawImage(imgClone, 0, i1.getHeight() - mergeWidth, null);

        return result;
    }

}

这篇关于我想在Java中合并4张图片一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆