278 lines
6.2 KiB
Java
278 lines
6.2 KiB
Java
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.PrintStream;
|
|
|
|
public class Word {
|
|
public final static String ZK_PATH = "/HZK16";
|
|
public final static String ENCODE = "GB2312";
|
|
private int[] unit = new int[32];
|
|
private String yes = "$";// "■";
|
|
private String no = " ";
|
|
private PrintStream print = null;
|
|
public Word() {
|
|
// String str = "我";
|
|
//
|
|
// try {
|
|
//
|
|
// byte[] b = str.getBytes("GB2312");
|
|
//
|
|
// for (int i = 0; i < b.length; i++) {
|
|
//
|
|
// System.out.println(Integer.toHexString(b[i]));
|
|
//
|
|
// }
|
|
//
|
|
// } catch (Exception ex) {
|
|
//
|
|
// }
|
|
|
|
}
|
|
|
|
public void setYes(String str) {
|
|
yes = str;
|
|
}
|
|
|
|
public void setNo(String str) {
|
|
no = str;
|
|
}
|
|
|
|
private int negativeToPlus(byte b) {
|
|
return b & 0xFF;
|
|
}
|
|
|
|
public void setOutputFile(String filename) throws Exception {
|
|
print = new PrintStream(filename);
|
|
}
|
|
|
|
public void colseFileStream() {
|
|
print.close();
|
|
}
|
|
|
|
private void readChina(char ch) { // 需要得到点阵的汉字
|
|
byte[] buf = new byte[32];
|
|
FileInputStream input = null;
|
|
try {
|
|
if (ch < 0x80) {
|
|
long offset = (ch + 155) * 32; // 获得偏移量
|
|
File file = new File("HZK16");
|
|
input = new FileInputStream(file);
|
|
input.skip(offset);
|
|
input.read(buf, 0, 32);
|
|
for (int i = 0; i < 32; i++)
|
|
unit[i] = negativeToPlus(buf[i]);
|
|
input.close();
|
|
}
|
|
if (ch > 0x80) {
|
|
String string = Character.toString(ch);
|
|
byte[] bt = string.getBytes("GBK"); // 获得国标码
|
|
int a1 = negativeToPlus(bt[0]); // 转为无符号整数
|
|
int a2 = negativeToPlus(bt[1]);
|
|
int qh = a1 - 0xA0; // 得到区位码
|
|
int wh = a2 - 0xA0;
|
|
long offset = (94 * (qh - 1) + (wh - 1)) * 32; // 获得偏移量
|
|
|
|
File file = new File("HZK16");
|
|
input = new FileInputStream(file);
|
|
input.skip(offset);
|
|
input.read(buf, 0, 32);
|
|
for (int i = 0; i < 32; i++)
|
|
unit[i] = negativeToPlus(buf[i]);
|
|
input.close();
|
|
}
|
|
// String string = Character.toString(ch);
|
|
// byte[] bt = string.getBytes("GBK"); // 获得国标码
|
|
// int a1 = negativeToPlus(bt[0]); // 转为无符号整数
|
|
// int a2 = negativeToPlus(bt[1]);
|
|
// int qh = a1 - 0xA0; // 得到区位码
|
|
// int wh = a2 - 0xA0;
|
|
// long offset = (94 * (qh - 1) + (wh - 1)) * 32; // 获得偏移量
|
|
//
|
|
// File file = new File("HZK16");
|
|
// input = new FileInputStream(file);
|
|
// input.skip(offset);
|
|
// input.read(buf, 0, 32);
|
|
// for (int i = 0; i < 32; i++)
|
|
// unit[i] = negativeToPlus(buf[i]);
|
|
// input.close();
|
|
} catch (Exception e) {
|
|
System.out.println("文件异常");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void writeChina(char ch) { // 参数: yes是有点处显示 no是无点处显示
|
|
int i, j, k;
|
|
readChina(ch);
|
|
for (j = 0; j < 16; j++) {
|
|
for (i = 0; i < 2; i++)
|
|
for (k = 0; k < 8; k++)
|
|
if ((unit[j * 2 + i] & (0x80 >> k)) >= 1) // 取bit位值
|
|
{
|
|
System.out.print(yes);
|
|
} else {
|
|
System.out.print(no);
|
|
}
|
|
System.out.println();
|
|
}
|
|
}
|
|
|
|
public void writeChinaToFile_V(String str) {
|
|
try {
|
|
int i, j, k;
|
|
for (int x = 0; x < str.length(); x++) {
|
|
readChina(str.charAt(x));
|
|
for (j = 0; j < 16; j++) {
|
|
for (i = 0; i < 2; i++) {
|
|
for (k = 0; k < 8; k++) {
|
|
if ((unit[j * 2 + i] & (0x80 >> k)) >= 1) // 取bit位值
|
|
{
|
|
print.print(yes);
|
|
} else {
|
|
print.print(no);
|
|
}
|
|
}
|
|
}
|
|
print.println();
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void writeChinaToFile_H(String str) {
|
|
try {
|
|
int i, j, k;
|
|
{
|
|
for (j = 0; j < 16; j++) {
|
|
print.print(" ");
|
|
// 输出每个字的同一行///////////////
|
|
for (int x = 0; x < str.length(); x++) {
|
|
readChina(str.charAt(x));
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
for (k = 0; k < 8; k++) {
|
|
if ((unit[j * 2 + i] & (0x80 >> k)) >= 1) // 取bit位值
|
|
{
|
|
print.print(yes);
|
|
System.out.print(yes);
|
|
} else {
|
|
print.print(no);
|
|
System.out.print(no);
|
|
}
|
|
}
|
|
}
|
|
print.print(" ");
|
|
System.out.print(" ");
|
|
}
|
|
print.println();
|
|
System.out.println();
|
|
// //////////////////////////////////
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
Word charcode = new Word();
|
|
String str = "ALIPAY→∞";
|
|
String str2 = "支付梦想任你飞翔";
|
|
charcode.setOutputFile("output.txt"); // 确定输出 文件流
|
|
charcode.writeChinaToFile_H(str);
|
|
charcode.writeChinaToFile_H(str2); // 要写的字符串
|
|
|
|
|
|
charcode.colseFileStream(); // 关闭流
|
|
|
|
|
|
|
|
|
|
// int code[] = null;
|
|
// byte data[] = null;
|
|
// Word getcode = new Word();
|
|
// code = getcode.getByteCode("我");
|
|
// data = getcode.read(code[0], code[1]);
|
|
// for (int i = 0; i < 32; i++) {
|
|
// System.out.println(data[i]);
|
|
// }
|
|
|
|
}
|
|
}
|
|
// public int[] getByteCode(String str) {
|
|
//
|
|
// int[] byteCode = new int[2];
|
|
//
|
|
// try {
|
|
//
|
|
// byte[] data = str.getBytes(ENCODE);
|
|
//
|
|
// byteCode[0] = data[0] < 0 ? 256 + data[0] : data[0];
|
|
//
|
|
// byteCode[1] = data[1] < 0 ? 256 + data[1] : data[1];
|
|
//
|
|
// } catch (Exception ex) {
|
|
//
|
|
// }
|
|
//
|
|
// return byteCode;
|
|
//
|
|
// }
|
|
//
|
|
// public byte[] read(int areaCode, int posCode) {
|
|
//
|
|
// byte[] data = null;
|
|
// try {
|
|
// int area = areaCode - 0xa0;// 获得真实区码
|
|
// int pos = posCode - 0xa0;// 获得真实位码
|
|
// InputStream in = getClass().getResourceAsStream(ZK_PATH);
|
|
// long offset = 32 * ((area - 1) * 94 + pos - 1);
|
|
// in.skip(offset);
|
|
// data = new byte[32];
|
|
// in.read(data, 0, 32);
|
|
// in.close();
|
|
// } catch (Exception ex) {
|
|
//
|
|
// }
|
|
//
|
|
// return data;
|
|
//
|
|
// }
|
|
|
|
// protected void drawString(Graphics g, String str, int x, int y, int
|
|
// color) {
|
|
//
|
|
// byte[] data = null;
|
|
// int[] code = null;
|
|
// int byteCount;// 到点阵数据的第几个字节了
|
|
// int lCount;// 控制列
|
|
// g.setColor(color);
|
|
// for (int i = 0; i < str.length(); i++) {
|
|
// if (str.charAt(i) < 0x80) {// 非中文
|
|
// g.drawString(str.substring(i, i + 1), x + (i << 4), y, 0);
|
|
// continue;
|
|
// }
|
|
//
|
|
// code = getByteCode(str.substring(i, i + 1));
|
|
// data = read(code[0], code[1]);
|
|
// byteCount = 0;
|
|
// for (int line = 0; line < 16; line++) {
|
|
// lCount = 0;
|
|
// for (int k = 0; k < 2; k++) {
|
|
// for (int j = 0; j < 8; j++) {
|
|
// if ((data[byteCount] & mask[j]) == mask[j]) {
|
|
// g.drawLine(x + lCount + (i << 4), y + line, x
|
|
// + lCount + (i << 4), y + line);
|
|
// }
|
|
//
|
|
// lCount++;
|
|
//
|
|
// }
|
|
// byteCount++;
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|