0

I have an int array containing gray scale values from 0-254, i also have the x and y size of the image. It is an easy thing to create an pgm image, but i want to display it in a jsp, so i need somehow to convert it to a jpeg or png image. If you suggest jai, than please tell me at which classes to look, or how to actually do it in jai. Thanks a lot, in advance.

Red33mer
  • 810
  • 3
  • 15
  • 22

2 Answers2

2

Maybe skip the PGM entirely?

int[] myImage = getGreyscaleIntArray();

BufferedImage im = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
WritableRaster raster = im.getRaster();
for(int h=0;h<height;h++)
{
    for(int w=0;w<width;w++)
    {
        raster.setSample(w,h,0, myImage[h * width + w]); 
    }
}

ByteArrayOutputStream myJpg = new ByteArrayOutputStream();
javax.imageio.ImageIO.write(im, "jpg", myJpg);

uses the JAI ImageIO api, specifically the ImageIO utility class

WriteableRaster sample from the Java Image Processing cookbook

Stobor
  • 44,246
  • 6
  • 66
  • 69
  • This was the first thing a thought about. But it doesnt work. Try reading a pgm file. ImageIO.read() will always return null if it's pgm format. – Red33mer Aug 09 '09 at 13:49
0

ImageMagick works well for converting images and Jmagick provides an interface to call directly from java programs.

GregA100k
  • 1,385
  • 1
  • 11
  • 16
  • I would have to install ImageMagick. I don't want to do that, since i wouldn't be able to deploy the app on any server running tomcat. – Red33mer Aug 09 '09 at 13:52