Image to byte array conversion problem

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.

publicstaticbyte[] convertImage(Image img){

try{

int[] pix =newint[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 =newbyte[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();

}

returnnull;

}

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?

publicstatic 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();

}

returnnull;

}

Thanks in advance.

[3294 byte] By [balachandrana] at [2007-10-2 20:21:46]
# 1

I guess I'm not understanding. When I do image to byte[], I do something like this:

byte[] buffer = null;

try {

BufferedImage image = ImageIO.read(this.getClass().getResource("images/someimage.jpg"));

ByteArrayOutputStream bos = new ByteArrayOutputStream();

ImageIO.write(image, "jpg", bos);

buffer = bos.toByteArray();

} catch (Exception e) {

e.printStackTrace();

}

gestes59a at 2007-7-13 23:04:21 > top of Java-index,Desktop,Core GUI APIs...
# 2

The image format that I am working with is TIFF. The input device that generates the TIFF images are displayed on the panel, when a user flips an image, inside the paint method I create a new BufferedImage and draw the flipped image on it.

This flipped buffered image has to be converted to byte array in order to save it in the MS SQL server.

To me it seems like that I may have to do the TIFF encoding before converting the image to a byte array.

So if I do:

1. Flip the image by using BufferedImage.

2. Tiff encode the image.

3. Convert the image to byte array.

Then there should be no problem displaying the images from the database. Whe I read the image from the database I have to convert the byte array to an image again.

What are my options for converting the image to a byte array? Thanks for yoru time!

balachandrana at 2007-7-13 23:04:21 > top of Java-index,Desktop,Core GUI APIs...
# 3

After some digging into old posts in JAI-INTEREST archives. I finally got this working. Hope this helps someone. Here is the new convertImage method that I am using based on the posts on the mailing list:

public static byte[] convertImage(Image img) {

ParameterBlock pb = new ParameterBlock();

pb.add(img) ;

PlanarImage image = JAI.create("awtImage", pb,null);

ByteArrayOutputStream stream = new ByteArrayOutputStream();

JAI.create("encode", image, stream, "tiff");

byte[] b = stream.toByteArray();

return b;

}

balachandrana at 2007-7-13 23:04:21 > top of Java-index,Desktop,Core GUI APIs...