title: Constructing Image From 2D Array in Java
body: <p>I want to create the Image from 2D Array.I used BufferImage concept to construct Image.but there is difference betwwen original Image and constructed Image is displayed by image below</p>
  
  <p><img src="https://i.stack.imgur.com/FbSsZ.jpg" alt="This is My original image"></p>
  
  <p><img src="https://i.stack.imgur.com/llO2K.jpg" alt="Image After reconstruction"></p>
  
  <p>I am using the following code</p>
  
  <pre><code>import java.awt.image.BufferedImage;
  import java.io.File;
  import javax.imageio.ImageIO;
  </code></pre>
  
  <p>/**
   *
   * @author pratibha
   */</p>
  
  <pre><code>public class ConstructImage{
      int[][] PixelArray;
      public ConstructImage(){
          try{
  
          BufferedImage bufferimage=ImageIO.read(new File("D:/q.jpg"));
          int height=bufferimage.getHeight();
          int width=bufferimage.getWidth();
          PixelArray=new int[width][height];
          for(int i=0;i&lt;width;i++){
              for(int j=0;j&lt;height;j++){
                  PixelArray[i][j]=bufferimage.getRGB(i, j);
              }
          }
          ///////create Image from this PixelArray
          BufferedImage bufferImage2=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
  
          for(int y=0;y&lt;height;y++){
              for(int x=0;x&lt;width;x++){
                  int Pixel=PixelArray[x][y]&lt;&lt;16 | PixelArray[x][y] &lt;&lt; 8 | PixelArray[x][y];
                  bufferImage2.setRGB(x, y,Pixel);
              }
  
  
          }
  
           File outputfile = new File("D:\\saved.jpg");
              ImageIO.write(bufferImage2, "jpg", outputfile);
  
  
          }
          catch(Exception ee){
              ee.printStackTrace();
          }
      }
  
      public static void main(String args[]){
          ConstructImage c=new ConstructImage();
      }
  }
  </code></pre>