3

I want to convert WritableRaster to byte array, but cannot cast to DataBufferByte

        BufferedImage thumbnail = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        thumbnail.createGraphics().drawImage(ImageIO.read(imageFile).getScaledInstance(100, 100, Image.SCALE_SMOOTH),0,0,null);


        WritableRaster raster = thumbnail.getRaster();

        // throws ClassCastException
        // java.awt.image.DataBufferInt cannot be cast to java.awt.image.DataBufferByte
        DataBufferByte imgData = (DataBufferByte) raster.getDataBuffer();

        byte[] data = imgData.getData();
  • The `DataBuffer` instance can be of a number of types one being `DataBufferByte` so casting won't work here. Looking at the sources you probably get a `DataBufferInt` which represents each pixel as an integer. What do you need the bytes for? – Thomas Oct 05 '15 at 14:50
  • 1
    Have a look here btw: http://stackoverflow.com/questions/15414259/java-bufferedimage-to-byte-array-and-back – Thomas Oct 05 '15 at 14:51
  • DataBufferByte and DataBufferInt are two different non-hierarachical classes, so no, you can't cast to a DataBufferByte. What are you trying to accomplish? – ControlAltDel Oct 05 '15 at 14:54

0 Answers0