3

Image converted to RGB grayscale.
Next, Grayscale-image is translated into an array, which are performed to define a transformation. As a result, the array consists of "0" and "255".

Then, I need to turn this array into BufferedImage.

I used the code:

public static BufferedImage getImageFromArray(int pixelsMain[][], int width, int height) throws IOException {

        int pixels[] = new int[320*240];

        for(int i=0, numb=0; i<pixelsMain.length; i++)
            for(int j=0; j<pixelsMain[i].length; j++){
                pixels[numb]=pixelsMain[i][j];
                numb++;
            }

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
        WritableRaster raster = (WritableRaster) image.getData();
        raster.setPixels(0,0,width,height,pixels);
        try {
            ImageIO.write(image, "bmp", new FileOutputStream("[path]"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

However, after performing the method - all the values ​​of "255" converted to "-1".

As a result, the image is completely black.

Can you please tell how to solve the problem?

2
  • did you mean 255? That's just interpretation: 255 unsigned char == -1 signed char Commented Jun 15, 2012 at 3:50
  • It's true) corrected) If the translation is correct - for some reason the image is written to disk as a black rectangle? Commented Jun 15, 2012 at 3:53

1 Answer 1

4

Use image.getRaster() instead of (WritableRaster)image.getData(). The latter is making a copy, so changing it has no effect.

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

Comments

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.