David.Turing's blog

           

          Java讀取BMP文件

          各位老大,幫我看看代碼

          ???????? // ?The?DataInputStream?allows?you?to?read?in?16?and?32?bit?numbers
          ????????DataInputStream?in? = ? new ?DataInputStream(instream);
          ????????DataOutputStream?out
          = new ?DataOutputStream(outstream);

          ????????
          // ?Verify?that?the?header?starts?with?'BM'
          ???????? if ?(in.read()? != ? ' B ' )? {
          ????????????
          throw ? new ?IOException( " Not?a?.BMP?file " );
          ????????}

          ????????out.write(
          ' B ' );
          ????????
          ????????
          if ?(in.read()? != ? ' M ' )? {
          ????????????
          throw ? new ?IOException( " Not?a?.BMP?file " );
          ????????}

          ????????out.write(
          ' M ' );
          ????????
          ????????
          // ?????Get?the?total?file?size
          ???????? int ?fileSize? = ?intelInt(in.readInt());
          ????????out.writeInt(intelInt(fileSize));

          ????????
          // ?????Skip?the?2?16-bit?reserved?words
          ????????out.writeShort(in.readUnsignedShort());
          ????????out.writeShort(in.readUnsignedShort());

          ????????
          int ?bitmapOffset? = ?intelInt(in.readInt());
          ????????out.writeInt(intelInt(bitmapOffset));
          ????????
          int ?bitmapInfoSize? = ?intelInt(in.readInt());
          ????????out.writeInt(intelInt(bitmapInfoSize));
          ????????
          ????????
          int ?width? = ?intelInt(in.readInt());
          ????????out.writeInt(intelInt(width));
          ????????
          int ?height? = ?intelInt(in.readInt());
          ????????out.writeInt(intelInt(height));
          ????????
          // ?Skip?the?16-bit?bitplane?size
          ????????out.writeShort(in.readUnsignedShort());
          ????????
          int ?bitCount? = ?intelShort(in.readUnsignedShort());
          ????????out.writeShort(intelShort(bitCount));
          ????????
          int ?compressionType? = ?intelInt(in.readInt());
          ????????out.writeInt(intelInt(compressionType));
          ????????
          int ?imageSize? = ?intelInt(in.readInt());
          ????????out.writeInt(intelInt(imageSize));
          // ?Skip?pixels?per?meter
          ????????out.writeInt(in.readInt());
          ????????out.writeInt(in.readInt());

          ????????
          int ?colorsUsed? = ?intelInt(in.readInt());
          ????????out.writeInt(intelInt(colorsUsed));
          ????????
          int ?colorsImportant? = ?intelInt(in.readInt());
          ????????out.writeInt(intelInt(colorsImportant));
          ????????System.out.println(
          " [colorused]= " + colorsUsed);

          // ?Read?the?pixels?from?the?stream?based?on?the?compression?type
          ???????? if ?(compressionType? == ?BI_RGB)? {
          ????????????
          if ?(bitCount? == ? 24 )? {
          ????????????????readRGB24(width,?height,?in,?out);
          ????????????}

          ????????}
          ?
          ????}


          ???? protected ? static ? void ?readRGB24( int ?width,? int ?height,
          ????????DataInputStream?in,?OutputStream?out)
          ????
          throws ?IOException
          ????
          {

          // ?Start?storing?at?the?bottom?of?the?array
          ???????? for ?( int ?h? = ?height - 1 ;?h? >= ? 0 ;?h -- )? {
          ?????????
          for ?( int ?w? = ? 0 ;?w? < ?width;?w ++ )? {

          // ?Read?in?the?red,?green,?and?blue?components
          ?????????? int ?red? = ?in.read();
          ??????????
          int ?green? = ?in.read();
          ??????????
          int ?blue? = ?in.read();
          ??????????out.write(red);
          ??????????out.write(green);
          ??????????out.write(blue);
          ????????????}

          ????????}

          ????}

          posted on 2006-06-06 10:29 david.turing 閱讀(1845) 評論(2)  編輯  收藏

          評論

          # re: Java讀取BMP文件 2007-11-07 17:45 zouni

          哪里抄過來的代碼?  回復(fù)  更多評論   

          # re: Java讀取BMP文件 2007-11-07 17:52 david.turing

          我暈,沒有publish到blog的文章也能google到?  回復(fù)  更多評論   


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(110)

          我參與的團(tuán)隊(duì)

          隨筆分類(126)

          隨筆檔案(155)

          文章分類(9)

          文章檔案(19)

          相冊

          搜索

          積分與排名

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 襄樊市| 武冈市| 鸡泽县| 梅州市| 莲花县| 屏东县| 休宁县| 玛沁县| 东乌珠穆沁旗| 梅州市| 葵青区| 呼玛县| 洪江市| 华亭县| 江城| 伊春市| 宜兴市| 长子县| 天津市| 阿巴嘎旗| 翁牛特旗| 成安县| 柞水县| 高州市| 鹤壁市| 石景山区| 崇信县| 安阳市| 平遥县| 尖扎县| 平顶山市| 彰武县| 黔江区| 枣强县| 隆昌县| 昌平区| 宁阳县| 龙州县| 荃湾区| 宣武区| 灵寿县|