Collect input stream as jpg file.

Discussion in 'Java' started by Boki, Oct 25, 2005.

  1. Boki

    Boki Guest

    Hi All,
    My last step, collect the input steam ( data ) to a jpg file,
    could you please help to finish it ? thank you very much!

    //--------------------
    ....
    public InputStream in;
    private StreamConnection con = null;
    in = con.openInputStream();
    ....
    //------------------
    ....
    // collect input data to jpg file


    try{

    int bytesToRead = in.available();
    if (bytesToRead > 0) {
    // Initialize buffer
    byte[] byteBuffer = new byte[bytesToRead];
    // Read bytes
    int nbrOfBytesRead = in.read(byteBuffer);
    String str = new String(byteBuffer);
    }
    }
    ------------------
    Boki, Oct 25, 2005
    #1
    1. Advertising

  2. Boki

    Boki Guest

    For this line:
    in.read(byteBuffer);

    It seems that the everytime I got data of inputStream, the in.read(xxx) will
    overwrite my array data, and it seems that I can't use pointer
    byteBuffer[pData++], right?

    Could you please help?
    Thank you very much!

    Best regards,
    Boki.

    "Boki" <> ¼¶¼g©ó¶l¥ó·s»D:djkk0c$s05$...
    > Hi All,
    > My last step, collect the input steam ( data ) to a jpg file,
    > could you please help to finish it ? thank you very much!
    >
    > //--------------------
    > ...
    > public InputStream in;
    > private StreamConnection con = null;
    > in = con.openInputStream();
    > ...
    > //------------------
    > ...
    > // collect input data to jpg file
    >
    >
    > try{
    >
    > int bytesToRead = in.available();
    > if (bytesToRead > 0) {
    > // Initialize buffer
    > byte[] byteBuffer = new byte[bytesToRead];
    > // Read bytes
    > int nbrOfBytesRead = in.read(byteBuffer);
    > String str = new String(byteBuffer);
    > }
    > }
    > ------------------
    >
    >
    Boki, Oct 25, 2005
    #2
    1. Advertising

  3. Boki

    Boki Guest

    any better way ?
    --------------
    try{
    int count_jpg=0;
    int bytesToRead = in.available();
    if (bytesToRead > 0) {
    // Initialize buffer
    byte[] byteBuffer = new byte[bytesToRead];
    // Read bytes
    int nbrOfBytesRead = in.read(byteBuffer);

    GUI.ggg+=bytesToRead;
    gui.repaint();

    for (count_jpg=0;count_jpg<bytesToRead;count_jpg++)
    imageData[GUI.ggg+count_jpg]=byteBuffer[count_jpg];
    }
    }
    "Boki" <> ¼¶¼g©ó¶l¥ó·s»D:djkkdr$bjk$...
    > For this line:
    > in.read(byteBuffer);
    >
    > It seems that the everytime I got data of inputStream, the in.read(xxx)
    > will overwrite my array data, and it seems that I can't use pointer
    > byteBuffer[pData++], right?
    >
    > Could you please help?
    > Thank you very much!
    >
    > Best regards,
    > Boki.
    >
    > "Boki" <> ¼¶¼g©ó¶l¥ó·s»D:djkk0c$s05$...
    >> Hi All,
    >> My last step, collect the input steam ( data ) to a jpg file,
    >> could you please help to finish it ? thank you very much!
    >>
    >> //--------------------
    >> ...
    >> public InputStream in;
    >> private StreamConnection con = null;
    >> in = con.openInputStream();
    >> ...
    >> //------------------
    >> ...
    >> // collect input data to jpg file
    >>
    >>
    >> try{
    >>
    >> int bytesToRead = in.available();
    >> if (bytesToRead > 0) {
    >> // Initialize buffer
    >> byte[] byteBuffer = new byte[bytesToRead];
    >> // Read bytes
    >> int nbrOfBytesRead = in.read(byteBuffer);
    >> String str = new String(byteBuffer);
    >> }
    >> }
    >> ------------------
    >>
    >>

    >
    >
    Boki, Oct 25, 2005
    #3
  4. Boki

    Boki Guest

    whatever, I have done the protype, I am trying the performance :D

    btw, how to capture the webcam jpg data on PC ??

    do I have to consider USB driver ?

    or How can I get the JPG image directly?

    Thank you very mcuh!

    :D

    Best regards,
    Boki.


    "Boki" <> ¼¶¼g©ó¶l¥ó·s»D:djklhg$h94$...
    > any better way ?
    > --------------
    > try{
    > int count_jpg=0;
    > int bytesToRead = in.available();
    > if (bytesToRead > 0) {
    > // Initialize buffer
    > byte[] byteBuffer = new byte[bytesToRead];
    > // Read bytes
    > int nbrOfBytesRead = in.read(byteBuffer);
    >
    > GUI.ggg+=bytesToRead;
    > gui.repaint();
    >
    > for (count_jpg=0;count_jpg<bytesToRead;count_jpg++)
    > imageData[GUI.ggg+count_jpg]=byteBuffer[count_jpg];
    > }
    > }
    > "Boki" <> ¼¶¼g©ó¶l¥ó·s»D:djkkdr$bjk$...
    >> For this line:
    >> in.read(byteBuffer);
    >>
    >> It seems that the everytime I got data of inputStream, the in.read(xxx)
    >> will overwrite my array data, and it seems that I can't use pointer
    >> byteBuffer[pData++], right?
    >>
    >> Could you please help?
    >> Thank you very much!
    >>
    >> Best regards,
    >> Boki.
    >>
    >> "Boki" <> ¼¶¼g©ó¶l¥ó·s»D:djkk0c$s05$...
    >>> Hi All,
    >>> My last step, collect the input steam ( data ) to a jpg file,
    >>> could you please help to finish it ? thank you very much!
    >>>
    >>> //--------------------
    >>> ...
    >>> public InputStream in;
    >>> private StreamConnection con = null;
    >>> in = con.openInputStream();
    >>> ...
    >>> //------------------
    >>> ...
    >>> // collect input data to jpg file
    >>>
    >>>
    >>> try{
    >>>
    >>> int bytesToRead = in.available();
    >>> if (bytesToRead > 0) {
    >>> // Initialize buffer
    >>> byte[] byteBuffer = new byte[bytesToRead];
    >>> // Read bytes
    >>> int nbrOfBytesRead = in.read(byteBuffer);
    >>> String str = new String(byteBuffer);
    >>> }
    >>> }
    >>> ------------------
    >>>
    >>>

    >>
    >>

    >
    >
    Boki, Oct 25, 2005
    #4
  5. Boki

    Roedy Green Guest

    On Tue, 25 Oct 2005 14:40:12 +0800, "Boki" <>
    wrote, quoted or indirectly quoted someone who said :

    > String str = new String(byteBuffer);


    You don't want a string. This has nothing to do with human-readable
    text. Have a look at the ImageIO class.

    see http://mindprod.com/jgloss/imageio.html

    It will let you read from a file on an InputStream.

    If you have to make this work on an old JVM you can do it with:

    Image image = toolkit.createImage( rawImage );
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 25, 2005
    #5
  6. Boki

    Roedy Green Guest

    On Tue, 25 Oct 2005 14:47:23 +0800, "Boki" <>
    wrote, quoted or indirectly quoted someone who said :

    >It seems that the everytime I got data of inputStream, the in.read(xxx) will
    >overwrite my array data, and it seems that I can't use pointer
    >byteBuffer[pData++], right?


    see http://mindprod.com/jgloss/readblocking.html
    for how to do that. However, you don't need to. ImageIO will do it for
    you.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 25, 2005
    #6
  7. Boki

    Roedy Green Guest

    On Tue, 25 Oct 2005 15:52:05 +0800, "Boki" <>
    wrote, quoted or indirectly quoted someone who said :

    >btw, how to capture the webcam jpg data on PC ??


    import javax.imageio.ImageIO;
    import javax.media.CaptureDeviceInfo;
    import javax.media.CaptureDeviceManager;
    import javax.media.ControllerAdapter;
    import javax.media.ControllerClosedEvent;
    import javax.media.ControllerListener;
    import javax.media.Format;
    import javax.media.Manager;
    import javax.media.MediaLocator;
    import javax.media.Player;
    import javax.media.RealizeCompleteEvent;
    import javax.media.StartEvent;
    import javax.media.StopEvent;
    import javax.media.control.FormatControl;
    import javax.media.control.FrameGrabbingControl;
    import javax.media.format.RGBFormat;
    import javax.media.format.VideoFormat;
    import javax.media.format.YUVFormat;
    import javax.media.util.BufferToImage;

    I would tackle something easier for now.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 25, 2005
    #7
  8. Boki

    Boki Guest

    Dear Green,
    any example :D?

    Best regards,
    Boki.

    "Roedy Green" <>
    ???????:...
    > On Tue, 25 Oct 2005 15:52:05 +0800, "Boki" <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >>btw, how to capture the webcam jpg data on PC ??

    >
    > import javax.imageio.ImageIO;
    > import javax.media.CaptureDeviceInfo;
    > import javax.media.CaptureDeviceManager;
    > import javax.media.ControllerAdapter;
    > import javax.media.ControllerClosedEvent;
    > import javax.media.ControllerListener;
    > import javax.media.Format;
    > import javax.media.Manager;
    > import javax.media.MediaLocator;
    > import javax.media.Player;
    > import javax.media.RealizeCompleteEvent;
    > import javax.media.StartEvent;
    > import javax.media.StopEvent;
    > import javax.media.control.FormatControl;
    > import javax.media.control.FrameGrabbingControl;
    > import javax.media.format.RGBFormat;
    > import javax.media.format.VideoFormat;
    > import javax.media.format.YUVFormat;
    > import javax.media.util.BufferToImage;
    >
    > I would tackle something easier for now.
    > --
    > Canadian Mind Products, Roedy Green.
    > http://mindprod.com Again taking new Java programming contracts.
    Boki, Oct 25, 2005
    #8
  9. Boki

    Boki Guest

    This is a good example, does it possible to receive unknow length, and auto
    stop when time out?

    I can't image that code stop running ~~ @@ ( that makes me can't implement
    time out when I polling data... )


    "Roedy Green" <>
    ???????:...
    > On Tue, 25 Oct 2005 14:47:23 +0800, "Boki" <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >>It seems that the everytime I got data of inputStream, the in.read(xxx)
    >>will
    >>overwrite my array data, and it seems that I can't use pointer
    >>byteBuffer[pData++], right?

    >
    > see http://mindprod.com/jgloss/readblocking.html
    > for how to do that. However, you don't need to. ImageIO will do it for
    > you.
    > --
    > Canadian Mind Products, Roedy Green.
    > http://mindprod.com Again taking new Java programming contracts.
    Boki, Oct 25, 2005
    #9
  10. Boki

    Boki Guest

    You are right, I don't need that.

    I don't know J2ME can use imageIO or not, I will try tomorrow :D thanks a
    lot.


    "Roedy Green" <>
    ???????:...
    > On Tue, 25 Oct 2005 14:40:12 +0800, "Boki" <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >> String str = new String(byteBuffer);

    >
    > You don't want a string. This has nothing to do with human-readable
    > text. Have a look at the ImageIO class.
    >
    > see http://mindprod.com/jgloss/imageio.html
    >
    > It will let you read from a file on an InputStream.
    >
    > If you have to make this work on an old JVM you can do it with:
    >
    > Image image = toolkit.createImage( rawImage );
    > --
    > Canadian Mind Products, Roedy Green.
    > http://mindprod.com Again taking new Java programming contracts.
    Boki, Oct 25, 2005
    #10
  11. Boki

    Roedy Green Guest

    On Wed, 26 Oct 2005 00:44:33 +0800, "Boki" <>
    wrote, quoted or indirectly quoted someone who said :

    >Dear Green,
    > any example :D?


    Sorry,. I am not up for it. It is very tricky.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Oct 26, 2005
    #11
  12. Boki

    Roedy Green Guest

    On Wed, 26 Oct 2005 00:46:06 +0800, "Boki" <>
    wrote, quoted or indirectly quoted someone who said :

    >This is a good example, does it possible to receive unknow length, and auto
    >stop when time out?


    that would give you random crap and be infuriatingly slow.

    For sockets you put a length on the front.

    For HTTP, if it won't give you a length, see
    http://mindprod.com/jgloss/readblocking.html
    http://mindprod.com/jgloss/readeverything.html
    http://mindprod.com/applets/fileio.html (HTTP GET)

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Oct 26, 2005
    #12
  13. Boki

    Roedy Green Guest

    On Wed, 26 Oct 2005 00:46:54 +0800, "Boki" <>
    wrote, quoted or indirectly quoted someone who said :

    >I don't know J2ME can use imageIO or not, I will try tomorrow :D thanks a
    >lot.

    It will need some sort of method for taking a JPG image as a stream
    of a byte[].
    You know how to convert byte[] to stream and back, right? If not see
    http://mindprod.com/applets/fileio.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Oct 26, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roland
    Replies:
    0
    Views:
    1,055
    Roland
    May 12, 2004
  2. Matthias
    Replies:
    5
    Views:
    423
    Whitecrest
    Jun 1, 2004
  3. Replies:
    9
    Views:
    631
    Alex Buell
    Apr 27, 2006
  4. Yannick

    ASP changes Big JPG -> Small JPG

    Yannick, Feb 2, 2004, in forum: ASP General
    Replies:
    6
    Views:
    150
    Tim Williams
    Feb 3, 2004
  5. Vilmar Brazão de Oliveira
    Replies:
    14
    Views:
    345
    Aaron Bertrand [MVP]
    Mar 5, 2004
Loading...

Share This Page