---
title: "关于BMP图片格式(部分垃圾代码)"
author: "wangshu3000"
date: "2007-11-17T19:05:38Z"
tags: []
categories: ["编程语言"]
slug: "bmp"
draft: false
lastmod: "2011-02-18T11:08:17Z"
id: 141405
url: "https://mid.iteye.com/blog/141405"
---

java 代码

001. 做了个刷投票的程序 识别随即码图片的地方用到了BMP图片识别 需要识别出BMP图片里的数字
002. 网站的BMP图片格式比较规则  20 * 45 的大小 每个数字大小是 10 * 8  左侧空 5 像素 右侧 5 像素
003. 研究BMP图片格式发现 BMP主要包括 3 部分 头 颜色表 图像数据区
004. 图像结构如下 类似C++里的结构体
005. public   class  BMPHeader {
006. public   byte \[] bm =  new   byte \[ 2 ]; // 2位 BM
007. public   byte \[] size =  new   byte \[ 4 ]; // 4位 2038 (f6 07)
008. public   byte \[] save =  new   byte \[ 4 ]; // 4位 00
009. public   byte \[] dataPos =  new   byte \[ 4 ]; // 4位 图像数据区位置
010. // (3604)文件头信息块大小，图像描述信息块的大小，图像颜色表的大小，保留(为01)
011. public   byte \[] desSize =  new   byte \[ 4 ]; // 4位图像描述信息块大小28h
012. public   byte \[] width =  new   byte \[ 4 ]; // 4位 宽度
013. public   byte \[] hight =  new   byte \[ 4 ]; // 4位 高度
014. public   byte \[] plane =  new   byte \[ 2 ]; // 2位 plane为0100
015. public   byte \[] bytes =  new   byte \[ 2 ]; // 2位 图像的位数 0800
016. public   byte \[] zip =  new   byte \[ 4 ]; // 4位 压缩方式00不压缩
017. public   byte \[] picSize =  new   byte \[ 4 ]; // 4位 c0 03 = 960 图像区数据的大小
018. public   byte \[] shuiPing =  new   byte \[ 4 ]; // 4位 水平每米有多少像素
019. public   byte \[] cuiZhi =  new   byte \[ 4 ]; // 4位 垂直每米有多少像素
020. public   byte \[] colorNum =  new   byte \[ 4 ]; // 4位 此图像所用的颜色数
021. // 图像数据从436-7f5
022. public   byte \[] colorTab =  new   byte \[ 1028 ];
023. public   byte \[] bmpData =  new   byte \[ 960 ];
024. }
025. 颜色表ColorTab应该为 1024 长 不知道为什么会是 1028  没算明白这里
026. 图像数据区是 960 位 每一位都是一个颜色点 保存颜色表中的索引数据  16 进制  3 行表示图像中的一行
027. 从下往上排列  45 = 16 * 3 - 3  有 3 位补零 因为bmp要保证每行数据点要为 4 的整数倍
028. 得到数据内容就可以根据行列坐标定位每一点的颜色 从而判断出图像中的数字
029. import  java.io.File;
030. import  java.io.FileInputStream;
031. import  java.io.FileNotFoundException;
032. import  java.io.IOException;
033. public   class  BMPTest {
034. private   byte \[]\[] parse960ToLine( byte \[] bmpData) {
035. int  lineSize =  48 ;
036. int  columnIndex =  0 ;
037. int  rowIndex =  0 ;
038. byte \[]\[] bmp =  new   byte \[ 20 ]\[ 48 ];
039. for  ( int  i =  0 ; i &lt; bmpData.length; i++) {
040. bmp\[rowIndex]\[columnIndex] = bmpData\[i];
041. columnIndex++;
042. if  (columnIndex ==  48 ) {
043. rowIndex++;
044. columnIndex =  0 ;
045. }
046. }
047. return  bmp;
048. }
049. private   short  parse1Num( byte \[]\[] theByte) {
050. if  (theByte\[ 0 ]\[ 0 ] ==  0  && theByte\[ 0 ]\[ 1 ] ==  0  && theByte\[ 0 ]\[ 2 ] ==  0
051. && theByte\[ 0 ]\[ 3 ] ==  0  && theByte\[ 0 ]\[ 4 ] ==  0
052. && theByte\[ 0 ]\[ 5 ] ==  0  && theByte\[ 0 ]\[ 6 ] ==  0
053. && theByte\[ 0 ]\[ 7 ] ==  0  && theByte\[ 7 ]\[ 0 ] ==  0
054. && theByte\[ 7 ]\[ 1 ] ==  0  && theByte\[ 1 ]\[ 1 ] ==  0
055. && theByte\[ 1 ]\[ 2 ] ==  0  && theByte\[ 2 ]\[ 2 ] ==  0
056. && theByte\[ 2 ]\[ 3 ] ==  0  && theByte\[ 3 ]\[ 3 ] ==  0
057. && theByte\[ 3 ]\[ 4 ] ==  0  && theByte\[ 4 ]\[ 4 ] ==  0 ) {
058. return   2 ;
059. }  else   if  (theByte\[ 0 ]\[ 0 ] ==  3  && theByte\[ 0 ]\[ 1 ] ==  0
060. && theByte\[ 0 ]\[ 2 ] ==  0  && theByte\[ 0 ]\[ 3 ] ==  0
061. && theByte\[ 0 ]\[ 4 ] ==  0  && theByte\[ 1 ]\[ 0 ] ==  0
062. && theByte\[ 1 ]\[ 1 ] ==  0  && theByte\[ 1 ]\[ 2 ] ==  3
063. && theByte\[ 9 ]\[ 1 ] ==  0  && theByte\[ 9 ]\[ 2 ] ==  0
064. && theByte\[ 9 ]\[ 3 ] ==  0  && theByte\[ 9 ]\[ 4 ] ==  0
065. && theByte\[ 9 ]\[ 5 ] ==  0  && theByte\[ 8 ]\[ 0 ] ==  0
066. && theByte\[ 8 ]\[ 1 ] ==  0  && theByte\[ 8 ]\[ 5 ] ==  0
067. && theByte\[ 8 ]\[ 6 ] ==  0 ) {
068. return   3 ;
069. }  else   if  (theByte\[ 0 ]\[ 1 ] ==  0  && theByte\[ 0 ]\[ 2 ] ==  0
070. && theByte\[ 0 ]\[ 3 ] ==  0  && theByte\[ 0 ]\[ 4 ] ==  0
071. && theByte\[ 0 ]\[ 5 ] ==  0  && theByte\[ 0 ]\[ 6 ] ==  0
072. && theByte\[ 0 ]\[ 3 ] ==  0  && theByte\[ 1 ]\[ 3 ] ==  0
073. && theByte\[ 2 ]\[ 3 ] ==  0  && theByte\[ 3 ]\[ 3 ] ==  0
074. && theByte\[ 4 ]\[ 3 ] ==  0  && theByte\[ 5 ]\[ 3 ] ==  0
075. && theByte\[ 6 ]\[ 3 ] ==  0  && theByte\[ 7 ]\[ 3 ] ==  0
076. && theByte\[ 8 ]\[ 3 ] ==  0  && theByte\[ 9 ]\[ 3 ] ==  0
077. && theByte\[ 0 ]\[ 4 ] ==  0  && theByte\[ 1 ]\[ 4 ] ==  0
078. && theByte\[ 2 ]\[ 4 ] ==  0  && theByte\[ 3 ]\[ 4 ] ==  0
079. && theByte\[ 4 ]\[ 4 ] ==  0  && theByte\[ 5 ]\[ 4 ] ==  0
080. && theByte\[ 6 ]\[ 4 ] ==  0  && theByte\[ 7 ]\[ 4 ] ==  0
081. && theByte\[ 8 ]\[ 4 ] ==  0  && theByte\[ 9 ]\[ 4 ] ==  0 ) {
082. return   1 ;
083. }  else   if  (theByte\[ 0 ]\[ 5 ] ==  0  && theByte\[ 1 ]\[ 5 ] ==  0
084. && theByte\[ 2 ]\[ 5 ] ==  0  && theByte\[ 3 ]\[ 5 ] ==  0
085. && theByte\[ 4 ]\[ 5 ] ==  0  && theByte\[ 5 ]\[ 5 ] ==  0
086. && theByte\[ 6 ]\[ 5 ] ==  0  && theByte\[ 7 ]\[ 5 ] ==  0
087. && theByte\[ 8 ]\[ 5 ] ==  0  && theByte\[ 9 ]\[ 5 ] ==  0
088. && theByte\[ 0 ]\[ 6 ] ==  0  && theByte\[ 1 ]\[ 6 ] ==  0
089. && theByte\[ 2 ]\[ 6 ] ==  0  && theByte\[ 3 ]\[ 6 ] ==  0
090. && theByte\[ 4 ]\[ 6 ] ==  0  && theByte\[ 5 ]\[ 6 ] ==  0
091. && theByte\[ 6 ]\[ 6 ] ==  0  && theByte\[ 7 ]\[ 6 ] ==  0
092. && theByte\[ 8 ]\[ 6 ] ==  0  && theByte\[ 9 ]\[ 6 ] ==  0 ) {
093. return   4 ;
094. }  else   if  (theByte\[ 5 ]\[ 0 ] ==  0  && theByte\[ 6 ]\[ 0 ] ==  0
095. && theByte\[ 7 ]\[ 0 ] ==  0  && theByte\[ 8 ]\[ 0 ] ==  0
096. && theByte\[ 9 ]\[ 0 ] ==  0  && theByte\[ 5 ]\[ 1 ] ==  0
097. && theByte\[ 6 ]\[ 1 ] ==  0  && theByte\[ 7 ]\[ 1 ] ==  0
098. && theByte\[ 8 ]\[ 1 ] ==  0  && theByte\[ 9 ]\[ 1 ] ==  0
099. && 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 &lt;  15 ; i++) {
152. for  ( int  j =  5 ; j &lt;  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 &lt;  15 ; i++) {
164. for  ( int  j =  14 ; j &lt;  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 &lt;  15 ; i++) {
176. for  ( int  j =  23 ; j &lt;  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 &lt;  15 ; i++) {
188. for  ( int  j =  32 ; j &lt;  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. }

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