2013-03-10 47 views
1

我有3個相同大小的2D矩陣(比方說200行和300列)。每個矩陣都代表三種「基本」顏色(紅色,綠色和藍色)之一的值。矩陣的值可以介於0和255之間。現在我想組合這些矩陣,將它們顯示爲彩色圖像(200乘300像素)。我怎樣才能在JAVA中做到這一點?如何在JAVA中分別在3個矩陣中顯示由R G B值表示的圖像

+1

[你有什麼嘗試?](http://www.whathaveyoutried.com/) – 2013-03-10 13:04:22

回答

3

第一:你可以從這個價值創造顏色,如:

Color c = new Color(red, green, blue, alpha);

需要注意的是:

  1. 紅色是Matrics1
  2. 綠色價值是Matrics2
  3. 藍色的值是Matrics3
  4. 的價值

然後創建新的形象:

BufferedImage image = new BufferedImage(200/*Width*/, 300/*height*/, BufferedImage.TYPE_INT_ARGB);

然後將圖像像這樣的值:

image.setRGB(x, y, c.getRGB()); 

這是此步驟的代碼,試試看:

public class Main { 

    public static void main(String args[]) throws IOException { 
     int red[][] = new int[200][300]; 
     int green[][] = new int[200][300]; 
     int blue[][] = new int[200][300]; 
     /////////////////set this matrices 

     BufferedImage image = new BufferedImage(200/*Width*/, 300/*height*/, BufferedImage.TYPE_INT_ARGB); 

     for (int i = 0; i < 200; i++) { 
      for (int j = 0; j < 300; j++) { 
       Color c = new Color(red[i][j], green[i][j], blue[i][j]); 
       image.setRGB(i, j, c.getRGB()); 
      } 
     } 
     ImageIO.write(image, "jpg", new File("/////////////image path.jpg")); 
    } 
}