4

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

This is My original image

Image After reconstruction

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();
    }
}
8
  • pixel array is 24 bit. where is alpha? Commented Aug 10, 2012 at 7:38
  • how we can defined Pixel Array? I have no Idea.Can u guide me Commented Aug 10, 2012 at 7:40
  • i looked at some structures and seen that it was r,g,b,alpha or alpha,r,g,b Commented Aug 10, 2012 at 7:41
  • you must be exchanging the alpha with red or blue. Commented Aug 10, 2012 at 7:42
  • so what is the solution? what should i do in my code? Commented Aug 10, 2012 at 7:42

1 Answer 1

8

You get a ARGB value from getRGB and the setRGB takes a ARGB value, so doing this is enough:

bufferImage2.setRGB(x, y, PixelArray[x][y]);

From the API of BufferedImage.getRGB:

Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.

...and from the API of BufferedImage.setRGB:

Sets a pixel in this BufferedImage to the specified RGB value. The pixel is assumed to be in the default RGB color model, TYPE_INT_ARGB, and default sRGB color space.


On the other hand I would recommend you do paint the image instead:

Graphics g = bufferImage2.getGraphics();
g.drawImage(g, 0, 0, null);
g.dispose();

Then you wouldn't need to worry about any color model etc.

Sign up to request clarification or add additional context in comments.

2 Comments

No problem :) Added a Graphics.drawImage example aswell.. maybe easier for you!
@Sumit you're forgetting to click the tick mark when people answer. You've done it here and in my answer earlier ;-) hint hint

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.