Decoding JPG to BufferedImage TYPE_BYTE_GRAY
I am seeking some assistance in using the ImageIO API to read raw byte JPG images to the specific TYPE_BYTE_GRAY BufferedImage. The JPG source files are 8 bit grayscale images.
If I use the createImage (byte rawImageData, int imageoffset, int imagelength) in the java.awt.Toolkit, I get a perfectly good image 8 bit gray scale image, but not a BufferedImage.
If I use the ImageIO.read(new ByteArrayInputStream ( rawImageData )) I get an image BufferedImage but the souce image is now an TYPE_USHORT_GRAY.
I have tried to use the ImageReadParam class and set the destination image to a BufferedImage.TYPE_BYTE_GRAY as follows;
ImageInputStream stream = ImageIO.createImageInputStream( new ByteArrayInputStream(pImage));
ImageReader reader = (ImageReader) iter.next();
ImageReadParam param = reader.getDefaultReadParam();
image = new BufferedImage(640, 480, BufferedImage.TYPE_BYTE_GRAY);
reader.setInput(stream, true, true);
image = reader.read(0, param);
The image returned is a BufferedImage and appears to be BufferedImage.TYPE_BYTE_GRAY, however, all the pixels are zero. I feel I must be missing something simple but I just am unable to crack it. Any pointers offered will be much appreciated.
I am using Java Version 1.5.0_22-b03 on a Windows XP operating system.