2016-07-15 108 views
0

我的代碼應該將來自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

您創建'Chroma'類,但您似乎沒有調用'newChroma(圖片,圖片)'方法 – jr593

+0

我進行了更改,除非現在打印的圖像沒有被更改。作爲我的結果,我只得到帶有白色背景的照片。 – Coder02983409

回答

0

我看到的一個快速問題是,您實際上並沒有在您的Chroma類中調用newChroma方法。在您說Chroma obj = new Chroma();後,您應該跟進obj.newChroma();

同樣在你的newChroma()方法中,你實際上並沒有對你創建的像素p做任何事情。你可能的意思是說red == 255 && blue == 255 && green == 255,使用而不是或。

+0

現在它只打印一個白色背景。新的背景沒有改變。 – Coder02983409

+0

你從哪裏得到你的'Picture'和'Pixel'類。我想也許這些在你改變它們後不會被放入圖像。因此,寫入的圖像沒有改變。 – jr593