Search code examples
javaarrayscolorsbufferedimagepixel

Differences between BufferedImage pixels and Color.getRGB() pixels


I'm trying to get pixel data from a bufferedimage that will only contain a gray scale translucent image (argb). I'm getting a WritableRaster from the image and setting its pixels using the Raster's setPixels method. using this method, I get ArrayIndexOutOfBounds Exceptions. After doing some research, i found that BufferedImages store one integer per pixel per band instead of having one integer per pixel. That is, 8 bits for each band for each pixel. This causes me a ton of trouble as i try to access and change the types of colors. I'm at a loss. I'll post any other details if need be. Thank you, Sina


Solution

  • would you please share the code? you have to note that if you want to set a rectangle (128x128) on part of an image, you have to call the set RGB method like this

    //start points to draw
    int blockX=128,blockY=128;
    //size of the target rectangle need to be painted
    int w=128,h=128;
    int[] colors=new int[h*w];
    //....init color array
    //start painting from x value in array
    int colorOffset=0;
    //number of value should read from array for one row
    int paintWidth=128;
    //I think you get the Exception here, because of wrong number passed for drawing pixel for each row
    out.setRGB(blockX,blockY, w, h, colors, colorOffset, paintWidth);
    

    once again, we are not magician, please share the code buddy :) this example also would help you