java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt..

how do resolve it...

Uhm don't cast?

We might be able to give you some real help if you would post the exact and complete error message and full stack trace as well as some relevant code (using code tags).

i trying this

1.BufferedImage image1=compress(robot.createScreenCapture(new Rectangle(width, height)), 0.5f);
2.Raster ras = ((BufferedImage) image1).getData();
3.DataBufferInt db = (DataBufferInt) ras.getDataBuffer();//error
5.int[] data = db.getData();

without calling compress method it works fine but in case of Compress...

java.lang.ClassCastException: java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt
at server.ScreenServer.sendScreen(ScreenServer.java:3)

here compress method...

public static void write(BufferedImage image, float quality, OutputStream out) throws IOException {
    Iterator writers = ImageIO.getImageWritersBySuffix("jpeg");
    if (!writers.hasNext())
        throw new IllegalStateException("No writers found");
    ImageWriter writer = (ImageWriter) writers.next();
    ImageOutputStream ios = ImageIO.createImageOutputStream(out);
    writer.setOutput(ios);
    ImageWriteParam param = writer.getDefaultWriteParam();
    if (quality >= 0) {
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality);
    }
    writer.write(null, new IIOImage(image, null, null), param);
}

public static BufferedImage read(byte[] bytes) {
    try {
        return ImageIO.read(new ByteArrayInputStream(bytes));
    } catch(IOException e) {
        throw new RuntimeException(e);
    }
}

public static byte[] getBytes(BufferedImage image, float quality) {
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream(50000);
        write(image, quality, out);
        return out.toByteArray();
    } catch(IOException e) {
        throw new RuntimeException(e);
    }
}

public static BufferedImage compress(BufferedImage image, float quality) {
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        write(image, quality, out);
        return ImageIO.read(new ByteArrayInputStream(out.toByteArray()));
    } catch(IOException e) {
        throw new RuntimeException(e);
    }
}

Edited 6 Years Ago by musthafa.aj: n/a

Well, getDataBuffer is only contracted to return a "DataBuffer", whether that "DataBuffer" is a "DataBufferByte" or "DataBufferInt", I would assume varies from image type to image type, and, seemingly, your compress method winds up changing it the to different type than what "createScreenCapture" returns, thereby changing the type of "DataBuffer" that underlies the image.

If you need the int color values array behind an image (and you have a BufferedImage anyway) then use the getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) method.

yes,you absolutely right!.

i just confused about parameters within getRGB method...

now i got it...

thank you masijad.... ;-)

This question has already been answered. Start a new discussion instead.