我的代码应该将2个图像中的2个对象组合成一个图像然后将其打印出来。第一个图像有一个对象和白色背景。第二张图片只是一个背景。我需要用第二张图片替换白色背景以创建新图片。我的代码没有打印出新图片。有什么帮助吗?
import java.awt.*;
class Chroma
{
public void newChroma (Picture noBGround, Picture newBGround)
{
int height = noBGround.getHeight();
int width = noBGround.getWidth();
Pixel[] pixels = noBGround.getPixels();
Pixel[] pixels2 = newBGround.getPixels();
Pixel p = null;
Pixel p2 = null;
for(int y=0; y<height; y++){
for(int x=0; x<width; x++){
p = pixels[x];
int red = p.getRed();
int green = p.getGreen();
int blue = p.getBlue();
p2 = pixels2[x];
int red2 = p2.getRed();
int green2 = p2.getGreen();
int blue2 = p2.getBlue();
if (red == 255 || green == 255 || blue == 255) {
p.setRed(red2);
p.setGreen(green2);
p.setBlue(blue2);
}
else {
p.setRed(red);
p.setGreen(green);
p.setBlue(blue);
}
}
}
noBGround.write("ChromaKey.jpg");
noBGround.explore();
}
}
public class ChromaKey
{
public static void main(String[] args)
{
Picture noBGround = new Picture("img1.jpg");
Picture newBGround = new Picture("img2.jpg");
Chroma obj = new Chroma();
}
}
答案 0 :(得分:0)
我看到的一个快速问题是你实际上没有在Chroma类中调用newChroma方法。在您说Chroma obj = new Chroma();
后,您应该跟进obj.newChroma();
。
同样在您的newChroma()
方法中,您实际上并未对您创建的像素p执行任何操作。你可能想说red == 255 && blue == 255 && green == 255
,使用而不是或。