谢谢两位
请告诉我大概的方法,譬如用到什么类库和API
当然,附源程序更好
mail to :[email protected]
谢了先

解决方案 »

  1.   

    ava 技巧 43:如何在 Java 应用程序中读取 8 位和 24 位 Microsoft Windows 位图
    在 Java 应用程序中加载位图文件的逐步指南作者:Jeff West 和 John D. Mitchell
    摘要
    目前,标准的 getImage() 方法仅支持 GIF 和 JPEG 图像。尽管存在用于读取 PNG(可移植网络图形)格式的 Java 例程,但我们还没听说过有用于读取 Microsoft Windows 位图图像的阅读程序。Jeff West 撰写的这篇技巧提供了加载 Windows 位图图像的代码。Java 的当前发行版并不正式支持在 Java 应用程序中读取 Microsoft Windows 位图文件。但别担心,我们有办法解决这个问题!这篇技巧将说明如何完成这一任务 -- 我们首先说明读取 Microsoft Windows 文件格式的基本步骤。Windows DIB(设备独立的位图)文件格式比较简单。与纯位图格式不同,DIB 格式保留着用于在内存中存储图像的明确信息。问题是图像格式的变体如此之多(1 位、4 位、8 位和 16 位,以及其他格式)。本篇 Java 技巧中提供的解决方案只处理 8 位和 24 位两种格式。这两种格式代表了最常见的变体。不管是哪种 Windows DIB 子类型,这种文件格式总是由 14 位文件头和 40 位信息头组成。这两个标头精确包含有关文件的存储内容和存储次序的信息。有关标头中每一项的确切含义,请参考 Microsoft Software Development Kit (SDK)。文件其余部分的内容随信息头中数据的不同而不同。我们看一下本文要处理的两种子类型。24 位格式很简单:RGB(红-绿-蓝)颜色值(3 个字节,并按 BGR 排序)紧接在信息头之后。但是,每个扫描行都被补足到 4 个字节。按照说明文档(请参阅 Microsoft SDK)的说法,这种“补足”是为了优化 Windows 位图绘图 API。同时,底部的扫描行是文件中的第一项内容 -- 因此相对普通的图形坐标系统(其矢量方向的正向分别为向下和向右)而言,必须从后向前读取图像。8 位子类型由于在信息头和象素数据之间插入调色板信息而复杂化。因此,每个象素条目只是进入 24 位 RGB 颜色的调色板数组的一个 8 位索引。在象素信息中,每个扫描行同样被补足到 4 个字节。请注意,本文提供的位图图像加载方法不支持对压缩位图图像进行解压缩。实际上,这个例程甚至不寻求这种可能性!如果遇到压缩 Windows DIB 文件,该例程肯定会产生异常。Windows SDK 中有对压缩 Windows DIB 格式的说明。至于性能,在运行 Microsoft Windows 95 的 486-DX2-66MHz 系统上,该例程读取 24 位 640 x 480 的文件(大约 920 千字节)所需的时间不超过 10 秒。使用 BufferedInputStream 而不是 FileInputStream 可明显提高性能。以下例程读取两种文件格式中的任一种,并生成一个 Image 图像。以下代码并未包含全面的错误和异常处理,以避免使该例程更加复杂。您总可用 Windows Paint 程序对不支持的 Windows DIB 子类型进行转换。    /**
     loadbitmap() 方法由 Windows C 代码转换而来。
     只能读取未压缩的 24 位和 8 位图像。已在
     Windows 95 上用 Microsoft Paint 保存的图像
    对它进行了测试。如果图像不是 24 位或 8 位图像,
     该程序拒绝进行任何尝试。我猜测如果先用 1100,
     然后用 0011 对字节执行掩码操作,则也可将 4 位
     图像包括在内。我实际上对这些图像不感兴趣。
     如果尝试读取压缩图像,该例程可能失败,并产生
     一个 IOException 异常。如果变量 ncompression
     不为 0,则表示已经过压缩。 参数:
         sdir 和 sfile 是 FileDialog 的
         getDirectory() 和 getFile() 方法的结果。 返回值:
         Image 对象,切记要检查 (Image)null !!!!    */
        public Image loadbitmap (String sdir, String sfile)
     {
     Image image;
     System.out.println("loading:"+sdir+sfile);
     try
         {
         FileInputStream fs=new FileInputStream(sdir+sfile);
         int bflen=14; // 14 字节 BITMAPFILEHEADER
         byte bf[]=new byte[bflen];
         fs.read(bf,0,bflen);
         int bilen=40; // 40 字节 BITMAPINFOHEADER
         byte bi[]=new byte[bilen];
         fs.read(bi,0,bilen);     // 解释数据。
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nsize = (((int)bf[5]&0xff)<<24)
    &nbsp;&nbsp;| (((int)bf[4]&0xff)<<16)
    &nbsp;&nbsp;| (((int)bf[3]&0xff)<<8)
    &nbsp;&nbsp;| (int)bf[2]&0xff;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("File type is :"+(char)bf[0]+(char)bf[1]);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Size of file is :"+nsize);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nbisize = (((int)bi[3]&0xff)<<24)
    &nbsp;&nbsp;| (((int)bi[2]&0xff)<<16)
    &nbsp;&nbsp;| (((int)bi[1]&0xff)<<8)
    &nbsp;&nbsp;| (int)bi[0]&0xff;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Size of bitmapinfoheader is :"+nbisize);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nwidth = (((int)bi[7]&0xff)<<24)
    &nbsp;&nbsp;| (((int)bi[6]&0xff)<<16)
    &nbsp;&nbsp;| (((int)bi[5]&0xff)<<8)
    &nbsp;&nbsp;| (int)bi[4]&0xff;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Width is :"+nwidth);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nheight = (((int)bi[11]&0xff)<<24)
    &nbsp;&nbsp;| (((int)bi[10]&0xff)<<16)
    &nbsp;&nbsp;| (((int)bi[9]&0xff)<<8)
    &nbsp;&nbsp;| (int)bi[8]&0xff;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Height is :"+nheight);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nplanes = (((int)bi[13]&0xff)<<8) | (int)bi[12]&0xff;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Planes is :"+nplanes);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nbitcount = (((int)bi[15]&0xff)<<8) | (int)bi[14]&0xff;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("BitCount is :"+nbitcount);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 查找表明压缩的非零值
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int ncompression = (((int)bi[19])<<24)
    &nbsp;&nbsp;| (((int)bi[18])<<16)
    &nbsp;&nbsp;| (((int)bi[17])<<8)
    &nbsp;&nbsp;| (int)bi[16];
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Compression is :"+ncompression);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nsizeimage = (((int)bi[23]&0xff)<<24)
    &nbsp;&nbsp;| (((int)bi[22]&0xff)<<16)
    &nbsp;&nbsp;| (((int)bi[21]&0xff)<<8)
    &nbsp;&nbsp;| (int)bi[20]&0xff;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("SizeImage is :"+nsizeimage);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nxpm = (((int)bi[27]&0xff)<<24)
    &nbsp;&nbsp;| (((int)bi[26]&0xff)<<16)
    &nbsp;&nbsp;| (((int)bi[25]&0xff)<<8)
    &nbsp;&nbsp;| (int)bi[24]&0xff;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("X-Pixels per meter is :"+nxpm);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nypm = (((int)bi[31]&0xff)<<24)
    &nbsp;&nbsp;| (((int)bi[30]&0xff)<<16)
    &nbsp;&nbsp;| (((int)bi[29]&0xff)<<8)
    &nbsp;&nbsp;| (int)bi[28]&0xff;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Y-Pixels per meter is :"+nypm);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nclrused = (((int)bi[35]&0xff)<<24)
    &nbsp;&nbsp;| (((int)bi[34]&0xff)<<16)
    &nbsp;&nbsp;| (((int)bi[33]&0xff)<<8)
    &nbsp;&nbsp;| (int)bi[32]&0xff;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Colors used are :"+nclrused);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int nclrimp = (((int)bi[39]&0xff)<<24)
    &nbsp;&nbsp;| (((int)bi[38]&0xff)<<16)
    &nbsp;&nbsp;| (((int)bi[37]&0xff)<<8)
    &nbsp;&nbsp;| (int)bi[36]&0xff;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Colors important are :"+nclrimp);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (nbitcount==24)
    &nbsp;&nbsp;{
    &nbsp;&nbsp;// 24 位格式不包含调色板数据,但扫描行被补足到
    &nbsp;&nbsp;// 4 个字节。
    &nbsp;&nbsp;int npad = (nsizeimage / nheight) - nwidth * 3;
    &nbsp;&nbsp;int ndata[] = new int [nheight * nwidth];
    &nbsp;&nbsp;byte brgb[] = new byte [( nwidth + npad) * 3 * nheight];
    &nbsp;&nbsp;fs.read (brgb, 0, (nwidth + npad) * 3 * nheight);
    &nbsp;&nbsp;int nindex = 0;
    &nbsp;&nbsp;for (int j = 0; j < nheight; j++)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i < nwidth; i++)
    &nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;ndata [nwidth * (nheight - j - 1) + i] =
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(255&0xff)<<24
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| (((int)brgb[nindex+2]&0xff)<<16)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| (((int)brgb[nindex+1]&0xff)<<8)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| (int)brgb[nindex]&0xff;
    &nbsp;&nbsp;&nbsp;// System.out.println("Encoded Color at ("
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+i+","+j+")is:"+nrgb+" (R,G,B)= ("
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+((int)(brgb[2]) & 0xff)+","
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+((int)brgb[1]&0xff)+","
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+((int)brgb[0]&0xff)+")");
    &nbsp;&nbsp;&nbsp;nindex += 3;
    &nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nindex += npad;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;image = createImage
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( new MemoryImageSource (nwidth, nheight,
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ndata, 0, nwidth));
    &nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if (nbitcount == 8)
    &nbsp;&nbsp;{
    &nbsp;&nbsp;// 必须确定颜色数。如果 clrsused 参数大于 0,
    &nbsp;&nbsp;// 则颜色数由它决定。如果它等于 0,则根据
    &nbsp;&nbsp;// bitsperpixel 计算颜色数。
    &nbsp;&nbsp;int nNumColors = 0;
    &nbsp;&nbsp;if (nclrused > 0)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nNumColors = nclrused;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;else
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nNumColors = (1&0xff)<<nbitcount;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;System.out.println("The number of Colors is"+nNumColors);&nbsp;&nbsp;// 某些位图不计算 sizeimage 域,请找出
    &nbsp;&nbsp;// 这些情况并对它们进行修正。
    &nbsp;&nbsp;if (nsizeimage == 0)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nsizeimage = ((((nwidth*nbitcount)+31) & ~31 ) >> 3);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nsizeimage *= nheight;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("nsizeimage (backup) is"+nsizeimage);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;// 读取调色板颜色。
    &nbsp;&nbsp;int npalette[] = new int [nNumColors];
    &nbsp;&nbsp;byte bpalette[] = new byte [nNumColors*4];
    &nbsp;&nbsp;fs.read (bpalette, 0, nNumColors*4);
    &nbsp;&nbsp;int nindex8 = 0;
    &nbsp;&nbsp;for (int n = 0; n < nNumColors; n++)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;npalette[n] = (255&0xff)<<24
    &nbsp;&nbsp;&nbsp;| (((int)bpalette[nindex8+2]&0xff)<<16)
    &nbsp;&nbsp;&nbsp;| (((int)bpalette[nindex8+1]&0xff)<<8)
    &nbsp;&nbsp;&nbsp;| (int)bpalette[nindex8]&0xff;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// System.out.println ("Palette Color "+n
    &nbsp;&nbsp;&nbsp;+" is:"+npalette[n]+" (res,R,G,B)= ("
    &nbsp;&nbsp;&nbsp;+((int)(bpalette[nindex8+3]) & 0xff)+","
    &nbsp;&nbsp;&nbsp;+((int)(bpalette[nindex8+2]) & 0xff)+","
    &nbsp;&nbsp;&nbsp;+((int)bpalette[nindex8+1]&0xff)+","
    &nbsp;&nbsp;&nbsp;+((int)bpalette[nindex8]&0xff)+")");
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nindex8 += 4;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;// 读取图像数据(实际上是调色板的索引)
    &nbsp;&nbsp;// 扫描行仍被补足到 4 个字节。
    &nbsp;&nbsp;int npad8 = (nsizeimage / nheight) - nwidth;
    &nbsp;&nbsp;System.out.println("nPad is:"+npad8);&nbsp;&nbsp;int ndata8[] = new int [nwidth*nheight];
    &nbsp;&nbsp;byte bdata[] = new byte [(nwidth+npad8)*nheight];
    &nbsp;&nbsp;fs.read (bdata, 0, (nwidth+npad8)*nheight);
    &nbsp;&nbsp;nindex8 = 0;
    &nbsp;&nbsp;for (int j8 = 0; j8 < nheight; j8++)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i8 = 0; i8 < nwidth; i8++)
    &nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;ndata8 [nwidth*(nheight-j8-1)+i8] =
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;npalette [((int)bdata[nindex8]&0xff)];
    &nbsp;&nbsp;&nbsp;nindex8++;
    &nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nindex8 += npad8;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;image = createImage
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( new MemoryImageSource (nwidth, nheight,
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ndata8, 0, nwidth));
    &nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
    &nbsp;&nbsp;{
    &nbsp;&nbsp;System.out.println ("Not a 24-bit or 8-bit Windows Bitmap, aborting...");
    &nbsp;&nbsp;image = (Image)null;
    &nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fs.close();
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return image;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;catch (Exception e)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Caught exception in loadbitmap!");
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;return (Image) null;
    &nbsp;}
    您已掌握了读取位图文件的技巧。很容易对此方法进行扩展,使它能够读取单色和 16 色(4 位)格式。
      

  2.   

     
      
    Java 技巧 43:如何在 Java 应用程序中读取 8 位和 24 位 Microsoft Windows 位图
    在 Java 应用程序中加载位图文件的逐步指南作者:Jeff West 和 John D. Mitchell摘要
    目前,标准的 getImage() 方法仅支持 GIF 和 JPEG 图像。尽管存在用于读取 PNG(可移植网络图形)格式的 Java 例程,但我们还没听说过有用于读取 Microsoft Windows 位图图像的阅读程序。Jeff West 撰写的这篇技巧提供了加载 Windows 位图图像的代码。 
    Java 的当前发行版并不正式支持在 Java 应用程序中读取 Microsoft Windows 位图文件。但别担心,我们有办法解决这个问题!这篇技巧将说明如何完成这一任务 -- 我们首先说明读取 Microsoft Windows 文件格式的基本步骤。 
    Windows DIB(设备独立的位图)文件格式比较简单。与纯位图格式不同,DIB 格式保留着用于在内存中存储图像的明确信息。问题是图像格式的变体如此之多(1 位、4 位、8 位和 16 位,以及其他格式)。本篇 Java 技巧中提供的解决方案只处理 8 位和 24 位两种格式。这两种格式代表了最常见的变体。 不管是哪种 Windows DIB 子类型,这种文件格式总是由 14 位文件头和 40 位信息头组成。这两个标头精确包含有关文件的存储内容和存储次序的信息。有关标头中每一项的确切含义,请参考 Microsoft Software Development Kit (SDK)。文件其余部分的内容随信息头中数据的不同而不同。 我们看一下本文要处理的两种子类型。24 位格式很简单:RGB(红-绿-蓝)颜色值(3 个字节,并按 BGR 排序)紧接在信息头之后。但是,每个扫描行都被补足到 4 个字节。按照说明文档(请参阅 Microsoft SDK)的说法,这种“补足”是为了优化 Windows 位图绘图 API。同时,底部的扫描行是文件中的第一项内容 -- 因此相对普通的图形坐标系统(其矢量方向的正向分别为向下和向右)而言,必须从后向前读取图像。 8 位子类型由于在信息头和象素数据之间插入调色板信息而复杂化。因此,每个象素条目只是进入 24 位 RGB 颜色的调色板数组的一个 8 位索引。在象素信息中,每个扫描行同样被补足到 4 个字节。 请注意,本文提供的位图图像加载方法不支持对压缩位图图像进行解压缩。实际上,这个例程甚至不寻求这种可能性!如果遇到压缩 Windows DIB 文件,该例程肯定会产生异常。Windows SDK 中有对压缩 Windows DIB 格式的说明。 至于性能,在运行 Microsoft Windows 95 的 486-DX2-66MHz 系统上,该例程读取 24 位 640 x 480 的文件(大约 920 千字节)所需的时间不超过 10 秒。使用 BufferedInputStream 而不是 FileInputStream 可明显提高性能。 以下例程读取两种文件格式中的任一种,并生成一个 Image 图像。以下代码并未包含全面的错误和异常处理,以避免使该例程更加复杂。您总可用 Windows Paint 程序对不支持的 Windows DIB 子类型进行转换。     /**
     loadbitmap() 方法由 Windows C 代码转换而来。
     只能读取未压缩的 24 位和 8 位图像。已在
     Windows 95 上用 Microsoft Paint 保存的图像
    对它进行了测试。如果图像不是 24 位或 8 位图像,
     该程序拒绝进行任何尝试。我猜测如果先用 1100,
     然后用 0011 对字节执行掩码操作,则也可将 4 位
     图像包括在内。我实际上对这些图像不感兴趣。
     如果尝试读取压缩图像,该例程可能失败,并产生
     一个 IOException 异常。如果变量 ncompression 
     不为 0,则表示已经过压缩。 参数:
         sdir 和 sfile 是 FileDialog 的
         getDirectory() 和 getFile() 方法的结果。 返回值:
         Image 对象,切记要检查 (Image)null !!!!    */
        public Image loadbitmap (String sdir, String sfile)
     {
     Image image;
     System.out.println("loading:"+sdir+sfile);
     try
         {
         FileInputStream fs=new FileInputStream(sdir+sfile);
         int bflen=14; // 14 字节 BITMAPFILEHEADER
         byte bf[]=new byte[bflen];
         fs.read(bf,0,bflen);
         int bilen=40; // 40 字节 BITMAPINFOHEADER
         byte bi[]=new byte[bilen];
         fs.read(bi,0,bilen);     // 解释数据。
         int nsize = (((int)bf[5]&0xff)<<24) 
      | (((int)bf[4]&0xff)<<16)
      | (((int)bf[3]&0xff)<<8)
      | (int)bf[2]&0xff;
         System.out.println("File type is :"+(char)bf[0]+(char)bf[1]);
         System.out.println("Size of file is :"+nsize);     int nbisize = (((int)bi[3]&0xff)<<24)
      | (((int)bi[2]&0xff)<<16)
      | (((int)bi[1]&0xff)<<8)
      | (int)bi[0]&0xff;
         System.out.println("Size of bitmapinfoheader is :"+nbisize);     int nwidth = (((int)bi[7]&0xff)<<24)
      | (((int)bi[6]&0xff)<<16)
      | (((int)bi[5]&0xff)<<8)
      | (int)bi[4]&0xff;
         System.out.println("Width is :"+nwidth);     int nheight = (((int)bi[11]&0xff)<<24)
      | (((int)bi[10]&0xff)<<16)
      | (((int)bi[9]&0xff)<<8)
      | (int)bi[8]&0xff;
         System.out.println("Height is :"+nheight);     int nplanes = (((int)bi[13]&0xff)<<8) | (int)bi[12]&0xff;
         System.out.println("Planes is :"+nplanes);     int nbitcount = (((int)bi[15]&0xff)<<8) | (int)bi[14]&0xff;
         System.out.println("BitCount is :"+nbitcount);     // 查找表明压缩的非零值
         int ncompression = (((int)bi[19])<<24)
      | (((int)bi[18])<<16)
      | (((int)bi[17])<<8)
      | (int)bi[16];
         System.out.println("Compression is :"+ncompression);     int nsizeimage = (((int)bi[23]&0xff)<<24)
      | (((int)bi[22]&0xff)<<16)
      | (((int)bi[21]&0xff)<<8)
      | (int)bi[20]&0xff;
         System.out.println("SizeImage is :"+nsizeimage);     int nxpm = (((int)bi[27]&0xff)<<24)
      | (((int)bi[26]&0xff)<<16)
      | (((int)bi[25]&0xff)<<8)
      | (int)bi[24]&0xff;
         System.out.println("X-Pixels per meter is :"+nxpm);     int nypm = (((int)bi[31]&0xff)<<24)
      | (((int)bi[30]&0xff)<<16)
      | (((int)bi[29]&0xff)<<8)
      | (int)bi[28]&0xff;
         System.out.println("Y-Pixels per meter is :"+nypm);     int nclrused = (((int)bi[35]&0xff)<<24)
      | (((int)bi[34]&0xff)<<16)
      | (((int)bi[33]&0xff)<<8)
      | (int)bi[32]&0xff;
         System.out.println("Colors used are :"+nclrused);     int nclrimp = (((int)bi[39]&0xff)<<24)
      | (((int)bi[38]&0xff)<<16)
      | (((int)bi[37]&0xff)<<8)
      | (int)bi[36]&0xff;
         System.out.println("Colors important are :"+nclrimp);     if (nbitcount==24)
      {
      // 24 位格式不包含调色板数据,但扫描行被补足到
      // 4 个字节。
      int npad = (nsizeimage / nheight) - nwidth * 3;
      int ndata[] = new int [nheight * nwidth];
      byte brgb[] = new byte [( nwidth + npad) * 3 * nheight];
      fs.read (brgb, 0, (nwidth + npad) * 3 * nheight);
      int nindex = 0;
      for (int j = 0; j < nheight; j++)
          {
          for (int i = 0; i < nwidth; i++)
       {
       ndata [nwidth * (nheight - j - 1) + i] =
           (255&0xff)<<24
           | (((int)brgb[nindex+2]&0xff)<<16)
           | (((int)brgb[nindex+1]&0xff)<<8)
           | (int)brgb[nindex]&0xff;
       // System.out.println("Encoded Color at ("
           +i+","+j+")is:"+nrgb+" (R,G,B)= ("
           +((int)(brgb[2]) & 0xff)+","
           +((int)brgb[1]&0xff)+","
           +((int)brgb[0]&0xff)+")");
       nindex += 3;
       }
          nindex += npad;
          }  image = createImage
          ( new MemoryImageSource (nwidth, nheight,
              ndata, 0, nwidth));
      }
         else if (nbitcount == 8)
      {
      // 必须确定颜色数。如果 clrsused 参数大于 0,
      // 则颜色数由它决定。如果它等于 0,则根据
      // bitsperpixel 计算颜色数。
      int nNumColors = 0;
      if (nclrused > 0)
          {
          nNumColors = nclrused;
          }
      else
          {
          nNumColors = (1&0xff)<<nbitcount;
          }
      System.out.println("The number of Colors is"+nNumColors);  // 某些位图不计算 sizeimage 域,请找出
      // 这些情况并对它们进行修正。
      if (nsizeimage == 0)
          {
          nsizeimage = ((((nwidth*nbitcount)+31) & ~31 ) >> 3);
          nsizeimage *= nheight;
          System.out.println("nsizeimage (backup) is"+nsizeimage);
          }  // 读取调色板颜色。
      int npalette[] = new int [nNumColors];
      byte bpalette[] = new byte [nNumColors*4];
      fs.read (bpalette, 0, nNumColors*4);
      int nindex8 = 0;
      for (int n = 0; n < nNumColors; n++)
          {
          npalette[n] = (255&0xff)<<24
       | (((int)bpalette[nindex8+2]&0xff)<<16)
       | (((int)bpalette[nindex8+1]&0xff)<<8)
       | (int)bpalette[nindex8]&0xff;
          // System.out.println ("Palette Color "+n
       +" is:"+npalette[n]+" (res,R,G,B)= ("
       +((int)(bpalette[nindex8+3]) & 0xff)+","
       +((int)(bpalette[nindex8+2]) & 0xff)+","
       +((int)bpalette[nindex8+1]&0xff)+","
       +((int)bpalette[nindex8]&0xff)+")");
          nindex8 += 4;
          }  // 读取图像数据(实际上是调色板的索引)
      // 扫描行仍被补足到 4 个字节。
      int npad8 = (nsizeimage / nheight) - nwidth;
      System.out.println("nPad is:"+npad8);  int ndata8[] = new int [nwidth*nheight];
      byte bdata[] = new byte [(nwidth+npad8)*nheight];
      fs.read (bdata, 0, (nwidth+npad8)*nheight);
      nindex8 = 0;
      for (int j8 = 0; j8 < nheight; j8++)
          {
          for (int i8 = 0; i8 < nwidth; i8++)
       {
       ndata8 [nwidth*(nheight-j8-1)+i8] =
           npalette [((int)bdata[nindex8]&0xff)];
       nindex8++;
       }
          nindex8 += npad8;
          }  image = createImage
          ( new MemoryImageSource (nwidth, nheight,
              ndata8, 0, nwidth));
      }
         else
      {
      System.out.println ("Not a 24-bit or 8-bit Windows Bitmap, aborting...");
      image = (Image)null;
      }     fs.close();
         return image;
         }
     catch (Exception e)
         {
         System.out.println("Caught exception in loadbitmap!");
         }
     return (Image) null;
     }
    您已掌握了读取位图文件的技巧。很容易对此方法进行扩展,使它能够读取单色和 16 色(4 位)格式。 作者简介
    Jeff West 是加州圣地亚哥市的一名工程学研究生。在研究燃烧和火焰扩张的闲暇之余,他沉迷于 Java。