?
Solved

URGENT!! Returning Image Stream

Posted on 2003-02-24
15
Medium Priority
?
279 Views
Last Modified: 2008-03-06
HI,
I am currently working on a project where I am attempting to stream images from a web cam to a mobile phone emulator via a server. I am using J2ME loaded onto the emulator to call the images from the server. Within my code i have a timer which calls a getNextImage() method every second:

class VideoTimer extends TimerTask {
          VideoTimer()
          {
          }
          public void run() {
               try {
                    getNextImage();
               } catch (Exception ex) {
                    System.out.println("Exception " + ex);
               }
          }
     }


     private void getNextImage()
     {    
          labImage = new LabImage1("");
          manager.setCurrent(labImage);



The following is my problem: when the LabImage1 MIDlet (Shown Below) is called by the getNextImage() method I want to make it return the next image in the iamge stream. The web cam when capturing frames creates a sequence of images on the server eg. Image.png, Image1.png, Image2.png, etc. Each time LabImage1 is called I want it to return the most recent image from this sequence. How can i do this in the code below?? Any help would be hugely appreciated.



import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
import javax.microedition.io.*;
import java.io.*;

public class LabImage1 extends Form
{
//     private Form form;
     private ImageItem imageItem;
     private Image image;
     private TextField textField;


     public LabImage1(String str)

     {
          super(str);

          try{    
               image = Image.createImage("/AutoCapture/Image.png");
     
          }
          catch(java.io.IOException ioe)
          {
               System.out.println("Error loading image!!");
          }

          imageItem = new ImageItem("",image,ImageItem.LAYOUT_CENTER,"No Image");
          append(imageItem);

     }
}
0
Comment
Question by:chouli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8007591
 Do you store the images somewhere? If yes just get the last element of your container. Otherwise why not have a static variable that keeps count of the images cerated. Increment this variable by one each tinme and then append it at the end of the image name.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8007602
 Somethign like:

public class LabImage1 extends Form
{
   private static int imageCount = 0;
//     private Form form;
    private ImageItem imageItem;
    private Image image;
    private TextField textField;


    public LabImage1(String str)

    {
         
         super(str);

         try{    
              if (imageCount == 0)
              {
                 image = Image.createImage("/AutoCapture/Image.png");
              }
              else
              {
                 image = Image.createImage("/AutoCapture/Image" + imageCount + ".png");
              }
         }
         catch(java.io.IOException ioe)
         {
              System.out.println("Error loading image!!");
         }

         imageItem = new ImageItem("",image,ImageItem.LAYOUT_CENTER,"No Image");
         append(imageItem);
         imageCount++;
    }
}
0
 

Author Comment

by:chouli
ID: 8008943
Thanks for your reply girionis. That looks good but theres just one problem. Because I'm calling the LabImage1 from another MIDlet the imagecount seems to be set to 0 each time its called and so the image doesn't actually strem/refresh on the screen. How would I ensure that the imagecount incrments each time?
I do store the images in a folder on the server as you mentioned. How would I get the last element of the container?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 8009240
> . Because I'm calling the LabImage1 from another MIDlet the imagecount seems to be set to 0 each time its called and so the image doesn't actually strem/refresh on the screen.

  This shouldn't matter since the "imageCount" variable is static and therefore it shouldn't be set to 0 every time you create a new LabImage1. Are you sure you have declared the variable to be static?

  The only way I see for the "imageCount" to be set to zero is if there is a conflict on the classloaders and the LabImage1 is loaded by two different classloaders.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8009275
 More thought into this... You said they are loaded from another MIDlet. Are they loaded from the same codebase? I am not sure if MIDlets work the same way as Applets, but with Applets you can only use static variables if they are loaded (the Applets) from the same classloader. There is a classloader per codebase. If this is the case with MIDlets you will not be able to use static variables.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8009299
 The second thing you can do is to initalize the variable in the class VideoTimer and pass it in the LabImage1 everytime:

class VideoTimer extends TimerTask {
   private static int imageCount = 0;
         VideoTimer()
         {
         }
         public void run() {
              try {
                   getNextImage();
              } catch (Exception ex) {
                   System.out.println("Exception " + ex);
              }
         }
    }


    private void getNextImage()
    {    
         labImage = new LabImage1("", imageCount);
         manager.setCurrent(labImage);
         imageCount++;
    }
}


public class LabImage1 extends Form
{
//     private Form form;
   private ImageItem imageItem;
   private Image image;
   private TextField textField;


   public LabImage1(String str, int imageCount)
   {
        super(str);

        try{    
             if (imageCount == 0)
             {
                image = Image.createImage("/AutoCapture/Image.png");
             }
             else
             {
                image = Image.createImage("/AutoCapture/Image" + imageCount + ".png");
             }
        }
        catch(java.io.IOException ioe)
        {
             System.out.println("Error loading image!!");
        }

        imageItem = new ImageItem("",image,ImageItem.LAYOUT_CENTER,"No Image");
        append(imageItem);
        imageCount++;
   }
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 8009321
 Sorry, take this line

> imageCount++;

  out of the LabImage1 class.
0
 

Author Comment

by:chouli
ID: 8009338
I'm not sure if the two MIDlets are loaded from the same codebase. They are packaged together in a project and they are then compiled together. Doesn't the LabImage1 midlet initialises the imagecount variable to 0 each time it runs? This would then mean that only the first image is displayed each time. This seems to be the case at the moment anyway. I have declared the imagecount variable to be static. My code is exactly the same as the example you gave above.
Would it be possible for me to retrieve the last image saved each time the MIDlet runs as you mentioned above? Do you know how i might do this??
0
 

Author Comment

by:chouli
ID: 8009359
I'm not sure if the two MIDlets are loaded from the same codebase. They are packaged together in a project and they are then compiled together. Doesn't the LabImage1 midlet initialises the imagecount variable to 0 each time it runs? This would then mean that only the first image is displayed each time. This seems to be the case at the moment anyway. I have declared the imagecount variable to be static. My code is exactly the same as the example you gave above.
Would it be possible for me to retrieve the last image saved each time the MIDlet runs as you mentioned above? Do you know how i might do this??
0
 

Author Comment

by:chouli
ID: 8009369
I'm not sure if the two MIDlets are loaded from the same codebase. They are packaged together in a project and they are then compiled together. Doesn't the LabImage1 midlet initialises the imagecount variable to 0 each time it runs? This would then mean that only the first image is displayed each time. This seems to be the case at the moment anyway. I have declared the imagecount variable to be static. My code is exactly the same as the example you gave above.
Would it be possible for me to retrieve the last image saved each time the MIDlet runs as you mentioned above? Do you know how i might do this??
0
 
LVL 35

Expert Comment

by:girionis
ID: 8009608
 If MIDlets are not in the same codebase and they work like Applets then they probably are loaded by different classloaders and thus the static variable is not static anymore. However, as I told you above, I do not have much of experience with MIDlets so you might want to do some more research on this.

  However if they are on the same codebase then this is *very strange* behaviour since the variable is static and we increment it at the end of the LabImage1 constructor. It does not matter how many times you do a "new" statement, the static variable should be incremented each time and not reset to zero.

  Have you also tried my second recommendation?
0
 
LVL 35

Accepted Solution

by:
girionis earned 1100 total points
ID: 8009635
> Would it be possible for me to retrieve the last image saved each time the MIDlet runs as you mentioned above? Do you know how i might do this??

  Yes but you would need to communicate with the server, load all files in memory and then do a comparison and get the last one. Coudl be very expensive for massive amounts of files.
0
 

Expert Comment

by:CleanupPing
ID: 9059117
chouli:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9671284
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept girionis' comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question