我的代碼應該將來自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();
}
}
您創建'Chroma'類,但您似乎沒有調用'newChroma(圖片,圖片)'方法 – jr593
我進行了更改,除非現在打印的圖像沒有被更改。作爲我的結果,我只得到帶有白色背景的照片。 – Coder02983409