日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

读取bmp图片数据

發布時間:2023/12/9 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 读取bmp图片数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 }

?

轉載于:https://www.cnblogs.com/zt-zoro/p/4580714.html

總結

以上是生活随笔為你收集整理的读取bmp图片数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。