- 浏览: 16726 次
- 性别:
- 来自: 武汉
最新评论
原来研究的小项目,现在简单整理了一下,免费贡献给大家!
package com.tool.hz2py;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
protected Hz2py hz2py;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hz2py = new Hz2py();
TextView view = (TextView) findViewById(R.id.text);
view.setText(hz2py.hz2py("汉字转拼音"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
#include "hz2py.h"
#include <string.h>
#include "com_tool_hz2py_Hz2py.h"
#define HZ2PY_UTF8_CHECK_LENGTH 20
#define HZ2PY_FILE_READ_BUF_ARRAY_SIZE 1000
#define HZ2PY_INPUT_BUF_ARRAY_SIZE 1024
#define HZ2PY_OUTPUT_BUF_ARRAY_SIZE 2048
#define HZ2PY_STR_COPY(to, from, count) \
ok = 1;\
i = 0;\
_tmp = from;\
while(i < count)\
{\
if (*_tmp == '\0')\
{\
ok = 0;\
break;\
}\
_tmp ++;\
i ++;\
}\
if (ok)\
{\
i = 0;\
while(i < count)\
{\
*to = *from;\
to ++;\
from ++;\
i ++;\
}\
}\
else\
{\
if (overage_buff != NULL)\
{\
while(*from != '\0')\
{\
*overage_buff = *from;\
from ++;\
}\
}\
break;\
}
//将utf8编码的字符串中的汉字解成拼音
// in 输入
// out 输出
// first_letter_only 是否只输出拼音首字母
// polyphone_support 是否输出多音字
// add_blank 是否在拼音之间追加空格
// convert_double_char 是否转换全角字符为半角字符
// overage_buff 末尾如果有多余的不能组成完整utf8字符的字节,将写到overage_buff,传NULL将输出到out
void utf8_to_pinyin(char *in, char *out, int first_letter_only,
int polyphone_support, int add_blank, int convert_double_char,
char *overage_buff) {
int i = 0;
char *utf = in;
char *_tmp;
char *_tmp2;
char py_tmp[30] = "";
char py_tmp2[30] = "";
char *out_start_flag = out;
int uni;
int ok = 0;
while (*utf != '\0') {
if ((*utf >> 7) == 0) {
HZ2PY_STR_COPY(out, utf, 1);
//如果为一个字节加上#号分隔
*out = '#'; //用#号做为分隔符
out++;
//去掉其它的英文只留汉字
//只能搜索到汉字拼音里面字母
// out--;
// *out = ' ';
}
//两个字节
else if ((*utf & 0xE0) == 0xC0) {
HZ2PY_STR_COPY(out, utf, 2);
}
//三个字节
else if ((*utf & 0xF0) == 0xE0) {
if (*(utf + 1) != '\0' && *(utf + 2) != '\0') {
uni = (((int) (*utf & 0x0F)) << 12)
| (((int) (*(utf + 1) & 0x3F)) << 6)
| (*(utf + 2) & 0x3F);
if (uni > 19967 && uni < 40870) {
memset(py_tmp, '\0', 30);
memset(py_tmp2, '\0', 30);
strcpy(py_tmp, _pinyin_table_[uni - 19968]);
_tmp = py_tmp;
_tmp2 = py_tmp2;
if (first_letter_only == 1) {
*_tmp2 = *_tmp;
_tmp++;
_tmp2++;
while (*_tmp != '\0') {
if (*_tmp == '|' || *(_tmp - 1) == '|') {
*_tmp2 = *_tmp;
_tmp2++;
}
_tmp++;
}
} else {
strcpy(py_tmp2, py_tmp);
}
_tmp2 = py_tmp2;
if (polyphone_support == 0) {
while (*_tmp2 != '\0') {
if (*_tmp2 == '|') {
*_tmp2 = '\0';
break;
}
_tmp2++;
}
_tmp2 = py_tmp2;
}
strcpy(out, _tmp2);
out += strlen(_tmp2);
if (add_blank) {
*out = '#'; //用#号做为分隔符
out++;
}
utf += 3;
} else if (convert_double_char && uni > 65280 && uni < 65375) {
*out = uni - 65248;
out++;
utf += 3;
} else if (convert_double_char && uni == 12288) {
*out = 32;
out++;
utf += 3;
} else {
HZ2PY_STR_COPY(out, utf, 3);
}
} else {
HZ2PY_STR_COPY(out, utf, 3);
}
}
//四个字节
else if ((*utf & 0xF8) == 0xF0) {
HZ2PY_STR_COPY(out, utf, 4);
}
//五个字节
else if ((*utf & 0xFC) == 0xF8) {
HZ2PY_STR_COPY(out, utf, 5);
}
//六个字节
else if ((*utf & 0xFE) == 0xFC) {
HZ2PY_STR_COPY(out, utf, 6);
} else {
if (overage_buff != NULL) {
*overage_buff = *utf;
overage_buff++;
} else {
HZ2PY_STR_COPY(out, utf, 1);
}
break;
}
}
}
//判断一个字符串是否为utf8编码
int is_utf8_string(char *utf) {
int length = strlen(utf);
int check_sub = 0;
int i = 0;
if (length > HZ2PY_UTF8_CHECK_LENGTH) {
length = HZ2PY_UTF8_CHECK_LENGTH;
}
for (; i < length; i++) {
if (check_sub == 0) {
if ((utf[i] >> 7) == 0) {
continue;
} else if ((utf[i] & 0xE0) == 0xC0) {
check_sub = 1;
} else if ((utf[i] & 0xF0) == 0xE0) {
check_sub = 2;
} else if ((utf[i] & 0xF8) == 0xF0) {
check_sub = 3;
} else if ((utf[i] & 0xFC) == 0xF8) {
check_sub = 4;
} else if ((utf[i] & 0xFE) == 0xFC) {
check_sub = 5;
} else {
return 0;
}
} else {
if ((utf[i] & 0xC0) != 0x80) {
return 0;
}
check_sub--;
}
}
return 1;
}
int hztpy(const char *read_buff, char *outbuf) {
char overage_buff[7] = { 0 };
char *_tmp = NULL;
char inbuf[HZ2PY_INPUT_BUF_ARRAY_SIZE] = { 0 };
int add_blank = 1;
int polyphone_support = 1;
int first_letter_only = 0;
int convert_double_char = 0;
// first_letter_only 是否只输出拼音首字母
// polyphone_support 是否输出多音字
// add_blank 是否在拼音之间追加空格
// convert_double_char 是否转换全角字符为半角字符
// overage_buff 末尾如果有多余的不能组成完整utf8字符的字节,将写到overage_buff,传NULL将输出到out
_tmp = inbuf;
if (strlen(overage_buff)) {
strcpy(_tmp, overage_buff);
_tmp += strlen(overage_buff);
memset(overage_buff, '\0', 7);
}
strcpy(_tmp, read_buff);
if (!is_utf8_string(inbuf)) {
return -1;
}
utf8_to_pinyin(inbuf, outbuf, first_letter_only, polyphone_support,
add_blank, convert_double_char, overage_buff);
return 1;
}
JNIEXPORT jstring JNICALL Java_com_tool_hz2py_Hz2py_hz2py(JNIEnv *env,
jobject thiz, jstring text) {
const char* pText = env->GetStringUTFChars(text, 0);
char* oText = new char[512];//256中文
memset(oText,0,512);
hztpy(pText,oText);
jstring returnText = env->NewStringUTF(oText);
env->ReleaseStringUTFChars(text,pText);
delete oText;
return returnText;
}
代码格式显示不出来,郁闷.请大家移步这里看代码吧!
IT十万为什么 » http://www.ithtw.com/37.html
package com.tool.hz2py;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
protected Hz2py hz2py;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hz2py = new Hz2py();
TextView view = (TextView) findViewById(R.id.text);
view.setText(hz2py.hz2py("汉字转拼音"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
#include "hz2py.h"
#include <string.h>
#include "com_tool_hz2py_Hz2py.h"
#define HZ2PY_UTF8_CHECK_LENGTH 20
#define HZ2PY_FILE_READ_BUF_ARRAY_SIZE 1000
#define HZ2PY_INPUT_BUF_ARRAY_SIZE 1024
#define HZ2PY_OUTPUT_BUF_ARRAY_SIZE 2048
#define HZ2PY_STR_COPY(to, from, count) \
ok = 1;\
i = 0;\
_tmp = from;\
while(i < count)\
{\
if (*_tmp == '\0')\
{\
ok = 0;\
break;\
}\
_tmp ++;\
i ++;\
}\
if (ok)\
{\
i = 0;\
while(i < count)\
{\
*to = *from;\
to ++;\
from ++;\
i ++;\
}\
}\
else\
{\
if (overage_buff != NULL)\
{\
while(*from != '\0')\
{\
*overage_buff = *from;\
from ++;\
}\
}\
break;\
}
//将utf8编码的字符串中的汉字解成拼音
// in 输入
// out 输出
// first_letter_only 是否只输出拼音首字母
// polyphone_support 是否输出多音字
// add_blank 是否在拼音之间追加空格
// convert_double_char 是否转换全角字符为半角字符
// overage_buff 末尾如果有多余的不能组成完整utf8字符的字节,将写到overage_buff,传NULL将输出到out
void utf8_to_pinyin(char *in, char *out, int first_letter_only,
int polyphone_support, int add_blank, int convert_double_char,
char *overage_buff) {
int i = 0;
char *utf = in;
char *_tmp;
char *_tmp2;
char py_tmp[30] = "";
char py_tmp2[30] = "";
char *out_start_flag = out;
int uni;
int ok = 0;
while (*utf != '\0') {
if ((*utf >> 7) == 0) {
HZ2PY_STR_COPY(out, utf, 1);
//如果为一个字节加上#号分隔
*out = '#'; //用#号做为分隔符
out++;
//去掉其它的英文只留汉字
//只能搜索到汉字拼音里面字母
// out--;
// *out = ' ';
}
//两个字节
else if ((*utf & 0xE0) == 0xC0) {
HZ2PY_STR_COPY(out, utf, 2);
}
//三个字节
else if ((*utf & 0xF0) == 0xE0) {
if (*(utf + 1) != '\0' && *(utf + 2) != '\0') {
uni = (((int) (*utf & 0x0F)) << 12)
| (((int) (*(utf + 1) & 0x3F)) << 6)
| (*(utf + 2) & 0x3F);
if (uni > 19967 && uni < 40870) {
memset(py_tmp, '\0', 30);
memset(py_tmp2, '\0', 30);
strcpy(py_tmp, _pinyin_table_[uni - 19968]);
_tmp = py_tmp;
_tmp2 = py_tmp2;
if (first_letter_only == 1) {
*_tmp2 = *_tmp;
_tmp++;
_tmp2++;
while (*_tmp != '\0') {
if (*_tmp == '|' || *(_tmp - 1) == '|') {
*_tmp2 = *_tmp;
_tmp2++;
}
_tmp++;
}
} else {
strcpy(py_tmp2, py_tmp);
}
_tmp2 = py_tmp2;
if (polyphone_support == 0) {
while (*_tmp2 != '\0') {
if (*_tmp2 == '|') {
*_tmp2 = '\0';
break;
}
_tmp2++;
}
_tmp2 = py_tmp2;
}
strcpy(out, _tmp2);
out += strlen(_tmp2);
if (add_blank) {
*out = '#'; //用#号做为分隔符
out++;
}
utf += 3;
} else if (convert_double_char && uni > 65280 && uni < 65375) {
*out = uni - 65248;
out++;
utf += 3;
} else if (convert_double_char && uni == 12288) {
*out = 32;
out++;
utf += 3;
} else {
HZ2PY_STR_COPY(out, utf, 3);
}
} else {
HZ2PY_STR_COPY(out, utf, 3);
}
}
//四个字节
else if ((*utf & 0xF8) == 0xF0) {
HZ2PY_STR_COPY(out, utf, 4);
}
//五个字节
else if ((*utf & 0xFC) == 0xF8) {
HZ2PY_STR_COPY(out, utf, 5);
}
//六个字节
else if ((*utf & 0xFE) == 0xFC) {
HZ2PY_STR_COPY(out, utf, 6);
} else {
if (overage_buff != NULL) {
*overage_buff = *utf;
overage_buff++;
} else {
HZ2PY_STR_COPY(out, utf, 1);
}
break;
}
}
}
//判断一个字符串是否为utf8编码
int is_utf8_string(char *utf) {
int length = strlen(utf);
int check_sub = 0;
int i = 0;
if (length > HZ2PY_UTF8_CHECK_LENGTH) {
length = HZ2PY_UTF8_CHECK_LENGTH;
}
for (; i < length; i++) {
if (check_sub == 0) {
if ((utf[i] >> 7) == 0) {
continue;
} else if ((utf[i] & 0xE0) == 0xC0) {
check_sub = 1;
} else if ((utf[i] & 0xF0) == 0xE0) {
check_sub = 2;
} else if ((utf[i] & 0xF8) == 0xF0) {
check_sub = 3;
} else if ((utf[i] & 0xFC) == 0xF8) {
check_sub = 4;
} else if ((utf[i] & 0xFE) == 0xFC) {
check_sub = 5;
} else {
return 0;
}
} else {
if ((utf[i] & 0xC0) != 0x80) {
return 0;
}
check_sub--;
}
}
return 1;
}
int hztpy(const char *read_buff, char *outbuf) {
char overage_buff[7] = { 0 };
char *_tmp = NULL;
char inbuf[HZ2PY_INPUT_BUF_ARRAY_SIZE] = { 0 };
int add_blank = 1;
int polyphone_support = 1;
int first_letter_only = 0;
int convert_double_char = 0;
// first_letter_only 是否只输出拼音首字母
// polyphone_support 是否输出多音字
// add_blank 是否在拼音之间追加空格
// convert_double_char 是否转换全角字符为半角字符
// overage_buff 末尾如果有多余的不能组成完整utf8字符的字节,将写到overage_buff,传NULL将输出到out
_tmp = inbuf;
if (strlen(overage_buff)) {
strcpy(_tmp, overage_buff);
_tmp += strlen(overage_buff);
memset(overage_buff, '\0', 7);
}
strcpy(_tmp, read_buff);
if (!is_utf8_string(inbuf)) {
return -1;
}
utf8_to_pinyin(inbuf, outbuf, first_letter_only, polyphone_support,
add_blank, convert_double_char, overage_buff);
return 1;
}
JNIEXPORT jstring JNICALL Java_com_tool_hz2py_Hz2py_hz2py(JNIEnv *env,
jobject thiz, jstring text) {
const char* pText = env->GetStringUTFChars(text, 0);
char* oText = new char[512];//256中文
memset(oText,0,512);
hztpy(pText,oText);
jstring returnText = env->NewStringUTF(oText);
env->ReleaseStringUTFChars(text,pText);
delete oText;
return returnText;
}
代码格式显示不出来,郁闷.请大家移步这里看代码吧!
IT十万为什么 » http://www.ithtw.com/37.html
发表评论
-
MultiSlidingDrawer多方向抽屉导航
2015-03-16 16:29 859项目需求很复杂,各种要求。官方的drawerlayout和开 ... -
分享两本android电子书
2015-03-09 16:46 1604Android 3D游戏开发技术宝典(PDF教程下载) ... -
编写自己的Adapter模板
2015-03-06 14:19 955如果你项目一直用系统给你封装的BaseAdapter的话,那 ... -
三国杀 JAVA源码(可移植Android平台)
2015-03-04 17:35 1283JAVA源码网盘下载:http://pan.baidu.c ... -
android应用斗地主
2015-03-02 18:55 321斗地主是纸牌游戏。关于阶级斗争详见土地改革运动。 斗地主是一 ... -
Android应用基于XMPP的仿微信
2015-03-02 18:53 624基于XMPP的微客服的项目源码,本站之前也介绍过一个微 ... -
高仿QQ界面(无通信)
2015-03-02 15:02 815本项目是一个高仿QQ最新版本的项目,界面超级华丽,使用了大量 ... -
MVP在安卓(Android)整理
2015-02-27 16:29 471前言 MVP作为一种MVC的 ... -
安卓手机QQ红点拖拽消除的实现
2015-02-20 10:02 1482新版手机QQ5.+上新增了 ... -
优化 Android ListView 异步加载图片
2015-02-18 10:55 723先说说这篇文章的优点把,开启线程异步加载图片,然后刷新UI显 ... -
Android增量升级简单实现,附源码
2015-01-19 15:47 669随着现在手机硬件不断 ... -
Android使用AIDL跨进程数据共享
2015-01-16 13:44 1662AIDL:Android Interface Defi ...
相关推荐
java汉字转拼音,android汉字转拼音,汉字转拼音首字母,汉字转ASCII
Android中将汉字转成拼音,其中使用了一个第三方包。
基于 Android 原生 ICU 的库实现; 优点是简单,快捷; 缺点是仅支持 Android 4.3 及以上的系统。 主要就是从源码(Android 5.0)里拷贝 HanziToPinyin.java 和 Transliterator.java 这两个文件过来。 Android 旧版...
android 汉字转为拼音,带测试文件
java安卓android汉字转拼音中文转拼音pinyin4j简化精简版. 网上流传的有180KB,太大了,对于android开发来说。我精简了很多无关内容,收录常用汉字大约2万个,完全够用了,还专门写了一个helper类,直接调用这个...
Android汉字转拼音的demo,用的第三方jar包pinyin4j.jar
Android Java汉字转拼音总结, Android Java汉字转拼音总结。。
android中文转拼音
文章http://blog.csdn.net/spare_h/article/details/6733547的源码,基于android4.2源码提取。在2.X和4.X上均测试通过。
在网上查了很多这类的工具代码 但没能成功转换 经过自己研究在前人的基础上修改终于实现的Android汉字转拼音 我在小米3运行测试的 测试没问题
android 汉字转拼音应用 源代码,没有使用任何第三方jar,本应用可以打包成jar供开发使用。很强大。
Android中文转拼音所用到的jar,可以帮助我们把中文转换成拼音
这是一个android的汉字转拼音包,用于数据查询搜索的。
安卓汉字转拼音 联系人字母城市索引jar包 pinyinHelper pinyin4j-2.5.0.jar
其主要是android 中将汉字转换为拼音,但对其多音字的处理,其默认的是一般的拼音。总之,对于android中的汉字转拼音,其为系统的一种方法。