1 public void getBMPImage(String source) throws Exception { 2 3 clearNData(); //清除数据保存区 4 FileInputStream fs = null; 5 6 try { 7 fs = new FileInputStream(source); 8 int bfLen = 14; 9 byte bf[] = new byte[bfLen];10 fs.read(bf, 0, bfLen); // 读取14字节BMP文件头11 int biLen = 40;12 byte bi[] = new byte[biLen];13 fs.read(bi, 0, biLen); // 读取40字节BMP信息头14 // 源图宽度15 nWidth = (((int) bi[7] & 0xff) << 24)16 | (((int) bi[6] & 0xff) << 16)17 | (((int) bi[5] & 0xff) << 8) | (int) bi[4] & 0xff;18 // 源图高度19 nHeight = (((int) bi[11] & 0xff) << 24)20 | (((int) bi[10] & 0xff) << 16)21 | (((int) bi[9] & 0xff) << 8) | (int) bi[8] & 0xff;22 // 位数23 nBitCount = (((int) bi[15] & 0xff) << 8) | (int) bi[14] & 0xff;24 // 源图大小25 int nSizeImage = (((int) bi[23] & 0xff) << 24)26 | (((int) bi[22] & 0xff) << 16)27 | (((int) bi[21] & 0xff) << 8) | (int) bi[20] & 0xff;28 // 对24位BMP进行解析29 if (nBitCount == 24){30 int nPad = (nSizeImage / nHeight) - nWidth * 3;31 nData = new int[nHeight * nWidth];32 nB=new int[nHeight * nWidth];33 nR=new int[nHeight * nWidth];34 nG=new int[nHeight * nWidth];35 byte bRGB[] = new byte[(nWidth + nPad) * 3 * nHeight];36 fs.read(bRGB, 0, (nWidth + nPad) * 3 * nHeight);37 int nIndex = 0;38 for (int j = 0; j < nHeight; j++){39 for (int i = 0; i < nWidth; i++) {40 nData[nWidth * (nHeight - j - 1) + i] = (255 & 0xff) << 2441 | (((int) bRGB[nIndex + 2] & 0xff) << 16)42 | (((int) bRGB[nIndex + 1] & 0xff) << 8)43 | (int) bRGB[nIndex] & 0xff; 44 nB[nWidth * (nHeight - j - 1) + i]=(int) bRGB[nIndex]& 0xff;45 nG[nWidth * (nHeight - j - 1) + i]=(int) bRGB[nIndex+1]& 0xff;46 nR[nWidth * (nHeight - j - 1) + i]=(int) bRGB[nIndex+2]& 0xff;47 nIndex += 3;48 }49 nIndex += nPad;50 }51 // Toolkit kit = Toolkit.getDefaultToolkit();52 // image = kit.createImage(new MemoryImageSource(nWidth, nHeight,53 // nData, 0, nWidth));54 /* 55 //调试数据的读取56 FileWriter fw = new FileWriter("C:\\Documents and Settings\\Administrator\\My Documents\\nDataRaw.txt");//创建新文件57 PrintWriter out = new PrintWriter(fw);58 for(int j=0;j