Hello everyone! I have tried again and again to get this to work using multiple suggestions from the Internet. I am trying to convert an int[] to an Image object. I am getting the array using a PixelGrabber object:
int img1_1d[] = new int[img1_w * img1_h];
PixelGrabber grab1 = new PixelGrabber(img1,0,0,img1_w,img1_h,img1_1d,0,img1_w);
try {
boolean r = grab1.grabPixels();
if (!r) System.out.println("Pixel grabber ERROR");
} catch (InterruptedException e2) {
e2.printStackTrace();
System.exit(1);
}
I am attempting to create the Image object using this code:
public static Image getImageFromArray(int[] pixels, int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0,0,width,height,pixels);
return image;
}
Every time the above method gets called, the image return is of the right dimesions but is solid black. What is happening? I need this fixed as soon as possible and thanks in advance for the help.