[摘要](int)bpalette[nindex8]&0xff; // System.out.println ("Palette Color "+n +"...
(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。
关键词:Java应用程序中加载位图文件的逐步向导