尝试将BufferedImage转换为int数组时出错

时间:2012-05-03 00:25:39

标签: java arrays bufferedimage

我正在尝试将BufferedImage转换为整数数组,但是我收到以下错误:“java.awt.image.DataBufferByte无法转换为java.awt.image.DataBufferInt

这是我的一些代码:

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 个答案:

答案 0 :(得分:3)

试试这个:

byte[] imgarray = ((DataBufferByte)img.getRaster().getDataBuffer()).getData();