posts - 1,  comments - 25,  trackbacks - 0

          下面就給出具體代碼,與以往一樣,我會在代碼的適當位置使用注釋。但我不會保證在我機器上可以運行的程序會在所有機器上運行(據說這可能是人品問題)。

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

          public class ImageCanvas
              extends Canvas
          {
            private Image byteImg;
            private InputStream input;
            public ImageCanvas()
            {
              try
              {
                jbInit();
              }
              catch (Exception e)
              {
                e.printStackTrace();
              }
            }

            /**
             * 三種方法任選一種
             * @throws Exception
             */
            private void jbInit() throws Exception
            {
              byteImg = this.readImage("/res/pic.bin", 11110); // 從一個合成包里讀取資源,如*.bin文件
              //byteImg = Image.createImage("/res/caidan.png"); // 直接讀取PNG文件
              //byteImg = this.importImage(this.caiDanImage()); // 從byte數組里記取資源,將資源像素保存在byte數組中
              System.out.println("內存情況: " +
                                 Runtime.getRuntime().freeMemory() +
                                 " / " + Runtime.getRuntime().totalMemory());//這種情況下會簡單看下每種方法對內存的影響
              //this.getByte("/res/caidan.png");
            }

            /**
             * Reads a file from the BIN file and return data as an Image
             * @param binfile String 文件名
             * @param pos long 需要跳過的字節長度
             * @return Image 返回一個Image對像
             */
            public Image readImage(String binfile, long pos)
            {
              byte buffer[];
              int len;
              try
              {
                InputStream is = getClass().getResourceAsStream(binfile);
                is.skip(pos);
                len = (is.read() & 0xFF) << 24; // 如果不懂得這幾句的意思,請留言給我,或是補習一下基本的編程知識
                len |= (is.read() & 0xFF) << 16;
                len |= (is.read() & 0xFF) << 8;
                len |= (is.read() & 0xFF);
                buffer = new byte[len];
                is.read(buffer, 0, buffer.length);
                is.close();
                is = null;
                System.gc();
              }
              catch (Exception e)
              {
                buffer = null;
                e.printStackTrace();
                System.gc();
                return null;
              }
              return Image.createImage(buffer, 0, buffer.length);
            }

            /**
             * 將資源文件轉換為byte數組
             * @param file String 需要轉換的文件名
             */
            private void getByte(String file)
            {
              byte[] myData = null;
              input = getClass().getResourceAsStream(file);
              try
              {
                ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
                int ch = 0;
                while ((ch = input.read()) != -1)
                {
                  byteArray.write(ch);
                }
                for (int i = 0; i < byteArray.size(); i++)
                {
                  myData = byteArray.toByteArray();
                }
                for (int i = 0; i < myData.length; i++)
                {
                  System.out.println(myData[i] + ",");
                }
              }
              catch (Exception e)
              {}
              //return myData;
            }

            /**
             * 利用byte數據流生成圖片
             * @param byteFile byte[] 圖片文件byte數據流
             * @return Image 返回Image對象
             */
            private Image importImage(byte[] byteFile)
            {
              Image img = null;
              img = Image.createImage(byteFile, 0, byteFile.length);
              return img;
            }

            protected void paint(Graphics g)
            {
              /** @todo Add paint codes */
              g.setColor(0xffffff);
              g.fillRect(0, 0, this.getWidth(), this.getHeight());
              g.drawImage(byteImg, 30, 30, g.TOP | g.LEFT);
            }

            private byte[] xiangSuImage()
            {
              byte[] iCaiDanImage =
                  { -119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0,
                  13, 73, 72, 68, 82, 0, 0, 0, 65, 0, 0, 0, 56,
                  ......//未完的數據
                  };
              return iCaiDanImage;
            }

          }

          總體來

          posted on 2011-05-19 08:37 Daniel 閱讀(401) 評論(0)  編輯  收藏 所屬分類: CoreJava
          Byte[]-->Image" trackback:ping="http://www.aygfsteel.com/mlzry0612/services/trackbacks/350562.aspx" /> -->
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(3)

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 尤溪县| 五大连池市| 济源市| 东辽县| 宝清县| 吉水县| 堆龙德庆县| 青田县| 广宁县| 临湘市| 隆回县| 新乡县| 丹寨县| 鹤壁市| 沈阳市| 股票| 社会| 丹江口市| 兴山县| 长兴县| 宝鸡市| 昌乐县| 新平| 会昌县| 彰化市| 会同县| 青龙| 双牌县| 清水县| 武夷山市| 宣威市| 深水埗区| 启东市| 建始县| 崇礼县| 健康| 广西| 巴彦淖尔市| 南岸区| 荆门市| 瑞昌市|