⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmpjlabel.java

📁 java版的位图处理
💻 JAVA
字号:
package bmpload;
import java.awt.image.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class BmpJLabel extends JLabel
{
	File file=null;
	public BmpJLabel(){
		super();
	}
	public void loadBmp(File file)
	{
		this.file=file;
		String name=file.getPath();
		ImageIcon icon=new ImageIcon(loadbitmap(name,BmpJLabel.this));
		if(icon.getIconWidth()>getWidth())
		{
			icon=new ImageIcon(icon.getImage().getScaledInstance(getWidth(),-1,Image.SCALE_DEFAULT));
		}
		setIcon(icon);
	}
	public File getFileName()
	{
		return this.file;
	}
	public static Image loadbitmap (String str,JComponent com)
	{
		Image image;
		try
		{
			 FileInputStream fs=new FileInputStream(str);
			 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;

			 int nbisize = (((int)bi[3]&0xff)<<24)
		  | (((int)bi[2]&0xff)<<16)
		  | (((int)bi[1]&0xff)<<8)
		  | (int)bi[0]&0xff;

			 int nwidth = (((int)bi[7]&0xff)<<24)
		  | (((int)bi[6]&0xff)<<16)
		  | (((int)bi[5]&0xff)<<8)
		  | (int)bi[4]&0xff;

			 int nheight = (((int)bi[11]&0xff)<<24)
		  | (((int)bi[10]&0xff)<<16)
		  | (((int)bi[9]&0xff)<<8)
		  | (int)bi[8]&0xff;

			 int nplanes = (((int)bi[13]&0xff)<<8) | (int)bi[12]&0xff;

			 int nbitcount = (((int)bi[15]&0xff)<<8) | (int)bi[14]&0xff;

			 // 查找表明压缩的非零值
			 int ncompression = (((int)bi[19])<<24)
		  | (((int)bi[18])<<16)
		  | (((int)bi[17])<<8)
		  | (int)bi[16];

			 int nsizeimage = (((int)bi[23]&0xff)<<24)
		  | (((int)bi[22]&0xff)<<16)
		  | (((int)bi[21]&0xff)<<8)
		  | (int)bi[20]&0xff;

			 int nxpm = (((int)bi[27]&0xff)<<24)
		  | (((int)bi[26]&0xff)<<16)
		  | (((int)bi[25]&0xff)<<8)
		  | (int)bi[24]&0xff;

			 int nypm = (((int)bi[31]&0xff)<<24)
		  | (((int)bi[30]&0xff)<<16)
		  | (((int)bi[29]&0xff)<<8)
		  | (int)bi[28]&0xff;

			 int nclrused = (((int)bi[35]&0xff)<<24)
		  | (((int)bi[34]&0xff)<<16)
		  | (((int)bi[33]&0xff)<<8)
		  | (int)bi[32]&0xff;

			 int nclrimp = (((int)bi[39]&0xff)<<24)
		  | (((int)bi[38]&0xff)<<16)
		  | (((int)bi[37]&0xff)<<8)
		  | (int)bi[36]&0xff;

			 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;
		   nindex += 3;
		   }
			  nindex += npad;
			  }

		  image =com.createImage( new MemoryImageSource (nwidth, nheight,
				  ndata, 0, nwidth));
		  }
			 else if (nbitcount == 8)
		  {
		  int nNumColors = 0;
		  if (nclrused > 0)
			  {
			  nNumColors = nclrused;
			  }
		  else
			  {
			  nNumColors = (1&0xff)<<nbitcount;
			  }
		  if (nsizeimage == 0)
			  {
			  nsizeimage = ((((nwidth*nbitcount)+31) & ~31 ) >> 3);
			  nsizeimage *= nheight;
			  }

		  // 读取调色板颜色。
		  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;
			  nindex8 += 4;
			  }

		  int npad8 = (nsizeimage / nheight) - nwidth;

		  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 =com.createImage
			  ( new MemoryImageSource (nwidth, nheight,
				  ndata8, 0, nwidth));
		  }
			 else
		  {
		  image = (Image)null;
		  }

			 fs.close();
			 return image;
			 }
		 catch (Exception e)
			 {
			 }
		 return (Image) null;
	 }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -