Java读取BMP文件

各位老大,帮我看看代码

None.gif         //  The DataInputStream allows you to read in 16 and 32 bit numbers
None.gif
        DataInputStream in  =   new  DataInputStream(instream);
None.gif        DataOutputStream out
= new  DataOutputStream(outstream);
None.gif
None.gif        
//  Verify that the header starts with 'BM'
ExpandedBlockStart.gifContractedBlock.gif
         if  (in.read()  !=   ' B ' dot.gif {
InBlock.gif            
throw   new  IOException( " Not a .BMP file " );
ExpandedBlockEnd.gif        }

None.gif        out.write(
' B ' );
None.gif        
ExpandedBlockStart.gifContractedBlock.gif        
if  (in.read()  !=   ' M ' dot.gif {
InBlock.gif            
throw   new  IOException( " Not a .BMP file " );
ExpandedBlockEnd.gif        }

None.gif        out.write(
' M ' );
None.gif        
None.gif        
//      Get the total file size
None.gif
         int  fileSize  =  intelInt(in.readInt());
None.gif        out.writeInt(intelInt(fileSize));
None.gif
None.gif        
//      Skip the 2 16-bit reserved words
None.gif
        out.writeShort(in.readUnsignedShort());
None.gif        out.writeShort(in.readUnsignedShort());
None.gif
None.gif        
int  bitmapOffset  =  intelInt(in.readInt());
None.gif        out.writeInt(intelInt(bitmapOffset));
None.gif        
int  bitmapInfoSize  =  intelInt(in.readInt());
None.gif        out.writeInt(intelInt(bitmapInfoSize));
None.gif        
None.gif        
int  width  =  intelInt(in.readInt());
None.gif        out.writeInt(intelInt(width));
None.gif        
int  height  =  intelInt(in.readInt());
None.gif        out.writeInt(intelInt(height));
None.gif        
None.gif
//  Skip the 16-bit bitplane size
None.gif
        out.writeShort(in.readUnsignedShort());
None.gif        
int  bitCount  =  intelShort(in.readUnsignedShort());
None.gif        out.writeShort(intelShort(bitCount));
None.gif        
int  compressionType  =  intelInt(in.readInt());
None.gif        out.writeInt(intelInt(compressionType));
None.gif        
int  imageSize  =  intelInt(in.readInt());
None.gif        out.writeInt(intelInt(imageSize));
None.gif
//  Skip pixels per meter
None.gif
        out.writeInt(in.readInt());
None.gif        out.writeInt(in.readInt());
None.gif
None.gif        
int  colorsUsed  =  intelInt(in.readInt());
None.gif        out.writeInt(intelInt(colorsUsed));
None.gif        
int  colorsImportant  =  intelInt(in.readInt());
None.gif        out.writeInt(intelInt(colorsImportant));
None.gif        System.out.println(
" [colorused]= " + colorsUsed);
None.gif
None.gif
//  Read the pixels from the stream based on the compression type
ExpandedBlockStart.gifContractedBlock.gif
         if  (compressionType  ==  BI_RGB)  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if  (bitCount  ==   24 dot.gif {
InBlock.gif                readRGB24(width, height, in, out);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }
 
None.gif    }


None.gif     protected   static   void  readRGB24( int  width,  int  height,
None.gif        DataInputStream in, OutputStream out)
None.gif    
throws  IOException
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif
InBlock.gif
//  Start storing at the bottom of the array
ExpandedSubBlockStart.gifContractedSubBlock.gif
         for  ( int  h  =  height - 1 ; h  >=   0 ; h -- dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif         
for  ( int  w  =   0 ; w  <  width; w ++ dot.gif {
InBlock.gif
InBlock.gif
//  Read in the red, green, and blue components
InBlock.gif
           int  red  =  in.read();
InBlock.gif          
int  green  =  in.read();
InBlock.gif          
int  blue  =  in.read();
InBlock.gif          out.write(red);
InBlock.gif          out.write(green);
InBlock.gif          out.write(blue);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值