0

Currently working on Image manipulation in Java

I have the byte array(PPM) of size 921600 (640*480*3)

byte[] image; // PPM

BufferedImage image = ImageIO.read(new ByteArrayInputStream(image));

image is null.

Tried with ImageMagic and JAI libraries. But it does not help me.

Is it possible to get the RGB components from byte array and convert it to JPG file.

Any help is appreciated.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Amarnath R
  • 973
  • 3
  • 14
  • 33
  • check for a solution here http://stackoverflow.com/questions/9943962/imageio-read-returns-null-with-no-errors – Lysenko Andrii Jul 24 '15 at 07:43
  • If your `byte[] image` was actually a PPM file (or the bytes in PPM format), JAI ImageIO (`jai_imageio.jar`) would be able to read it. However, from your answer, it seems to be just the "raw" pixels (ie. the width, height and maxval should be stored *inside* the byte array in PPM format). – Harald K Jul 24 '15 at 18:13

2 Answers2

0

Below is the Code (which will convert PPM(byte array to Buffered image and you can save buffered image to the file)

// Method Call

BufferedImage image = ppm(width, height, 255, byte[]);

//Method Definition

static public BufferedImage ppm(int width, int height, int maxcolval, byte[] data){
        if(maxcolval<256){
            BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            int r,g,b,k=0,pixel;
            if(maxcolval==255){                                      // don't scale
                for(int y=0;y<height;y++){
                    for(int x=0;(x<width)&&((k+3)<data.length);x++){
                        r=data[k++] & 0xFF;
                        g=data[k++] & 0xFF;
                        b=data[k++] & 0xFF;
                        pixel=0xFF000000+(r<<16)+(g<<8)+b;
                        image.setRGB(x,y,pixel);
                    }
                }
            }
            else{
                for(int y=0;y<height;y++){
                    for(int x=0;(x<width)&&((k+3)<data.length);x++){
                        r=data[k++] & 0xFF;r=((r*255)+(maxcolval>>1))/maxcolval;  // scale to 0..255 range
                        g=data[k++] & 0xFF;g=((g*255)+(maxcolval>>1))/maxcolval;
                        b=data[k++] & 0xFF;b=((b*255)+(maxcolval>>1))/maxcolval;
                        pixel=0xFF000000+(r<<16)+(g<<8)+b;
                        image.setRGB(x,y,pixel);
                    }
                }
            }
            return image;
        }
        else{


            BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            int r,g,b,k=0,pixel;
            for(int y=0;y<height;y++){
                for(int x=0;(x<width)&&((k+6)<data.length);x++){
                    r=(data[k++] & 0xFF)|((data[k++] & 0xFF)<<8);r=((r*255)+(maxcolval>>1))/maxcolval;  // scale to 0..255 range
                    g=(data[k++] & 0xFF)|((data[k++] & 0xFF)<<8);g=((g*255)+(maxcolval>>1))/maxcolval;
                    b=(data[k++] & 0xFF)|((data[k++] & 0xFF)<<8);b=((b*255)+(maxcolval>>1))/maxcolval;
                    pixel=0xFF000000+(r<<16)+(g<<8)+b;
                    image.setRGB(x,y,pixel);
                }
            }
            return image;
        }
    }
Amarnath R
  • 973
  • 3
  • 14
  • 33
0

You can use a WritableRaster to set the pixels in the image for you:


For a grayscale image, you will have a byte array like this:

byte[] arr = new byte[width * height];

To make an image, use:

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
img.getRaster().setDataElements(0, 0, width, height, arr);

For a color image, you will have an array like this:

byte[] arr = new byte[width * height * 3];

So, to make an image, use:

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
img.getRaster().setDataElements(0, 0, width, height, arr);

You may need to mess this the TYPE in the first line. See here to choose which type your image is.

Aaron Berger
  • 71
  • 1
  • 4