所以我要做的就是拍摄两张图片并得到一个看起来像是两者的平铺的结果。每个图块的大小在程序中设置(它应该由用户控制,但我稍后会担心)并且尺寸设置将是正方形的长度和高度。首先,我尝试平铺其中一个图像并将另一个图像设置为背景图像,但是当我尝试使空白图块变得透明时,它使整个图像成为这样,并且最终图像中没有平铺。我现在拥有它的方式,结果只是带有白框的第一张图片,我想要出现背景图片。
|| || || ||
|| || ||
|| || || ||
|| || ||
|| || || ||
|| || ||
|| || || ||
最终的图片应该看起来像棋盘格。就像上面的“||”一样是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 :(得分: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);
}
}
这里的想法是创建输出图像,并为每个&#34; tile&#34;只绘制应该在相应位置绘制的(第一个或第二个)输入图像的部分,使用这个漂亮的10参数 - Graphics#drawImage
方法