I want to create an image from a 2D array. I used the concept of BufferImage to create Image.but there is a difference between the original image and the constructed image in the image below


I am using the following code
import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO;
/ ** * * @author pratibha * /
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<width;i++){ for(int j=0;j<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<height;y++){ for(int x=0;x<width;x++){ int Pixel=PixelArray[x][y]<<16 | PixelArray[x][y] << 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(); } }
source share