2

I have 1D array of pixel values and i can get red, green and blue this way.

int rgb[] = new int[]
        {
            (argb >> 16) & 0xff, //red
            (argb >>  8) & 0xff, //green
            (argb      ) & 0xff  //blue
        };

I know width height of image as well which I want to create. So, in total I have following data. 1) width of new image 2) height of new image 3) one dimension array of pixel value.

My supervisor has advised me to use createRaster method but function arguments are hard to understand for me. Can you suggest me some simple code? Thanks.

Salman Aslam
  • 761
  • 1
  • 11
  • 20
  • What are you finding hard to understand about the function arguments in createRaster. Your supervisor has set you a task for you to learn and understand something, not to take a short-cut and just ask for some code. – Nick Bull Feb 01 '12 at 11:04

2 Answers2

1

As stated in this previous SO post:

public static Image getImageFromArray(int[] pixels, int width, int height) {
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            WritableRaster raster = (WritableRaster) image.getData();
            raster.setPixels(0,0,width,height,pixels);
            return image;
        }

If you are having trouble understanding what the parameters are, you should take a look at the Java Documentation.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
0

You could:

 InputStream is = new ByteArrayInputStream(rgb);
 Image img = ImageIO.read(is);

rgb should be a byte array.

vulkanino
  • 9,074
  • 7
  • 44
  • 71