String byteToString = "";
//将19968到40869的汉字都搞一遍.
for (int i = 19968; i < 40869; i++) {
byte byte1 = (byte) (224 + (i >> 12));
byte byte2 = (byte) ((i >> 6 & 0x3f) + 128);
byte byte3 = (byte) ((i & 0x3f) + 128);
byte[] bytes = { byte1, byte2, byte3 };
//因为utf8中, 汉字是3个byte的, 这3个byte有特定的编码方法, 在wiki上可以查到, 简单说就是110xxxxx, 1xxxxxxx ,1xxxxxxx
try {
byteToString = new String(bytes, "UTF8");
//将上面的3个byte, 生成一个汉字的String
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jpanel.changeText(byteToString);
System.out.println(byteToString);
jpanel.updateUI();
//刷新jpanel, 把汉字写上去
try {
Thread.sleep(1000);
//睡1秒, 刷汉字需要时间
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
captureImage();
//截图
try {
Thread.sleep(1000);
//分析也是需要时间的.
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// checkBMP();
String resultStr = i + " " + byteToString + " " + checkBMP();
//分析.
try {
write2file.write2File(resultStr);
//分析完之后, 写文件.
} catch (IOException e) {
e.printStackTrace();
}
}
把上面的代码放在一个init()方法中, 就可以自动开始工作了, 搞20000多个汉字, 用了十几个小时.
截图的方法:
public void captureImage() {
fullScreenImage = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
pickedImage = fullScreenImage.getSubimage(508, 516, 16, 16);
try {
ImageIO.write(pickedImage, "bmp", new File("c:\\temp\\temp.bmp"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
分析bmp:
public String checkBMP() {
try {
FileInputStream fs = new FileInputStream(new File("c:\\temp\\temp.bmp"));
int bflen = 14; // 14 byte BITMAPFILEHEADER
byte bf[] = new byte[bflen];
fs.read(bf, 0, bflen);
int bilen = 40; // 40-byte BITMAPINFOHEADER
byte bi[] = new byte[bilen];
fs.read(bi, 0, bilen);
// Interperet data.
// int nsize = (((int) bf[5] & 0xff) << 24) | (((int) bf[4] & 0xff)
// << 16) | (((int) bf[3] & 0xff) << 8) | (int) bf[2] & 0xff;
// System.out.println("File type is :" + (char) bf[0] + (char)
// bf[1]);
// System.out.println("Size of file is :" + nsize);
// Head info data
// int nbisize = (((int) bi[3] & 0xff) << 24) | (((int) bi[2] &
// 0xff) << 16) | (((int) bi[1] & 0xff) << 8) | (int) bi[0] & 0xff;
// System.out.println("Size of bitmapinfoheader is :" + nbisize);
int nwidth = (((int) bi[7] & 0xff) << 24) | (((int) bi[6] & 0xff) << 16) | (((int) bi[5] & 0xff) << 8) | (int) bi[4] & 0xff;
// System.out.println("Width is :" + nwidth);
int nheight = (((int) bi[11] & 0xff) << 24) | (((int) bi[10] & 0xff) << 16) | (((int) bi[9] & 0xff) << 8) | (int) bi[8] & 0xff;
// System.out.println("Height is :" + nheight);
// // number of planes in this bitmap
// int nplanes = (((int) bi[13] & 0xff) << 8) | (int) bi[12] & 0xff;
// System.out.println("Planes is :" + nplanes);
int originalBRGArray[][] = new int[nheight][nwidth];
//
// int nbitcount = (((int) bi[15] & 0xff) << 8) | (int) bi[14] &
// 0xff;
// System.out.println("BitCount is :" + nbitcount);
//
// // Look for non-zero values to indicate compression
// int ncompression = (((int) bi[19]) << 24) | (((int) bi[18]) <<
// 16) | (((int) bi[17]) << 8) | (int) bi[16];
// System.out.println("Compression is :" + ncompression);
//
int nsizeimage = (((int) bi[23] & 0xff) << 24) | (((int) bi[22] & 0xff) << 16) | (((int) bi[21] & 0xff) << 8) | (int) bi[20] & 0xff;
// System.out.println("SizeImage is :" + nsizeimage);
// int nxpm = (((int) bi[27] & 0xff) << 24) | (((int) bi[26] & 0xff)
// << 16) | (((int) bi[25] & 0xff) << 8) | (int) bi[24] & 0xff;
// System.out.println("X-Pixels per meter is :" + nxpm);
//
// int nypm = (((int) bi[31] & 0xff) << 24) | (((int) bi[30] & 0xff)
// << 16) | (((int) bi[29] & 0xff) << 8) | (int) bi[28] & 0xff;
// System.out.println("Y-Pixels per meter is :" + nypm);
// int nclrused = (((int) bi[35] & 0xff) << 24) | (((int) bi[34] &
// 0xff) << 16) | (((int) bi[33] & 0xff) << 8) | (int) bi[32] &
// 0xff;
// System.out.println("Colors used are :" + nclrused);
// int nclrimp = (((int) bi[39] & 0xff) << 24) | (((int) bi[38] &
// 0xff) << 16) | (((int) bi[37] & 0xff) << 8) | (int) bi[36] &
// 0xff;
// System.out.println("Colors important are :" + nclrimp);
// if (nsizeimage == 0) {
// nsizeimage = ((((nwidth * nbitcount) + 31) & ~31) >> 3);
// nsizeimage *= nheight;
// System.out.println("nsizeimage (backup) is" + nsizeimage);
// }
int npad = (nsizeimage / nheight) - nwidth * 3;
int ndata[] = 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;
// System.out.println("Encoded Color at (" + i + "," + j
// + ")is:" + " (R,G,B)= (" + ((int) (brgb[nindex + 2])
// & 0xff) + ","
// + ((int) brgb[nindex + 1] & 0xff) + "," + ((int)
// brgb[nindex] & 0xff) + ")");
if (((int) brgb[nindex] & 0xff) > 128) {
originalBRGArray[i][j] = 0;
} else {
originalBRGArray[i][j] = 1;
}
// System.out.println("orignalBRGArray i:" + i + " j:" + j +
// " is: " + originalBRGArray[i][j]);
nindex += 3;
}
nindex += npad;
}
String resultString = transferArray(originalBRGArray);
fs.close();
return resultString;
} catch (Exception e) {
e.printStackTrace(System.out);
System.out.println("Caught exception in loadbitmap!");
}
return null;
}
编码成每4个点用一个byte来表示亮跟灭:
private String transferArray(int originalBRGArray[][]) {
Byte[] bytes = new Byte[16 * 4];
int byteIndex = 0;
StringBuffer tmp = new StringBuffer();
for (int k = 0; k < 16 * 4; k++) {
int columnIndex = k / 4;
// System.out.println("columnIndex=" + columnIndex);
int rowIndex = (k % 4) * 4;
int a = originalBRGArray[columnIndex][15 - rowIndex];
int b = originalBRGArray[columnIndex][14 - rowIndex];
int c = originalBRGArray[columnIndex][13 - rowIndex];
int d = originalBRGArray[columnIndex][12 - rowIndex];
// bytes[k] = (byte) (a + b * 2 + c * 4 + d * 8);
int byteEachAddress = a + b * 2 + c * 4 + d * 8;
// System.out.println("a b c d = " + a + " " + b + " " + c + " " +
// d);
// System.out.println("bytes[" + k + "] is " + bytes[k]);
tmp.append(Integer.toHexString(byteEachAddress));
}
// String[] resultStrArray = new String[16 * 4];
// System.out.println(tmp);
// String resultString = swapString(tmp.toString());
String resultString = tmp.toString();
System.out.println("BMP data is " + resultString + " after transferred");
// return resultString;
return resultString;
}
有个值得注意的是bmp的编码是从最下面的一行开始记录的, 往上走的, 所以row0其实是最下面的一行.column倒是很老实.
写汉字的方法:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.setFont(new Font("simsun", Font.PLAIN, 16));
g.drawString(textBeenDraw, 100, 100);
// bmpParse.Reader(g);
// bmpParse.loadbitmap(g);
}