本站首页    管理页面    写新日志    退出


[图形图象]读取bmp图片数据(java)
sunshine 发表于 2007/11/12 22:26:49

一 读取bmp图片数据 //  获取待检测图像 ,数据保存在数组 nData[],nB[] ,nG[] ,nR[]中  public  void getBMPImage(String source) throws Exception {                    clearNData();                        //清除数据保存区         FileInputStream fs = null;               try {            fs = new FileInputStream(source);            int bfLen = 14;            byte bf[] = new byte[bfLen];            fs.read(bf, 0, bfLen); // 读取14字节BMP文件头            int biLen = 40;            byte bi[] = new byte[biLen];            fs.read(bi, 0, biLen); // 读取40字节BMP信息头             // 源图宽度            nWidth = (((int) bi[7] & 0xff) << 24)                    | (((int) bi[6] & 0xff) << 16)                    | (((int) bi[5] & 0xff) << 8) | (int) bi[4] & 0xff;             // 源图高度            nHeight = (((int) bi[11] & 0xff) << 24)                    | (((int) bi[10] & 0xff) << 16)                    | (((int) bi[9] & 0xff) << 8) | (int) bi[8] & 0xff;             // 位数            nBitCount = (((int) bi[15] & 0xff) << 8) | (int) bi[14] & 0xff;             // 源图大小            int nSizeImage = (((int) bi[23] & 0xff) << 24)                    | (((int) bi[22] & 0xff) << 16)                    | (((int) bi[21] & 0xff) << 8) | (int) bi[20] & 0xff;             // 对24位BMP进行解析            if (nBitCount == 24){                int nPad = (nSizeImage / nHeight) - nWidth * 3;                nData = new int[nHeight * nWidth];                nB=new int[nHeight * nWidth];                nR=new int[nHeight * nWidth];                nG=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;                                              nB[nWidth * (nHeight - j - 1) + i]=(int) bRGB[nIndex]& 0xff;                        nG[nWidth * (nHeight - j - 1) + i]=(int) bRGB[nIndex+1]& 0xff;                        nR[nWidth * (nHeight - j - 1) + i]=(int) bRGB[nIndex+2]& 0xff;                        nIndex += 3;                    }                    nIndex += nPad;                } //               Toolkit kit = Toolkit.getDefaultToolkit(); //               image = kit.createImage(new MemoryImageSource(nWidth, nHeight, //                       nData, 0, nWidth)); /*               //调试数据的读取                 FileWriter fw = new FileWriter("C:\\Documents and Settings\\Administrator\\My Documents\\nDataRaw.txt");//创建新文件                PrintWriter out = new PrintWriter(fw);                for(int j=0;j<nHeight;j++){                 for(int i=0;i<nWidth;i++){                  out.print((65536*256+nData[nWidth * (nHeight - j - 1) + i])+"_"                    +nR[nWidth * (nHeight - j - 1) + i]+"_"                    +nG[nWidth * (nHeight - j - 1) + i]+"_"                    +nB[nWidth * (nHeight - j - 1) + i]+" ");                                   }                 out.println("");                }                out.close();*/                      }        }        catch (Exception e) {            e.printStackTrace();            throw new Exception(e);        }         finally {            if (fs != null) {                fs.close();            }        }     //   return image;    } 

阅读全文(4472) | 回复(0) | 编辑 | 精华

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)

 
«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31

  公告

有一种鸟儿是永远关不住的
因为它的每片羽翼上都沾满了自由的光辉

方向:计算机视觉 人工智能 演化算法

 


  我的分类(专题)
  最近日志

  最新评论

  留言板

  链接

  Blog信息
blog名称:阳光海岸心
日志总数:166
评论数量:237
留言数量:-4
访问次数:1450433
建立时间:2006年6月2日



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.031 second(s), page refreshed 144773318 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号