读取bmp图片数据


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  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) << 24
  41. | (((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<nHeight;j++){
  59.   for(int i=0;i<nWidth;i++){
  60.   out.print((65536*256+nData[nWidth * (nHeight - j - 1) + i])+"_"
  61.   +nR[nWidth * (nHeight - j - 1) + i]+"_"
  62.   +nG[nWidth * (nHeight - j - 1) + i]+"_"
  63.   +nB[nWidth * (nHeight - j - 1) + i]+" ");
  64.  
  65.   }
  66.   out.println("");
  67.   }
  68.   out.close();
  69. */
  70. }
  71. }
  72. catch (Exception e) {
  73. e.printStackTrace();
  74. throw new Exception(e);
  75. }
  76. finally {
  77. if (fs != null) {
  78. fs.close();
  79. }
  80. }
  81. // return image;
  82. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.