簡體   English   中英

以平鋪方式串聯兩個圖像

[英]Concatenating two images in tiled fashion

所以我想做的是拍攝兩個圖像,並得到看起來像兩個圖像的結果。 每個磁貼的大小是在程序中設置的(應該由用戶控制,但我稍后會擔心),設置的大小將是正方形的長度和高度。 首先,我嘗試平鋪其中一幅圖像,並將另一幅圖像設置為背景圖像,但是當我嘗試使空圖塊透明時,它使整個圖像保持這種方式,並且最終圖像中沒有平鋪。 我現在所擁有的方式,結果就是我希望背景圖像出現的帶有白色框的第一張圖像。

||  ||  ||  ||
  ||  ||  ||  
||  ||  ||  ||
  ||  ||  || 
||  ||  ||  ||
  ||  ||  ||  
||  ||  ||  ||

最終圖片應看起來像棋盤格。 就像上面的“ ||”一樣 是image1,而“”是image2。 謝謝!

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Merging {
static BufferedImage background;
static BufferedImage foreground;

public static void main(String[] args) {    
            // load source images
            try {
            foreground = ImageIO.read(new File("C:\\Users\\Owner\\Desktop\\CSCI1302\\Project 2\\sample1.png"));
            background = ImageIO.read(new File("C:\\Users\\Owner\\Desktop\\CSCI1302\\Project 2\\sample2.png"));
            } catch (IOException e) {}

            // create the new image, canvas size is the max. of both image sizes
            int w = Math.max(foreground.getWidth(), foreground.getWidth());
            int h = Math.max(foreground.getHeight(), foreground.getHeight());

            // edit the overlay to delete pixels (vertical stripes)
            Graphics2D g = (Graphics2D) foreground.getGraphics();
            Graphics2D g2 = (Graphics2D) background.getGraphics();

            // edit the overlay to delete pixels (checkers)
            Color c = new Color(1f,0f,0f,.5f ); // tried to set this to checkers for transparency purposes
            int checker = 10;
            for(int row = 0; row <= foreground.getHeight() / checker; row++) {
                for(int col = 0; col <= foreground.getHeight() / checker; col++) {
                    if(row % 2 == 0) {
                        g.fillRect(row*checker, checker*col*2, checker, checker);
                        g2.fillRect(row*checker, checker+checker*col*2, checker, checker);
                    }
                    else {
                        g.fillRect(row*checker, checker+checker*col*2, checker, checker);
                        g2.fillRect(row*checker, checker*col*2, checker, checker);
                    }
                }
            }

            // paint both images, preserving the alpha channels
            BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g3 = (Graphics2D) combined.getGraphics();
            g3.drawImage(background, 0, 0, null);
            g3.drawImage(foreground, 0, 0, null);

            ImageIcon i1 = new ImageIcon(combined);
                JOptionPane.showMessageDialog(null, i3);

}

好吧,我認為仍然有兩種解釋方式,但是根據您到目前為止發布的代碼,我認為這應該是您要尋找的內容:

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Merging
{
    static BufferedImage background;
    static BufferedImage foreground;

    public static void main(String[] args)
    {
        // load source images
        try
        {
            foreground = ImageIO.read(new File("image0.jpg"));
            background = ImageIO.read(new File("image1.jpg"));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        // create the new image, canvas size is the max. of both image sizes
        int w = Math.max(foreground.getWidth(), foreground.getWidth());
        int h = Math.max(foreground.getHeight(), foreground.getHeight());
        int checker = 10;

        BufferedImage combined = new BufferedImage(w, h,
            BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) combined.getGraphics();
        for (int row = 0; row <= h / checker; row++)
        {
            for (int col = 0; col <= w / checker; col++)
            {
                BufferedImage source = foreground;
                if ((row+col) % 2 == 0)
                {
                    source = background;
                }
                int dx0 = col * checker;
                int dy0 = row * checker;
                int dx1 = dx0 + checker;
                int dy1 = dy0 + checker;
                int sx0 = col * checker;
                int sy0 = row * checker;
                int sx1 = dx0 + checker;
                int sy1 = dy0 + checker;
                g.drawImage(source, dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, null);
            }
        }
        ImageIcon i1 = new ImageIcon(combined);
        JOptionPane.showMessageDialog(null, i1);
    }
}

此處的想法是創建輸出圖像,並使用該漂亮的10參數Graphics#drawImage對於每個“平鋪”僅繪制應在相應位置繪制的(第一幅或第二幅)輸入圖像的部分。 Graphics#drawImage方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM