-1

I am trying to write an buffered image into a file that appends the next buffered image bytes.I have the following code for which some runtime exception is thrown. when i run this code i get the following exception. Why and what has to be changed?

 import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.imageio.ImageIO;






    public class FileT
    {
        public static void main(String[] args)
        {   
            try {
                BufferedImage originalImage = ImageIO.read(new File("ani.jpg"));
                int i=0,c=0;
                // convert BufferedImage to byte array
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageIO.write(originalImage, "jpg", baos);
                baos.flush();
                byte[] imageInByte = baos.toByteArray();
                byte[] copybuf = new byte[1024];
                baos.close();

                while(i<imageInByte.length)
                {
                    copybuf[c]=imageInByte[i];
                    c++;

                    if(i%1023==0)
                        {
                        // convert byte array back to BufferedImage
                       InputStream in = new ByteArrayInputStream(copybuf);
                       BufferedImage bImageFromConvert = ImageIO.read(in);
    ImageIO.write(bImageFromConvert, "jpg", new FileOutputStream(new File("ani1.jpg"),true));   

                        }
                        copybuf = new byte[1024];
                        i++;

             }
            }
            catch (IOException e) {
                System.out.println(e.getMessage());
            }
            }
    }
Exception in thread "main" java.lang.IllegalArgumentException: image == null!
    at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
    at javax.imageio.ImageIO.getWriter(ImageIO.java:1592)
    at javax.imageio.ImageIO.write(ImageIO.java:1578)
    at FileT.main(FileT.java:45)
Vishal S
  • 73
  • 11
  • `copybuf = new byte[1024];`...`while...if(i%1023==0)`, I think you should redo whatever you got going on here. This code scares me – ug_ Nov 07 '15 at 20:28
  • Please indent your code properly (use your editor/IDE options for that if you don't want to do it manually). Also which line is `45` in `FileT`? – Pshemo Nov 07 '15 at 20:30
  • This is the line : ImageIO.write(bImageFromConvert, "jpg", new FileOutputStream(new File("ani1.jpg"),true)); – Vishal S Nov 07 '15 at 20:35
  • `java.lang.IllegalArgumentException: image == null!` says that the argument `image` can not be `null` –  Nov 07 '15 at 20:39
  • yes but i do not understand why it is null – Vishal S Nov 07 '15 at 20:47
  • Read the javadoc of ImageIO.read and see why it returns a null. – Little Santi Nov 07 '15 at 20:53
  • Problem is, I am reading in chunks and trying to append the byte. This is a module i got from stack overflow as i would like to use it for client server application. if(i%1023==0) will send the chunk. But i could not write if i process the byte one by one and the to append it with FileOutputStream. So only i went a round about way with some modification in the if Loop. – Vishal S Nov 07 '15 at 21:19
  • I suggest you create a new question asking about the chunk partitioning of an image, because this has been closed. – Little Santi Nov 08 '15 at 19:43

1 Answers1

0
if(i%1023==0){
// convert byte array back to BufferedImage
    InputStream in = new ByteArrayInputStream(copybuf);
    BufferedImage bImageFromConvert = ImageIO.read(in);
    ImageIO.write(bImageFromConvert, "jpg", new FileOutputStream(new File("ani1.jpg"),true));
}
copybuf = new byte[1024];
i++;

In this code you might want to change new FileOutputStream(new File()) to be casted to ImageOutputStream

It is very hard to determine from your question what you need, but I think this will fix it. If it doesn't just leave a comment and Ill try to fix it

Kore
  • 436
  • 2
  • 14
  • ImageIO.write(bImageFromConvert, "jpg", new FileOutputStream(new File("ani1.jpg"),true)); This is the line i m getting error. Can u tell me exactly what has to be changed? I do not get that – Vishal S Nov 07 '15 at 20:41
  • check the duplicate question, it has your answer there – Kore Nov 07 '15 at 20:49
  • could you tell me the problem here? – Vishal S Nov 07 '15 at 20:55
  • your bimagefromconvert is null – Kore Nov 07 '15 at 20:57
  • but how? I have read some data from the image file. – Vishal S Nov 07 '15 at 21:03
  • read the duplicate question please – Kore Nov 07 '15 at 21:04
  • I am not getting null pointer exception at all. Please read the exception – Vishal S Nov 07 '15 at 21:14
  • it is similar, because passing an image as null is fine, but when imageio write gets it it throws theillegalargumentexception, this only occurs because image=null, so it is very similar to a NPE – Kore Nov 07 '15 at 21:17
  • Unless you tell me what exactly has to be changed i do not think i would understand. I have no experience in file handling before. I do not get anything from duplicate question so if u know its a duplicate question u must be knowing what is wrong. Please let me know why its empty which i think it is not. I even tried to print but same exception is thrown. – Vishal S Nov 07 '15 at 21:34