如何在连接运行时重新绘制黑莓的部分屏幕?

时间:2010-11-12 10:09:22

标签: multithreading blackberry repaint

我有两个问题。

第一个是关于更新UI,第二个是当我尝试连接到相机以获取mjpeg流并运行getResponseCode()时,应用程序会锁定。 MDS显示了大量数据传输。

我有一些像......这样的课程:

Http extends Thread {
  public abstract String getUrl();
  public abstract String getBase64Encode();
  public abstract void onReturn(int responseCode, InputStream is,int lenght);
  protected abstract void onError(Exception e);
}

CameraHttp扩展了Http,MjpegHttp扩展了CameraHttp。

http连接到jpeg或mjpeg相机地址的URL。

我有一个相机类。它启动与重写方法mjpegconnection.go()的连接; 我在ViewCam屏幕上也有一个静态位图,它扩展了MainScreen。

开始后:

url = getUrl();
queryString = encodeURL(queryString);
byte postmsg[] = queryString.getBytes("UTF-8");
httpConnection = (HttpConnection) Connector.open(url
    + ";deviceside=false", Connector.READ_WRITE);
httpConnection.setRequestMethod(HttpConnection.GET);
httpConnection.setRequestProperty("Authorization", getBase64Encode());
os = httpConnection.openDataOutputStream(); 

for (int i = 0; i < postmsg.length; i++) {
    os.write(postmsg[i]);
}
{
     if (!cancel) {
         System.out.println(httpConnection.getURL()+ 
             " *****"+httpConnection.getPort());
         System.out.println("onreturn oncesi"
             + httpConnection.getResponseCode());
         onReturn(httpConnection.getResponseCode(), httpConnection
             .openInputStream(),(int) httpConnection.getLength());

         System.out.println("onreturn sornrası");
     }
     os.close();
     httpConnection.close();
}
} catch (Exception e) {
    System.out.println("hata " + e.getMessage());
    try {
        httpConnection.close();
        Thread.sleep(60);
    } catch (Exception ie) {
}
onError(e);
}

dosomething之后

 // decides mjpeg-jpeg stream
 // if it is mjpeg, direct to parser,
 // else it sets image with setImage() and return to connection with go();
 public void parse(InputStream is, int lenght) {
     try {
         if (!type.isMjpegStream()) {
             setImage(is, lenght);
             System.gc();
             StaticVar.ActiveCam.setConnected(true);
         } else { 
             if (parser == null) {
             parser = new JpegParser(is, this);
         } else {
             parser.setInputSteam(is, this);
         }
         parser.parse();
         is.close();
     }
     } catch (Exception e) {
     } 
}

 public void setImage(InputStream is, int lenght) {
     byte[] raw = new byte[lenght];
     try {
         is.read(raw);
         currentImage = Bitmap.createBitmapFromBytes(raw, 0, raw.length, 1);
         ViewCam.ViewCam=currentImage;       //static var.
     } catch (IOException e) {
         System.out.println("catche***********");
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
 }

如何重新绘制屏幕以显示位图?

我的ViewCam

public class ViewCam extends MainScreen {
Header header;
String headerString;
public static Bitmap ViewCam;// cam image shows
private static Thread My;// runs connection

void OnStart() {
    My = new Thread() {
        public void run() {
            System.out.println("ONSTART");
            StaticVar.ActiveCam.go();
        };
    };
    My.start();
    Bitmap bitmap = Bitmap.getBitmapResource("res/main.png");
    Bitmap bmp2 = ResizeImage.resizeBitmap(bitmap, Display.getWidth(),
            Display.getHeight());
    Background bg = BackgroundFactory.createBitmapBackground(bmp2);
    this.setBackground(bg);
    this.getMainManager().setBackground(bg);


}


public ViewCam() {
    StaticVar.ActiveCam.getIp();

    OnStart();

    headerString ="Cam View";
    header = new Header("res/bartop.png", headerString, 0);
    add(header);

    ViewCam = Bitmap.getBitmapResource("res/spexco_splash.png");
    ViewCam = ResizeImage.bestFit(ViewCam, Display.getWidth(), Display
            .getHeight());
    BitmapField bf = new BitmapField(ViewCam);
    add(bf);
}
}

1 个答案:

答案 0 :(得分:1)

尝试Screen.invalidate()

public void invalidate(int x,int y,int width,int height)

使此屏幕的区域无效。
此方法将此屏幕的某个区域标记为需要重新绘制。重新绘制稍后由主事件派发线程处理。

注意:任何线程都可以安全地调用此方法,并且不需要在事件锁定上进行同步。

覆盖:
在班级经理中失效 参数:
x - ContentRect坐标中区域的左边缘 y - ContentRect坐标中区域的上边缘 width - 区域的宽度(以像素为单位) height - 区域的高度(以像素为单位)。