0

I want to convert byte array into Image file . This is the portion of my code

      BufferedImage image = user_space(getImage(image_path(path,name,"jpg")));

            System.out.println("Image:"+image);
           //Image : BufferedImage@12d263f: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@12a0f6c transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 466 height = 336 #numData Elements 3 dataOff[0] = 2
            decode = decode_text(get_byte_data(image)); 
            try {
                System.out.println("length :"+decode.length);
                //length :- 73
                BufferedImage bImageFromConvert = ImageIO.read(new ByteArrayInputStream(decode));
                System.out.println(bImageFromConvert);          
                ImageIO.write(bImageFromConvert, "jpg", new File(
                        "D:/eeraj/new-darksouls.jpg"));
                }

     private byte[] get_byte_data(BufferedImage image)
       {
        WritableRaster raster   = image.getRaster();
        DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();
        return buffer.getData();
       }

     private byte[] decode_text(byte[] image)
        {
        int length = 0;
        int offset  = 24;
        //loop through 32 bytes of data to determine text length    
        for(int i=24; i<32; ++i) //i=24 will also work, as only the 4th byte   contains real data
        {
            length = (length << 1) | (image[i] & 1);
        }      
        byte[] result = new byte[length];                
        //loop through each byte of text
        for(int b=0; b<result.length; ++b )
        {

            //loop through each bit within a byte of text
            for(int i=0; i<8; ++i, ++offset)
            {
                //assign bit: [(new byte value) << 1] OR [(text byte) AND 1]
                result[b] = (byte)((result[b] << 1) | (image[offset] & 1));
            }
        }
        return result;
    }

I am getting an error

            java.lang.IllegalArgumentException: im == null!
                  at javax.imageio.ImageIO.write(Unknown Source)
                  at javax.imageio.ImageIO.write(Unknown Source)

the ImageIO.read method is returning null. That results in the exception later when I try and write the image.

I have search on google to find the solution for this problem but I found nothing specific apart from modifying the image header information .

Please let me know that how can I solve this problem ?

gaurav kumar
  • 171
  • 2
  • 6
  • 15
  • 1
    I suspect that it's telling you that your image data is bogus. Maybe decode_text or get_byte_data is broken. – Hot Licks Apr 07 '13 at 18:46
  • are yousure `decode` contains valid image data ? see what [javadoc](http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html#read%28javax.imageio.stream.ImageInputStream%29) says about [ImageIO.read()](http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html#read%28javax.imageio.stream.ImageInputStream%29) – A4L Apr 07 '13 at 18:46
  • As I am getting the correct length of the image after decoding so there is no problem in the decoding section. – gaurav kumar Apr 07 '13 at 18:52
  • So you could be getting the right length of zeros and that would be OK? – Hot Licks Apr 07 '13 at 19:00
  • Thanks @Hot Licks.. I have added the remaining method details So now you can check the error that I am doing . – gaurav kumar Apr 07 '13 at 19:01
  • I don't see how you can have the length and result data both encoded in the LSB of the same bytes of image. – Hot Licks Apr 07 '13 at 19:06
  • What file format do you believe you're decoding, BTW? Generally a JPG file is compressed, and you'd have to uncompress it before you could do anything with it. – Hot Licks Apr 07 '13 at 20:34

0 Answers0