java 代码
- 做了个刷投票的程序 识别随即码图片的地方用到了BMP图片识别 需要识别出BMP图片里的数字
- 网站的BMP图片格式比较规则 20 * 45 的大小 每个数字大小是 10 * 8 左侧空 5 像素 右侧 5 像素
- 研究BMP图片格式发现 BMP主要包括 3 部分 头 颜色表 图像数据区
- 图像结构如下 类似C++里的结构体
- public class BMPHeader {
- public byte [] bm = new byte [ 2 ]; // 2位 BM
- public byte [] size = new byte [ 4 ]; // 4位 2038 (f6 07)
- public byte [] save = new byte [ 4 ]; // 4位 00
- public byte [] dataPos = new byte [ 4 ]; // 4位 图像数据区位置
- // (3604)文件头信息块大小,图像描述信息块的大小,图像颜色表的大小,保留(为01)
- public byte [] desSize = new byte [ 4 ]; // 4位图像描述信息块大小28h
- public byte [] width = new byte [ 4 ]; // 4位 宽度
- public byte [] hight = new byte [ 4 ]; // 4位 高度
- public byte [] plane = new byte [ 2 ]; // 2位 plane为0100
- public byte [] bytes = new byte [ 2 ]; // 2位 图像的位数 0800
- public byte [] zip = new byte [ 4 ]; // 4位 压缩方式00不压缩
- public byte [] picSize = new byte [ 4 ]; // 4位 c0 03 = 960 图像区数据的大小
- public byte [] shuiPing = new byte [ 4 ]; // 4位 水平每米有多少像素
- public byte [] cuiZhi = new byte [ 4 ]; // 4位 垂直每米有多少像素
- public byte [] colorNum = new byte [ 4 ]; // 4位 此图像所用的颜色数
- // 图像数据从436-7f5
- public byte [] colorTab = new byte [ 1028 ];
- public byte [] bmpData = new byte [ 960 ];
- }
- 颜色表ColorTab应该为 1024 长 不知道为什么会是 1028 没算明白这里
- 图像数据区是 960 位 每一位都是一个颜色点 保存颜色表中的索引数据 16 进制 3 行表示图像中的一行
- 从下往上排列 45 = 16 * 3 - 3 有 3 位补零 因为bmp要保证每行数据点要为 4 的整数倍
- 得到数据内容就可以根据行列坐标定位每一点的颜色 从而判断出图像中的数字
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- public class BMPTest {
- private byte [][] parse960ToLine( byte [] bmpData) {
- int lineSize = 48 ;
- int columnIndex = 0 ;
- int rowIndex = 0 ;
- byte [][] bmp = new byte [ 20 ][ 48 ];
- for ( int i = 0 ; i < bmpData.length; i++) {
- bmp[rowIndex][columnIndex] = bmpData[i];
- columnIndex++;
- if (columnIndex == 48 ) {
- rowIndex++;
- columnIndex = 0 ;
- }
- }
- return bmp;
- }
- private short parse1Num( byte [][] theByte) {
- if (theByte[ 0 ][ 0 ] == 0 && theByte[ 0 ][ 1 ] == 0 && theByte[ 0 ][ 2 ] == 0
- && theByte[ 0 ][ 3 ] == 0 && theByte[ 0 ][ 4 ] == 0
- && theByte[ 0 ][ 5 ] == 0 && theByte[ 0 ][ 6 ] == 0
- && theByte[ 0 ][ 7 ] == 0 && theByte[ 7 ][ 0 ] == 0
- && theByte[ 7 ][ 1 ] == 0 && theByte[ 1 ][ 1 ] == 0
- && theByte[ 1 ][ 2 ] == 0 && theByte[ 2 ][ 2 ] == 0
- && theByte[ 2 ][ 3 ] == 0 && theByte[ 3 ][ 3 ] == 0
- && theByte[ 3 ][ 4 ] == 0 && theByte[ 4 ][ 4 ] == 0 ) {
- return 2 ;
- } else if (theByte[ 0 ][ 0 ] == 3 && theByte[ 0 ][ 1 ] == 0
- && theByte[ 0 ][ 2 ] == 0 && theByte[ 0 ][ 3 ] == 0
- && theByte[ 0 ][ 4 ] == 0 && theByte[ 1 ][ 0 ] == 0
- && theByte[ 1 ][ 1 ] == 0 && theByte[ 1 ][ 2 ] == 3
- && theByte[ 9 ][ 1 ] == 0 && theByte[ 9 ][ 2 ] == 0
- && theByte[ 9 ][ 3 ] == 0 && theByte[ 9 ][ 4 ] == 0
- && theByte[ 9 ][ 5 ] == 0 && theByte[ 8 ][ 0 ] == 0
- && theByte[ 8 ][ 1 ] == 0 && theByte[ 8 ][ 5 ] == 0
- && theByte[ 8 ][ 6 ] == 0 ) {
- return 3 ;
- } else if (theByte[ 0 ][ 1 ] == 0 && theByte[ 0 ][ 2 ] == 0
- && theByte[ 0 ][ 3 ] == 0 && theByte[ 0 ][ 4 ] == 0
- && theByte[ 0 ][ 5 ] == 0 && theByte[ 0 ][ 6 ] == 0
- && theByte[ 0 ][ 3 ] == 0 && theByte[ 1 ][ 3 ] == 0
- && theByte[ 2 ][ 3 ] == 0 && theByte[ 3 ][ 3 ] == 0
- && theByte[ 4 ][ 3 ] == 0 && theByte[ 5 ][ 3 ] == 0
- && theByte[ 6 ][ 3 ] == 0 && theByte[ 7 ][ 3 ] == 0
- && theByte[ 8 ][ 3 ] == 0 && theByte[ 9 ][ 3 ] == 0
- && theByte[ 0 ][ 4 ] == 0 && theByte[ 1 ][ 4 ] == 0
- && theByte[ 2 ][ 4 ] == 0 && theByte[ 3 ][ 4 ] == 0
- && theByte[ 4 ][ 4 ] == 0 && theByte[ 5 ][ 4 ] == 0
- && theByte[ 6 ][ 4 ] == 0 && theByte[ 7 ][ 4 ] == 0
- && theByte[ 8 ][ 4 ] == 0 && theByte[ 9 ][ 4 ] == 0 ) {
- return 1 ;
- } else if (theByte[ 0 ][ 5 ] == 0 && theByte[ 1 ][ 5 ] == 0
- && theByte[ 2 ][ 5 ] == 0 && theByte[ 3 ][ 5 ] == 0
- && theByte[ 4 ][ 5 ] == 0 && theByte[ 5 ][ 5 ] == 0
- && theByte[ 6 ][ 5 ] == 0 && theByte[ 7 ][ 5 ] == 0
- && theByte[ 8 ][ 5 ] == 0 && theByte[ 9 ][ 5 ] == 0
- && theByte[ 0 ][ 6 ] == 0 && theByte[ 1 ][ 6 ] == 0
- && theByte[ 2 ][ 6 ] == 0 && theByte[ 3 ][ 6 ] == 0
- && theByte[ 4 ][ 6 ] == 0 && theByte[ 5 ][ 6 ] == 0
- && theByte[ 6 ][ 6 ] == 0 && theByte[ 7 ][ 6 ] == 0
- && theByte[ 8 ][ 6 ] == 0 && theByte[ 9 ][ 6 ] == 0 ) {
- return 4 ;
- } else if (theByte[ 5 ][ 0 ] == 0 && theByte[ 6 ][ 0 ] == 0
- && theByte[ 7 ][ 0 ] == 0 && theByte[ 8 ][ 0 ] == 0
- && theByte[ 9 ][ 0 ] == 0 && theByte[ 5 ][ 1 ] == 0
- && theByte[ 6 ][ 1 ] == 0 && theByte[ 7 ][ 1 ] == 0
- && theByte[ 8 ][ 1 ] == 0 && theByte[ 9 ][ 1 ] == 0
- && theByte[ 9 ][ 2 ] == 0 && theByte[ 9 ][ 3 ] == 0
- && theByte[ 9 ][ 4 ] == 0 && theByte[ 9 ][ 5 ] == 0
- && theByte[ 9 ][ 6 ] == 0 && theByte[ 2 ][ 6 ] == 0
- && theByte[ 3 ][ 6 ] == 0 && theByte[ 4 ][ 6 ] == 0
- && theByte[ 5 ][ 6 ] == 0 && theByte[ 2 ][ 7 ] == 0 ) {
- return 5 ;
- } else if (theByte[ 2 ][ 0 ] == 0 && theByte[ 3 ][ 0 ] == 0
- && theByte[ 4 ][ 0 ] == 0 && theByte[ 5 ][ 0 ] == 0
- && theByte[ 6 ][ 0 ] == 0 && theByte[ 7 ][ 0 ] == 0
- && theByte[ 1 ][ 1 ] == 0 && theByte[ 2 ][ 1 ] == 0
- && theByte[ 3 ][ 1 ] == 0 && theByte[ 4 ][ 1 ] == 0
- && theByte[ 5 ][ 1 ] == 0 && theByte[ 6 ][ 1 ] == 0
- && theByte[ 7 ][ 1 ] == 0 && theByte[ 8 ][ 1 ] == 0 ) {
- return 6 ;
- } else if (theByte[ 9 ][ 0 ] == 0 && theByte[ 9 ][ 1 ] == 0
- && theByte[ 9 ][ 2 ] == 0 && theByte[ 9 ][ 3 ] == 00
- && theByte[ 9 ][ 4 ] == 0 && theByte[ 9 ][ 5 ] == 0
- && theByte[ 9 ][ 6 ] == 0 && theByte[ 9 ][ 7 ] == 0
- && theByte[ 8 ][ 7 ] == 0 && theByte[ 8 ][ 6 ] == 0
- && theByte[ 7 ][ 6 ] == 0 && theByte[ 7 ][ 7 ] == 0
- && theByte[ 6 ][ 6 ] == 0 && theByte[ 6 ][ 5 ] == 0
- && theByte[ 5 ][ 5 ] == 0 && theByte[ 5 ][ 4 ] == 0
- && theByte[ 4 ][ 4 ] == 0 ) {
- return 7 ;
- } else if (theByte[ 2 ][ 0 ] == 0 && theByte[ 3 ][ 0 ] == 0
- && theByte[ 7 ][ 0 ] == 0 && theByte[ 1 ][ 1 ] == 0
- && theByte[ 2 ][ 1 ] == 0 && theByte[ 3 ][ 1 ] == 0
- && theByte[ 4 ][ 1 ] == 0 && theByte[ 0 ][ 2 ] == 0
- && theByte[ 0 ][ 3 ] == 0 && theByte[ 0 ][ 4 ] == 0
- && theByte[ 1 ][ 5 ] == 0 && theByte[ 1 ][ 6 ] == 0
- && theByte[ 1 ][ 6 ] == 0 && theByte[ 4 ][ 2 ] == 0
- && theByte[ 5 ][ 2 ] == 0 && theByte[ 6 ][ 2 ] == 0 ) {
- return 8 ;
- } else if (theByte[ 1 ][ 6 ] == 0 && theByte[ 2 ][ 6 ] == 0
- && theByte[ 3 ][ 6 ] == 0 && theByte[ 4 ][ 6 ] == 0
- && theByte[ 5 ][ 6 ] == 0 && theByte[ 6 ][ 6 ] == 0
- && theByte[ 2 ][ 7 ] == 0 && theByte[ 3 ][ 7 ] == 0
- && theByte[ 4 ][ 7 ] == 0 && theByte[ 5 ][ 7 ] == 0
- && theByte[ 6 ][ 7 ] == 0 && theByte[ 7 ][ 7 ] == 0
- && theByte[ 5 ][ 5 ] == 0 && theByte[ 4 ][ 2 ] == 0
- && theByte[ 4 ][ 3 ] == 0 && theByte[ 4 ][ 4 ] == 0
- && theByte[ 2 ][ 1 ] == 0 ) {
- return 9 ;
- } else {
- return 0 ;
- }
- }
- private String parse4Num( byte [][] theByte) {
- int x = 0 ;
- int y = 0 ;
- String num;
- byte [][] numByte = new byte [ 10 ][ 8 ];
- for ( int i = 5 ; i < 15 ; i++) {
- for ( int j = 5 ; j < 13 ; j++) {
- numByte[x][y] = theByte[i][j];
- y++;
- }
- x++;
- y = 0 ;
- }
- num = "" + parse1Num(numByte);
- x = 0 ;
- y = 0 ;
- numByte = new byte [ 10 ][ 8 ];
- for ( int i = 5 ; i < 15 ; i++) {
- for ( int j = 14 ; j < 22 ; j++) {
- numByte[x][y] = theByte[i][j];
- y++;
- }
- x++;
- y = 0 ;
- }
- num += parse1Num(numByte);
- x = 0 ;
- y = 0 ;
- numByte = new byte [ 10 ][ 8 ];
- for ( int i = 5 ; i < 15 ; i++) {
- for ( int j = 23 ; j < 31 ; j++) {
- numByte[x][y] = theByte[i][j];
- y++;
- }
- x++;
- y = 0 ;
- }
- num += parse1Num(numByte);
- x = 0 ;
- y = 0 ;
- numByte = new byte [ 10 ][ 8 ];
- for ( int i = 5 ; i < 15 ; i++) {
- for ( int j = 32 ; j < 40 ; j++) {
- numByte[x][y] = theByte[i][j];
- y++;
- }
- x++;
- y = 0 ;
- }
- num += parse1Num(numByte);
- return num;
- }
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) {
- BMPTest t = new BMPTest();
- t.handler( "D:\\eclipse2\\workspace\\BMPTest\\classes\\9834.bmp" );
- }
- public void handler(String fileName) {
- File bmpFile = new File(fileName);
- FileInputStream fis;
- BMPHeader header = new BMPHeader();
- BMPTest bt = new BMPTest();
- try {
- fis = new FileInputStream(bmpFile);
- fis.read(header.bm);
- fis.read(header.size);
- fis.read(header.save);
- fis.read(header.dataPos);
- fis.read(header.desSize);
- fis.read(header.width);
- fis.read(header.hight);
- fis.read(header.plane);
- fis.read(header.bytes);
- fis.read(header.zip);
- fis.read(header.picSize);
- fis.read(header.shuiPing);
- fis.read(header.cuiZhi);
- fis.read(header.colorNum);
- fis.read(header.colorTab);
- fis.read(header.bmpData);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println( "The Picture is:"
- + bt.parse4Num(bt.parse960ToLine(header.bmpData)));
- }
- }
随机码图片格式确定 每位数字的位置都是确定的。。 现在网站上的随机码换成彩色的了