2016-07-15 53 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