9.html
来自「写给JSP初级程序员的书」· HTML 代码 · 共 325 行 · 第 1/2 页
HTML
325 行
int nxpm = (((int)bi[27]&0xff)<<24)<BR>
| (((int)bi[26]&0xff)<<16)<BR>
| (((int)bi[25]&0xff)<<8)<BR>
| (int)bi[24]&0xff;<BR>
System.out.println("X-Pixels per meter is :"+nxpm);<BR>
<BR>
int nypm = (((int)bi[31]&0xff)<<24)<BR>
| (((int)bi[30]&0xff)<<16)<BR>
| (((int)bi[29]&0xff)<<8)<BR>
| (int)bi[28]&0xff;<BR>
System.out.println("Y-Pixels per meter is :"+nypm);<BR>
<BR>
int nclrused = (((int)bi[35]&0xff)<<24)<BR>
| (((int)bi[34]&0xff)<<16)<BR>
| (((int)bi[33]&0xff)<<8)<BR>
| (int)bi[32]&0xff;<BR>
System.out.println("Colors used are :"+nclrused);<BR>
<BR>
int nclrimp = (((int)bi[39]&0xff)<<24)<BR>
| (((int)bi[38]&0xff)<<16)<BR>
| (((int)bi[37]&0xff)<<8)<BR>
| (int)bi[36]&0xff;<BR>
System.out.println("Colors important are :"+nclrimp);<BR>
<BR>
if (nbitcount==24)<BR>
{<BR>
// 24 位格式不包含调色板数据,但扫描行被补足到<BR>
// 4 个字节。<BR>
int npad = (nsizeimage / nheight) - nwidth * 3;<BR>
int ndata[] = new int [nheight * nwidth];<BR>
byte brgb[] = new byte [( nwidth + npad) * 3 * nheight];<BR>
fs.read (brgb, 0, (nwidth + npad) * 3 * nheight);<BR>
int nindex = 0;<BR>
for (int j = 0; j < nheight; j++)<BR>
{<BR>
for (int i = 0; i < nwidth; i++)<BR>
{<BR>
ndata [nwidth * (nheight - j - 1) + i] =<BR>
(255&0xff)<<24<BR>
| (((int)brgb[nindex+2]&0xff)<<16)<BR>
| (((int)brgb[nindex+1]&0xff)<<8)<BR>
| (int)brgb[nindex]&0xff;<BR>
// System.out.println("Encoded Color at ("<BR>
+i+","+j+")is:"+nrgb+" (R,G,B)= ("<BR>
+((int)(brgb[2]) & 0xff)+","<BR>
+((int)brgb[1]&0xff)+","<BR>
+((int)brgb[0]&0xff)+")");<BR>
nindex += 3;<BR>
}<BR>
nindex += npad;<BR>
}<BR>
<BR>
image = createImage<BR>
( new MemoryImageSource (nwidth, nheight,<BR>
ndata, 0, nwidth));<BR>
}<BR>
else if (nbitcount == 8)<BR>
{<BR>
// 必须确定颜色数。如果 clrsused 参数大于 0,<BR>
// 则颜色数由它决定。如果它等于 0,则根据<BR>
// bitsperpixel 计算颜色数。<BR>
int nNumColors = 0;<BR>
if (nclrused > 0)<BR>
{<BR>
nNumColors = nclrused;<BR>
}<BR>
else<BR>
{<BR>
nNumColors = (1&0xff)<<nbitcount;<BR>
}<BR>
System.out.println("The number of Colors is"+nNumColors);<BR>
<BR>
// 某些位图不计算 sizeimage 域,请找出<BR>
// 这些情况并对它们进行修正。<BR>
if (nsizeimage == 0)<BR>
{<BR>
nsizeimage = ((((nwidth*nbitcount)+31) & ~31 ) >> 3);<BR>
nsizeimage *= nheight;<BR>
System.out.println("nsizeimage (backup) is"+nsizeimage);<BR>
}<BR>
<BR>
// 读取调色板颜色。<BR>
int npalette[] = new int [nNumColors];<BR>
byte bpalette[] = new byte [nNumColors*4];<BR>
fs.read (bpalette, 0, nNumColors*4);<BR>
int nindex8 = 0;<BR>
for (int n = 0; n < nNumColors; n++)<BR>
{<BR>
npalette[n] = (255&0xff)<<24<BR>
| (((int)bpalette[nindex8+2]&0xff)<<16)<BR>
| (((int)bpalette[nindex8+1]&0xff)<<8)<BR>
| (int)bpalette[nindex8]&0xff;<BR>
// System.out.println ("Palette Color "+n<BR>
+" is:"+npalette[n]+" (res,R,G,B)= ("<BR>
+((int)(bpalette[nindex8+3]) & 0xff)+","<BR>
+((int)(bpalette[nindex8+2]) & 0xff)+","<BR>
+((int)bpalette[nindex8+1]&0xff)+","<BR>
+((int)bpalette[nindex8]&0xff)+")");<BR>
nindex8 += 4;<BR>
}<BR>
<BR>
// 读取图像数据(实际上是调色板的索引)<BR>
// 扫描行仍被补足到 4 个字节。<BR>
int npad8 = (nsizeimage / nheight) - nwidth;<BR>
System.out.println("nPad is:"+npad8);<BR>
<BR>
int ndata8[] = new int [nwidth*nheight];<BR>
byte bdata[] = new byte [(nwidth+npad8)*nheight];<BR>
fs.read (bdata, 0, (nwidth+npad8)*nheight);<BR>
nindex8 = 0;<BR>
for (int j8 = 0; j8 < nheight; j8++)<BR>
{<BR>
for (int i8 = 0; i8 < nwidth; i8++)<BR>
{<BR>
ndata8 [nwidth*(nheight-j8-1)+i8] =<BR>
npalette [((int)bdata[nindex8]&0xff)];<BR>
nindex8++;<BR>
}<BR>
nindex8 += npad8;<BR>
}<BR>
<BR>
image = createImage<BR>
( new MemoryImageSource (nwidth, nheight,<BR>
ndata8, 0, nwidth));<BR>
}<BR>
else<BR>
{<BR>
System.out.println ("Not a 24-bit or 8-bit Windows Bitmap, aborting...");<BR>
image = (Image)null;<BR>
}<BR>
<BR>
fs.close();<BR>
return image;<BR>
}<BR>
catch (Exception e)<BR>
{<BR>
System.out.println("Caught exception in loadbitmap!");<BR>
}<BR>
return (Image) null;<BR>
}<BR>
</code>
<P>
您已掌握了读取位图文件的技巧。很容易对此方法进行扩展,使它能够读取单色和 16 色(4 位)格式。
<!-- end body text -->
<P>
<A NAME="bio">
<TR><TD VALIGN="TOP">
<STRONG>
<FONT SIZE="-1" FACE="Arial,Helvetica,Sans-serif">作者简介</FONT></STRONG><BR>
Jeff West 是加州圣地亚哥市的一名工程学研究生。在研究燃烧和火焰扩张的闲暇之余,他沉迷于 Java。
</TD>
</TR>
</td>
</tr>
</td>
</tr>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?