I am using the following code to convert an Image to a byte array. I have to convert the image to a byte arry so that the MS SQL server can be updated with the changes on the image.
public static byte[] convertImage(Image img) {
try {
int[] pix = new int[img.getWidth(null) * img.getHeight(null)];
PixelGrabber pg = new PixelGrabber(img, 0, 0, img.getWidth(null),
img.getHeight(null), pix, 0, img.getWidth(null));
pg.grabPixels();
System.out.println(pix.length);
byte[] pixels = new byte[img.getWidth(null) * img.getHeight(null)];
for (int j = 0; j < pix.length; j++) {
pixels[j] = new Integer(pix[j]).byteValue();
}
return pixels;
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
However I am getting the following error message while running the program.
in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at lizard.util.MemoryFileInputFilter.readUnsignedByte(MemoryFileInputFilter.java:103)
at lizard.util.MemoryFileInputFilter.readUnsignedShort(MemoryFileInputFilter.java:158)
at lizard.tiff.IFD.read(IFD.java:154)
at lizard.tiff.Tiff.read(Tiff.java:130)
I am using the Lizard library, the following utility method blows up on the line : t.getImage(0); How can I fix this problem?
public static Image getFrontImage(byte[] image) {
Tiff t = new Tiff();
try {
t.read(image);
Image check = t.getImage(0);
return check;
} catch (IOException e1) {
System.out.println("Exception occurred while displaying image due to "
+ e1.getMessage());
e1.printStackTrace();
}
return null;
}
Thanks in advance.