5

I am getting the int array from png image how I will convert this to bufferdimage or creating new PNG file ?

int[] pixel = new int[w1*h1];
        int i = 0;
        for (int xx = 0; xx < h1; xx++) {
            for (int yy = 0; yy < w1; yy++) {
                        pixel[i] = img.getRGB(yy, xx);
                        i++;
                }
         }
1

3 Answers 3

2

If you have an array of integers which are packed RGB values, this is the java code to save it to a file:

int width = 100;
int height = 100;
int[] rgbs = buildRaster(width, height);

DataBuffer rgbData = new DataBufferInt(rgbs, rgbs.length);

WritableRaster raster = Raster.createPackedRaster(rgbData, width, height, width,
    new int[]{0xff0000, 0xff00, 0xff},
    null);

ColorModel colorModel = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);

BufferedImage img = new BufferedImage(colorModel, raster, false, null);

String fname = "/tmp/whatI.png";
ImageIO.write(img, "png", new File(fname));
System.out.println("wrote to "+fname);

The reason for the arrays 0xff0000, 0xff00, 0xff is that the RGB bytes are packed with blue in the least significant byte. If you pack your ints different, alter that array.

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

Comments

1

You can rebuild the image manually, this is however a pretty expensive operation.

BufferedImage image = new BufferedImage(64, 64, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();

for(int i = 0; i < pixels.size(); i++)
{
    g.setColor(new java.awt.Color(pixels.get(i).getRed(), pixels.get(i).getGreen(), pixels.get(i).getBlue()));
    g.fillRect(pixels.get(i).getxPos(), pixels.get(i).getyPos(), 1, 1);
}

try 
{
    ImageIO.write(image, "PNG", new File("imageName.png"))
} 

catch(IOException error) 
{
    error.printStackTrace();
}

I formatted your image array into an object, this is personal preference tho (of course you could us an int array with this model as well). Keep in mind that you can always add the alpha to there as well.

Comments

0

Try the ImageIO class, which can take a byte array representing pixel data to build an image object and then writing it out in a particular format.

try {
    BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(yourBytes));
    ImageIO.write(bufferedImage, "png", new File("out.png"));
} catch (IOException e) {
    e.printStackTrace();
}

5 Comments

I have int array not byte
Exception in thread "main" java.lang.IllegalArgumentException: image == null! at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(Unknown Source) at javax.imageio.ImageIO.getWriter(Unknown Source) at javax.imageio.ImageIO.write(Unknown Source)
Why do you need an int array? RGB values go from 0 to 255.
because the 2 first pixel of my png image manipluated like int
@ELMEKKIAnouar I don't see how that has anything to do with why you need to store your RGB values as ints.

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.