0

I am trying to convert my BufferedImage into a integer array, but i am getting the following error: "java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt"

Here's my bit of code:

public class Test {
  public static void main (String []  args) { 
    BufferedImage img = null;
    try {
      img = ImageIO.read(new File("G.bmp"));
    } catch (IOException e) { }

    int[] imgarray = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
  }
}

1 Answer 1

3

Try this:

byte[] imgarray = ((DataBufferByte)img.getRaster().getDataBuffer()).getData();
Sign up to request clarification or add additional context in comments.

3 Comments

thanks this works.. now i have a byte array however, but i suppose byte is sufficient :)
Yes, a byte array is usually utilized to pass any binary data (to a stream, or to create an image from it and vice versa).
Any idea why this isn't represented as img.getWritableData(), or even just img.buffer? Also, why do we need to separately handle the treatment of every BufferedImage type - isn't there an inheritance-friendly solution?

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.