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 閱讀(1852) 評論(2)  編輯  收藏

          評論

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

          哪里抄過來的代碼?  回復  更多評論   

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

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


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(110)

          我參與的團隊

          隨筆分類(126)

          隨筆檔案(155)

          文章分類(9)

          文章檔案(19)

          相冊

          搜索

          積分與排名

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 冀州市| 云安县| 太原市| 清流县| 离岛区| 郯城县| 青阳县| 汉沽区| 寻乌县| 共和县| 沂水县| 会同县| 璧山县| 外汇| 泰州市| 台江县| 易门县| 潮安县| 郧西县| 泗水县| 东乌珠穆沁旗| 江津市| 拉孜县| 肥城市| 临西县| 仁化县| 蒙城县| 乌兰浩特市| 鄂伦春自治旗| 海兴县| 荆门市| 林芝县| 淮南市| 石阡县| 朝阳县| 邛崃市| 定结县| 鲜城| 曲沃县| 上林县| 贵定县|