Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Image to byte array conversion problem

843805May 17 2006 — edited May 18 2006
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 15 2006
Added on May 17 2006
3 comments
389 views