2014-03-25 27 views
0

所以我想要做的是拍摄两张图像,并得到看起来像两个平铺的结果。每个拼贴的大小在程序中设置(它应该是用户控制的,但我稍后会担心),大小设置将是广场的长度和高度。首先,我尝试平铺其中一幅图像,并将另一幅图像设置为背景图像,但是当我试图使空白平铺透明时,它会完成整幅图像,并且最终图像中没有平铺。我现在拥有它的方式,结果只是带有白色框的第一张图像,我希望背景图像出现。以平铺的方式连接两个图像

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

最终图片应该看起来像棋盘。所以像上面的“||”是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); 

} 
+0

所以结果应该是一个大的图像,其中两个输入ima每个“瓦”版图像的大小为10x10(在这个例子中)是交替地绘制的,就像瓷砖一样,棋盘式的? – Marco13

+0

是的,瓷砖的顺序应该看起来像棋盘。我编辑了文档以包含大部分布局的外观。 – user3337808

回答

0

嗯,我认为还是有解释这两种方式,但基于您迄今已发布的代码,我想这应该是你在找什么:

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参数方法仅绘制应该在相应位置处绘制的(第一或第二)输入图像的部分

+0

非常感谢!这工作!我甚至没有考虑过使用所有参数 – user3337808