Question

The dimensions of the output image is somewhat reversed. Also the alignment is reversed.

BufferedImage theImage = new BufferedImage(h,w, BufferedImage.TYPE_BYTE_GRAY);
      int [] pixtotal=new int [total_pixels];

      for (int x = 0; x < w; x++)
    {
         for (int y = 0; y < h; y++)
         {
          int gray= image.getRGB(x, y)& 0xFF;
          pixtotal[i]=gray;
          i++;

         }
    }

File outputfile = new File("e:/bndwimage.jpg");
WritableRaster raster = (WritableRaster) theImage.getData();
raster.setPixels(0,0,h,w,pixtotal);
theImage.setData(raster);
ImageIO.write(theImage, "jpg", outputfile);

h and w are height and width respectively. Why am I getting wrong image ??

EDIT : P.S : I have tried all the combinations of w and h .

on setting raster with raster.setPixels(0,0,w,h,pixtotal), it gives : ArrayIndexOutOfBoundsException: Coordinate out of bounds!

Était-ce utile?

La solution

raster.setPixels(0,0,h,w,pixtotal);

Per the Oracle documentation, setPixels takes the parameters x, y, w, h, iArray. You've swapped width and height, which would logically swap the dimensions of the image. This also dramatically changes how the pixels in the array are parsed, without causing any errors.

The same problem applies to your new BufferedImage(h,w, BufferedImage.TYPE_BYTE_GRAY).

Finally, I don't see where i is defined, but "increment by 1" is not generally how pixels are indexed into a 1-dimensional array. x + (y * w) should be the correct index for any given pixel.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top