5 Replies Latest reply: Mar 26, 2008 11:15 AM by 807591 RSS

    inputstream to image object

    807591
      Hello
      I am trying to make an image object from a byte array or a byte. I do not want to write the image too a file because it takes too long to read the file. the image object will be passed to a another class when it is made. I think this is a faster way to read it then making a file and then reading from it again.

      This is my attempt at it but i keep getting null from it. It does not seem to read into the image object. Does any know how to make an image object from an inputstream without writing it to a file. So then i can pass it on to the other class.
                     InputStream is = op.openInputStream();
                           StringBuffer buf = new StringBuffer();
                           byte[] data= new byte[8];
                           while(true)
                           {
                           Image image = null;
                           while ((is.read(data)) != -1) {
                                //System.out.println("\ndata "+data);
                                image=ImageIO.read(new ByteArrayInputStream(data));
                                System.out.println(image);
                           }
                          System.out.println(image);
                           go.ImagePanel(image);
                           return ResponseCodes.OBEX_HTTP_OK;
      This is the code that worked for me before.but it is writing the image to a file everytime. i need an image object.
                     InputStream is = op.openInputStream();
                           StringBuffer buf = new StringBuffer();
                           int data;
                           while(true)
                           {
                           FileOutputStream file = new FileOutputStream("image.jpg");
                           while ((data = is.read()) != -1) {
                                //System.out.println("\ndata "+data);
                               buf.append((char) data);
                               file.write(data);
                               
                           }
                          
                           file.close();
                           go.ImagePanel("image.jpg");
                           return ResponseCodes.OBEX_HTTP_OK;
      Thanks for any help
      martin
        • 1. Re: inputstream to image object
          807591
          InputStream is = op.openInputStream();
                               StringBuffer buf = new StringBuffer();
                               byte[] data= new byte[8];
                               while(true)
                               {
                               Image image = null;
                               while ((is.read(data)) != -1) {
                                    //System.out.println("\ndata "+data);
                                    image=ImageIO.read(new ByteArrayInputStream(data));
                                    System.out.println(image);
                               }
                              System.out.println(image);
                               go.ImagePanel(image);
                               return ResponseCodes.OBEX_HTTP_OK;
          Nm, misunderstood call to InputStream.read()
          • 2. Re: inputstream to image object
            807591
            Nm, misunderstood call to InputStream.read()
            Not sure what you mean.

            An image is coming in on the Inputstream. not sure how big the byte array should be so i put it at 8, the same as a char.

            thanks
            martin
            • 3. Re: inputstream to image object
              807591
              "nm" = nevermind. I had a question for you but answered it myself and edited the post.

              Still looking at your code.
              • 4. Re: inputstream to image object
                807591
                martinnaughton wrote:
                An image is coming in on the Inputstream. not sure how big the byte array should be so i put it at 8, the same as a char.
                A char is 16 bits (2 bytes), your array is 8 bytes (64 bits).
                • 5. Re: inputstream to image object
                  807591
                  Hello,
                  Just got it solved, You can take a inputstream in on Image.IO directly
                                 InputStream is = op.openInputStream();
                                       StringBuffer buf = new StringBuffer();
                                       while(true)
                                       {
                                              Image image =ImageIO.read(is);
                                            System.out.println(image);
                                       }
                                       go.ImagePanel(image);
                                       return ResponseCodes.OBEX_HTTP_OK;
                  Thanks for help newark

                  martin