ITeye博客

关于BMP图片格式(部分垃圾代码)

wangshu3000 · 2007-11-17 19:05 · 游戏开发 · 编程语言 · Markdown

java 代码

  1. 做了个刷投票的程序 识别随即码图片的地方用到了BMP图片识别 需要识别出BMP图片里的数字
  2. 网站的BMP图片格式比较规则  20 * 45 的大小 每个数字大小是 10 * 8  左侧空 5 像素 右侧 5 像素
  3. 研究BMP图片格式发现 BMP主要包括 3 部分 头 颜色表 图像数据区
  4. 图像结构如下 类似C++里的结构体
  5. public   class  BMPHeader {
  6. public   byte [] bm =  new   byte [ 2 ]; // 2位 BM
  7. public   byte [] size =  new   byte [ 4 ]; // 4位 2038 (f6 07)
  8. public   byte [] save =  new   byte [ 4 ]; // 4位 00
  9. public   byte [] dataPos =  new   byte [ 4 ]; // 4位 图像数据区位置
  10. // (3604)文件头信息块大小,图像描述信息块的大小,图像颜色表的大小,保留(为01)
  11. public   byte [] desSize =  new   byte [ 4 ]; // 4位图像描述信息块大小28h
  12. public   byte [] width =  new   byte [ 4 ]; // 4位 宽度
  13. public   byte [] hight =  new   byte [ 4 ]; // 4位 高度
  14. public   byte [] plane =  new   byte [ 2 ]; // 2位 plane为0100
  15. public   byte [] bytes =  new   byte [ 2 ]; // 2位 图像的位数 0800
  16. public   byte [] zip =  new   byte [ 4 ]; // 4位 压缩方式00不压缩
  17. public   byte [] picSize =  new   byte [ 4 ]; // 4位 c0 03 = 960 图像区数据的大小
  18. public   byte [] shuiPing =  new   byte [ 4 ]; // 4位 水平每米有多少像素
  19. public   byte [] cuiZhi =  new   byte [ 4 ]; // 4位 垂直每米有多少像素
  20. public   byte [] colorNum =  new   byte [ 4 ]; // 4位 此图像所用的颜色数
  21. // 图像数据从436-7f5
  22. public   byte [] colorTab =  new   byte [ 1028 ];
  23. public   byte [] bmpData =  new   byte [ 960 ];
  24. }
  25. 颜色表ColorTab应该为 1024 长 不知道为什么会是 1028  没算明白这里
  26. 图像数据区是 960 位 每一位都是一个颜色点 保存颜色表中的索引数据  16 进制  3 行表示图像中的一行
  27. 从下往上排列  45 = 16 * 3 - 3  有 3 位补零 因为bmp要保证每行数据点要为 4 的整数倍
  28. 得到数据内容就可以根据行列坐标定位每一点的颜色 从而判断出图像中的数字
  29. import  java.io.File;
  30. import  java.io.FileInputStream;
  31. import  java.io.FileNotFoundException;
  32. import  java.io.IOException;
  33. public   class  BMPTest {
  34. private   byte [][] parse960ToLine( byte [] bmpData) {
  35. int  lineSize =  48 ;
  36. int  columnIndex =  0 ;
  37. int  rowIndex =  0 ;
  38. byte [][] bmp =  new   byte [ 20 ][ 48 ];
  39. for  ( int  i =  0 ; i < bmpData.length; i++) {
  40. bmp[rowIndex][columnIndex] = bmpData[i];
  41. columnIndex++;
  42. if  (columnIndex ==  48 ) {
  43. rowIndex++;
  44. columnIndex =  0 ;
  45. }
  46. }
  47. return  bmp;
  48. }
  49. private   short  parse1Num( byte [][] theByte) {
  50. if  (theByte[ 0 ][ 0 ] ==  0  && theByte[ 0 ][ 1 ] ==  0  && theByte[ 0 ][ 2 ] ==  0
  51. && theByte[ 0 ][ 3 ] ==  0  && theByte[ 0 ][ 4 ] ==  0
  52. && theByte[ 0 ][ 5 ] ==  0  && theByte[ 0 ][ 6 ] ==  0
  53. && theByte[ 0 ][ 7 ] ==  0  && theByte[ 7 ][ 0 ] ==  0
  54. && theByte[ 7 ][ 1 ] ==  0  && theByte[ 1 ][ 1 ] ==  0
  55. && theByte[ 1 ][ 2 ] ==  0  && theByte[ 2 ][ 2 ] ==  0
  56. && theByte[ 2 ][ 3 ] ==  0  && theByte[ 3 ][ 3 ] ==  0
  57. && theByte[ 3 ][ 4 ] ==  0  && theByte[ 4 ][ 4 ] ==  0 ) {
  58. return   2 ;
  59. }  else   if  (theByte[ 0 ][ 0 ] ==  3  && theByte[ 0 ][ 1 ] ==  0
  60. && theByte[ 0 ][ 2 ] ==  0  && theByte[ 0 ][ 3 ] ==  0
  61. && theByte[ 0 ][ 4 ] ==  0  && theByte[ 1 ][ 0 ] ==  0
  62. && theByte[ 1 ][ 1 ] ==  0  && theByte[ 1 ][ 2 ] ==  3
  63. && theByte[ 9 ][ 1 ] ==  0  && theByte[ 9 ][ 2 ] ==  0
  64. && theByte[ 9 ][ 3 ] ==  0  && theByte[ 9 ][ 4 ] ==  0
  65. && theByte[ 9 ][ 5 ] ==  0  && theByte[ 8 ][ 0 ] ==  0
  66. && theByte[ 8 ][ 1 ] ==  0  && theByte[ 8 ][ 5 ] ==  0
  67. && theByte[ 8 ][ 6 ] ==  0 ) {
  68. return   3 ;
  69. }  else   if  (theByte[ 0 ][ 1 ] ==  0  && theByte[ 0 ][ 2 ] ==  0
  70. && theByte[ 0 ][ 3 ] ==  0  && theByte[ 0 ][ 4 ] ==  0
  71. && theByte[ 0 ][ 5 ] ==  0  && theByte[ 0 ][ 6 ] ==  0
  72. && theByte[ 0 ][ 3 ] ==  0  && theByte[ 1 ][ 3 ] ==  0
  73. && theByte[ 2 ][ 3 ] ==  0  && theByte[ 3 ][ 3 ] ==  0
  74. && theByte[ 4 ][ 3 ] ==  0  && theByte[ 5 ][ 3 ] ==  0
  75. && theByte[ 6 ][ 3 ] ==  0  && theByte[ 7 ][ 3 ] ==  0
  76. && theByte[ 8 ][ 3 ] ==  0  && theByte[ 9 ][ 3 ] ==  0
  77. && theByte[ 0 ][ 4 ] ==  0  && theByte[ 1 ][ 4 ] ==  0
  78. && theByte[ 2 ][ 4 ] ==  0  && theByte[ 3 ][ 4 ] ==  0
  79. && theByte[ 4 ][ 4 ] ==  0  && theByte[ 5 ][ 4 ] ==  0
  80. && theByte[ 6 ][ 4 ] ==  0  && theByte[ 7 ][ 4 ] ==  0
  81. && theByte[ 8 ][ 4 ] ==  0  && theByte[ 9 ][ 4 ] ==  0 ) {
  82. return   1 ;
  83. }  else   if  (theByte[ 0 ][ 5 ] ==  0  && theByte[ 1 ][ 5 ] ==  0
  84. && theByte[ 2 ][ 5 ] ==  0  && theByte[ 3 ][ 5 ] ==  0
  85. && theByte[ 4 ][ 5 ] ==  0  && theByte[ 5 ][ 5 ] ==  0
  86. && theByte[ 6 ][ 5 ] ==  0  && theByte[ 7 ][ 5 ] ==  0
  87. && theByte[ 8 ][ 5 ] ==  0  && theByte[ 9 ][ 5 ] ==  0
  88. && theByte[ 0 ][ 6 ] ==  0  && theByte[ 1 ][ 6 ] ==  0
  89. && theByte[ 2 ][ 6 ] ==  0  && theByte[ 3 ][ 6 ] ==  0
  90. && theByte[ 4 ][ 6 ] ==  0  && theByte[ 5 ][ 6 ] ==  0
  91. && theByte[ 6 ][ 6 ] ==  0  && theByte[ 7 ][ 6 ] ==  0
  92. && theByte[ 8 ][ 6 ] ==  0  && theByte[ 9 ][ 6 ] ==  0 ) {
  93. return   4 ;
  94. }  else   if  (theByte[ 5 ][ 0 ] ==  0  && theByte[ 6 ][ 0 ] ==  0
  95. && theByte[ 7 ][ 0 ] ==  0  && theByte[ 8 ][ 0 ] ==  0
  96. && theByte[ 9 ][ 0 ] ==  0  && theByte[ 5 ][ 1 ] ==  0
  97. && theByte[ 6 ][ 1 ] ==  0  && theByte[ 7 ][ 1 ] ==  0
  98. && theByte[ 8 ][ 1 ] ==  0  && theByte[ 9 ][ 1 ] ==  0
  99. && theByte[ 9 ][ 2 ] ==  0  && theByte[ 9 ][ 3 ] ==  0
  100. && theByte[ 9 ][ 4 ] ==  0  && theByte[ 9 ][ 5 ] ==  0
  101. && theByte[ 9 ][ 6 ] ==  0  && theByte[ 2 ][ 6 ] ==  0
  102. && theByte[ 3 ][ 6 ] ==  0  && theByte[ 4 ][ 6 ] ==  0
  103. && theByte[ 5 ][ 6 ] ==  0  && theByte[ 2 ][ 7 ] ==  0 ) {
  104. return   5 ;
  105. }  else   if  (theByte[ 2 ][ 0 ] ==  0  && theByte[ 3 ][ 0 ] ==  0
  106. && theByte[ 4 ][ 0 ] ==  0  && theByte[ 5 ][ 0 ] ==  0
  107. && theByte[ 6 ][ 0 ] ==  0  && theByte[ 7 ][ 0 ] ==  0
  108. && theByte[ 1 ][ 1 ] ==  0  && theByte[ 2 ][ 1 ] ==  0
  109. && theByte[ 3 ][ 1 ] ==  0  && theByte[ 4 ][ 1 ] ==  0
  110. && theByte[ 5 ][ 1 ] ==  0  && theByte[ 6 ][ 1 ] ==  0
  111. && theByte[ 7 ][ 1 ] ==  0  && theByte[ 8 ][ 1 ] ==  0 ) {
  112. return   6 ;
  113. }  else   if  (theByte[ 9 ][ 0 ] ==  0  && theByte[ 9 ][ 1 ] ==  0
  114. && theByte[ 9 ][ 2 ] ==  0  && theByte[ 9 ][ 3 ] ==  00
  115. && theByte[ 9 ][ 4 ] ==  0  && theByte[ 9 ][ 5 ] ==  0
  116. && theByte[ 9 ][ 6 ] ==  0  && theByte[ 9 ][ 7 ] ==  0
  117. && theByte[ 8 ][ 7 ] ==  0  && theByte[ 8 ][ 6 ] ==  0
  118. && theByte[ 7 ][ 6 ] ==  0  && theByte[ 7 ][ 7 ] ==  0
  119. && theByte[ 6 ][ 6 ] ==  0  && theByte[ 6 ][ 5 ] ==  0
  120. && theByte[ 5 ][ 5 ] ==  0  && theByte[ 5 ][ 4 ] ==  0
  121. && theByte[ 4 ][ 4 ] ==  0 ) {
  122. return   7 ;
  123. }  else   if  (theByte[ 2 ][ 0 ] ==  0  && theByte[ 3 ][ 0 ] ==  0
  124. && theByte[ 7 ][ 0 ] ==  0  && theByte[ 1 ][ 1 ] ==  0
  125. && theByte[ 2 ][ 1 ] ==  0  && theByte[ 3 ][ 1 ] ==  0
  126. && theByte[ 4 ][ 1 ] ==  0  && theByte[ 0 ][ 2 ] ==  0
  127. && theByte[ 0 ][ 3 ] ==  0  && theByte[ 0 ][ 4 ] ==  0
  128. && theByte[ 1 ][ 5 ] ==  0  && theByte[ 1 ][ 6 ] ==  0
  129. && theByte[ 1 ][ 6 ] ==  0  && theByte[ 4 ][ 2 ] ==  0
  130. && theByte[ 5 ][ 2 ] ==  0  && theByte[ 6 ][ 2 ] ==  0 ) {
  131. return   8 ;
  132. }  else   if  (theByte[ 1 ][ 6 ] ==  0  && theByte[ 2 ][ 6 ] ==  0
  133. && theByte[ 3 ][ 6 ] ==  0  && theByte[ 4 ][ 6 ] ==  0
  134. && theByte[ 5 ][ 6 ] ==  0  && theByte[ 6 ][ 6 ] ==  0
  135. && theByte[ 2 ][ 7 ] ==  0  && theByte[ 3 ][ 7 ] ==  0
  136. && theByte[ 4 ][ 7 ] ==  0  && theByte[ 5 ][ 7 ] ==  0
  137. && theByte[ 6 ][ 7 ] ==  0  && theByte[ 7 ][ 7 ] ==  0
  138. && theByte[ 5 ][ 5 ] ==  0  && theByte[ 4 ][ 2 ] ==  0
  139. && theByte[ 4 ][ 3 ] ==  0  && theByte[ 4 ][ 4 ] ==  0
  140. && theByte[ 2 ][ 1 ] ==  0 ) {
  141. return   9 ;
  142. }  else  {
  143. return   0 ;
  144. }
  145. }
  146. private  String parse4Num( byte [][] theByte) {
  147. int  x =  0 ;
  148. int  y =  0 ;
  149. String num;
  150. byte [][] numByte =  new   byte [ 10 ][ 8 ];
  151. for  ( int  i =  5 ; i <  15 ; i++) {
  152. for  ( int  j =  5 ; j <  13 ; j++) {
  153. numByte[x][y] = theByte[i][j];
  154. y++;
  155. }
  156. x++;
  157. y =  0 ;
  158. }
  159. num =  ""  + parse1Num(numByte);
  160. x =  0 ;
  161. y =  0 ;
  162. numByte =  new   byte [ 10 ][ 8 ];
  163. for  ( int  i =  5 ; i <  15 ; i++) {
  164. for  ( int  j =  14 ; j <  22 ; j++) {
  165. numByte[x][y] = theByte[i][j];
  166. y++;
  167. }
  168. x++;
  169. y =  0 ;
  170. }
  171. num += parse1Num(numByte);
  172. x =  0 ;
  173. y =  0 ;
  174. numByte =  new   byte [ 10 ][ 8 ];
  175. for  ( int  i =  5 ; i <  15 ; i++) {
  176. for  ( int  j =  23 ; j <  31 ; j++) {
  177. numByte[x][y] = theByte[i][j];
  178. y++;
  179. }
  180. x++;
  181. y =  0 ;
  182. }
  183. num += parse1Num(numByte);
  184. x =  0 ;
  185. y =  0 ;
  186. numByte =  new   byte [ 10 ][ 8 ];
  187. for  ( int  i =  5 ; i <  15 ; i++) {
  188. for  ( int  j =  32 ; j <  40 ; j++) {
  189. numByte[x][y] = theByte[i][j];
  190. y++;
  191. }
  192. x++;
  193. y =  0 ;
  194. }
  195. num += parse1Num(numByte);
  196. return  num;
  197. }
  198. /**
  199. * @param args
  200. * @throws IOException
  201. */
  202. public   static   void  main(String[] args) {
  203. BMPTest t =  new  BMPTest();
  204. t.handler( "D:\\eclipse2\\workspace\\BMPTest\\classes\\9834.bmp" );
  205. }
  206. public   void  handler(String fileName) {
  207. File bmpFile =  new  File(fileName);
  208. FileInputStream fis;
  209. BMPHeader header =  new  BMPHeader();
  210. BMPTest bt =  new  BMPTest();
  211. try  {
  212. fis =  new  FileInputStream(bmpFile);
  213. fis.read(header.bm);
  214. fis.read(header.size);
  215. fis.read(header.save);
  216. fis.read(header.dataPos);
  217. fis.read(header.desSize);
  218. fis.read(header.width);
  219. fis.read(header.hight);
  220. fis.read(header.plane);
  221. fis.read(header.bytes);
  222. fis.read(header.zip);
  223. fis.read(header.picSize);
  224. fis.read(header.shuiPing);
  225. fis.read(header.cuiZhi);
  226. fis.read(header.colorNum);
  227. fis.read(header.colorTab);
  228. fis.read(header.bmpData);
  229. }  catch  (FileNotFoundException e) {
  230. e.printStackTrace();
  231. }  catch  (IOException e) {
  232. e.printStackTrace();
  233. }
  234. System.out.println( "The Picture is:"
  235. + bt.parse4Num(bt.parse960ToLine(header.bmpData)));
  236. }
  237. }

随机码图片格式确定 每位数字的位置都是确定的。。 现在网站上的随机码换成彩色的了

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。