漢字锣吼,Ascii碼得湘,根據(jù)字庫解析成點陣
首先來了解兩個概念,
什么是點陣淘正,若干個虛實的點組成的一個矩陣摆马;
什么是字庫臼闻,點陣字庫是把每一個漢字都分成16×16或24×24個點,然后用每個點的虛實來表示漢字的輪廓囤采;
先看效果:
image1.png
image2.png
看到這里述呐,相信大家都不陌生,什么用品店面頭頂掛個LED顯示屏蕉毯,顯示文字跑馬燈乓搬; 那個就是將文字解析成點陣的一個實例;
好了代虾,知道要實現(xiàn)的效果进肯,接下來就看代碼實現(xiàn):
/**
* Created by jary on 2016/11/21. * 解析16*16的點陣字庫
*/
public class Font16 {
private final static String ENCODE = "GB2312";
private final static String ZK16 = "HZK16";//assets下的路徑
private final static String ASC16 = "ASC16";//assets下的路徑
private byte[][] arr;//返回的二位數(shù)組
private int all_16_32 = 16;//16*16
private int all_2_4 = 2;//一個漢字等于兩個字節(jié)
private int all_32_128 = 32;//漢字解析成16*16 所占字節(jié)數(shù)
private int font_width = 8;//ascii碼 8*16
private int font_height = 16;//ascii碼 8*16
private int all_16 = 16;//ascii碼解析成8*16 所占字節(jié)數(shù)
/**
* 解析成點陣
* @param str
* @return
*/
public byte[][] resolveString(String str) {
byte[] data = null;
int[] code = null;
int byteCount;
int lCount;
if (str.charAt(0) < 0x80) {
// 字母
arr = new byte[font_height][font_width];
data = read_a(str.charAt(0));
byteCount = 0;
for (int line = 0; line < 16; line++) {
lCount = 0;
for (int k = 0; k < 1; k++) {
for (int j = 0; j < 8; j++) {
if (((data[byteCount] >> (7 - j)) & 0x1) == 1) {
arr[line][lCount] = 1;
System.out.print("●");
} else {
System.out.print("○");
arr[line][lCount] = 0;
}
lCount++;
}
byteCount++;
}
System.out.println();
}
} else {
arr = new byte[all_16_32][all_16_32];
code = getByteCode(str.substring(0, 0 + 1));
data = read(code[0], code[1]);
byteCount = 0;
for (int line = 0; line < all_16_32; line++) {
lCount = 0;
for (int k = 0; k < all_2_4; k++) {
for (int j = 0; j < 8; j++) {
if (((data[byteCount] >> (7 - j)) & 0x1) == 1) {
arr[line][lCount] = 1;
System.out.print("●");
} else {
System.out.print("○");
arr[line][lCount] = 0;
}
lCount++;
}
byteCount++;
}
System.out.println();
}
}
return arr;
}
/**
* 讀取字庫中的ASCII 碼
*/
protected byte[] read_a(char char_num) {
byte[] data = null;
int ascii = (int) char_num;
try {
data = new byte[all_16];//定義緩存區(qū)的大小
InputStream inputStream = mContext.getResources().getAssets()
.open(ASC16);//打開ascii字庫的流
int offset = ascii * 16;//ascii碼在字庫里的偏移量
inputStream.skip(offset);
inputStream.read(data, 0, all_16);//讀取字庫中ascii碼點陣數(shù)據(jù)
inputStream.close();
return data;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
/**
* 讀取字庫中的漢字
* @param areaCode
* @param posCode
* @return
*/
protected byte[] read(int areaCode, int posCode) {
byte[] data = null;
try {
int area = areaCode - 0xa0;//區(qū)碼
int pos = posCode - 0xa0;//位碼
InputStream in = mContext.getResources().getAssets().open(ZK16);//打開中文字庫的流
long offset = all_32_128 * ((area - 1) * 94 + pos - 1);//漢字在字庫里的偏移量
in.skip(offset);//跳過偏移量
data = new byte[all_32_128];//定義緩存區(qū)的大小
in.read(data, 0, all_32_128);//讀取該漢字的點陣數(shù)據(jù)
in.close();
} catch (Exception ex) {
}
return data;
}
/**
* 獲取漢字的區(qū),位(ascii碼不需要區(qū)碼棉磨,位碼)
* @param str
* @return
*/
protected 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) {
ex.printStackTrace();
}
return byteCode;
}
}
解析出來字體風(fēng)格由字庫決定江掩,代碼中用到的兩個字庫文件:
ASC16(8x16 ASCII點陣 一個字符16Byte)
HZK16 (16x16 宋體漢字點陣 一個漢字32Byte)
網(wǎng)上直接搜索就可以下載;