冰浪

          哥已不再年輕 - 堅定夢想,畢生追求!
          posts - 85, comments - 90, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          J2ME中通過Http協議傳輸圖片

          Posted on 2009-04-09 09:36 冰浪 閱讀(231) 評論(0)  編輯  收藏 所屬分類: J2ME
          其實傳輸圖片和傳輸其他的數據沒有什么區別只是我們選擇怎樣的處理方法,如果我們傳輸java基本數據類型或者String那么比較容易,直接writeInt() readInt()等方法就可以了。如果是傳輸一整個對象比如一個人的信息,那么可以使用序列化把它拆開為按照一定的順序傳輸多個java的基本類型和String。至于圖片顯得要特殊一點,因為它是二進制的文件,Java中的InputStream提供了方法來讀取二進制文件,如果你對此方面的知識不熟悉請參考使用Java操作二進制文件

              在我們聯網的時候同樣還是要在另外一個線程進行,為了提高效率我們使用wait()和notify()來調度線程,線程啟動后會進入wait()的狀態,因為我們在midlet對象上調用了wait()。當用戶按了Connect Command后我們調用midlet.notify()來讓線程繼續運行。
                  if (cmd == connCommand)
                  {
                      synchronized (this)
                      {
                          notify();
                      }
                  } else if (cmd == exitCommand)
                  {
                      exitMIDlet();
                  }

                      synchronized (midlet)
                      {
                          try
                          {
                              midlet.wait();
                          } catch (InterruptedException e)
                          {
                              e.printStackTrace();
                          }
                      }
                      System.out.println("connect to server...");

              當讀取Image文件的時候,我們建立連接后就可以得到InputStream的實例了,接收數據顯得比較重要。我采用的方法是新建一個ByteArrayOutputStream實例baos,然后通過read()得到字節首先寫入到baos里面去。傳輸結束后通過baos.toByteArray()得到Image的字節數據,這樣我們就可以很容易的構建出圖片來了,最后把它顯示在Form里面。
                          httpConn = (HttpConnection) Connector.open(URL);
                          is = httpConn.openInputStream();
                          ByteArrayOutputStream baos = new ByteArrayOutputStream();
                          int ch = 0;
                          while ((ch = is.read()) != -1)
                          {
                              baos.write(ch);
                          }
                          byte[] imageData = baos.toByteArray();
                          Image image = Image.createImage(imageData, 0, imageData.length);
                          midlet.setImage(image);
                          baos.close();
                          is.close();
                          httpConn.close();
              首先你應該在web服務器上放一個大小適中的png格式的圖片,然后運行本程序進行聯網,這樣就可以通過http協議傳輸圖片了。

           

           

           

           

           

           

          下面是源代碼

          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;
          import javax.microedition.lcdui.*;
          import java.io.*;
          import javax.microedition.io.*;

          public class ImageGetter extends MIDlet implements CommandListener
          {

              private Display display;
              public static final Command connCommand = new Command("Connect",
                      Command.ITEM, 1);
              public static final Command exitCommand = new Command("Exit", Command.EXIT,
                      1);
              private Form mainForm;
              private GetterThread gt;

              protected void startApp() throws MIDletStateChangeException
              {

                  display = Display.getDisplay(this);
                  mainForm = new Form("Image Getter");
                  mainForm.append("Click Connect to get Image");
                  mainForm.addCommand(connCommand);
                  mainForm.addCommand(exitCommand);
                  mainForm.setCommandListener(this);
                  display.setCurrent(mainForm);
                  gt = new GetterThread(this);
                  gt.start();

              }

              public void setImage(Image image)
              {

                  mainForm.append(image);
                  display.setCurrent(mainForm);
              }

              protected void pauseApp()
              {

              }

              protected void destroyApp(boolean arg0) throws MIDletStateChangeException
              {

              }

              public void commandAction(Command cmd, Displayable disp)
              {
                  if (cmd == connCommand)
                  {
                      synchronized (this)
                      {
                          notify();
                      }
                  } else if (cmd == exitCommand)
                  {
                      exitMIDlet();
                  }
              }

              private void exitMIDlet()
              {
                  try
                  {
                      destroyApp(false);
                      notifyDestroyed();
                  } catch (MIDletStateChangeException e)
                  {
                      e.printStackTrace();
                  }
              }

              class GetterThread extends Thread
              {
                  private ImageGetter midlet;
                  public static final String URL = "http://localhost/j2medev.png";
                  private HttpConnection httpConn = null;
                  private InputStream is = null;

                  public GetterThread(ImageGetter midlet)
                  {
                      this.midlet = midlet;
                  }

                  public void run()
                  {
                      synchronized (midlet)
                      {
                          try
                          {
                              midlet.wait();
                          } catch (InterruptedException e)
                          {
                              e.printStackTrace();
                          }
                      }
                      System.out.println("connect to server...");
                      try
                      {
                          httpConn = (HttpConnection) Connector.open(URL);
                          is = httpConn.openInputStream();
                          ByteArrayOutputStream baos = new ByteArrayOutputStream();
                          int ch = 0;
                          while ((ch = is.read()) != -1)
                          {
                              baos.write(ch);
                          }
                          byte[] imageData = baos.toByteArray();
                          Image image = Image.createImage(imageData, 0, imageData.length);
                          midlet.setImage(image);
                          baos.close();
                          is.close();
                          httpConn.close();

                      } catch (IOException e)
                      {
                          e.printStackTrace();
                      }

                  }
              }

          }

          (轉于http://azi.javaeye.com/blog/194461)
          主站蜘蛛池模板: 兰坪| 恩平市| 项城市| 海安县| 青冈县| 永丰县| 册亨县| 西安市| 井陉县| 内丘县| 乐陵市| 鹿邑县| 宁远县| 平乐县| 阜南县| 华安县| 包头市| 友谊县| 沁源县| 浦县| 呼玛县| 中江县| 睢宁县| 灵山县| 保德县| 辉县市| 大同市| 海门市| 绵竹市| 新平| 延吉市| 曲水县| 辉南县| 余干县| 永福县| 江陵县| 黄梅县| 辉县市| 长岛县| 富宁县| 罗定市|