The jni text of the Android kanji Pinyin

Source: Internet
Author: User

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; @Overrideprotected 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 ("Kanji to Pinyin"));} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

  

JNI class: Hz2py

Package Com.tool.hz2py;public class Hz2py {static {system.loadlibrary ("hz2py");}; Public native string Hz2py (string text);

  

Here is the C + + header file and code

/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class com_tool_hz2py_hz2py */#ifn def _included_com_tool_hz2py_hz2py#define _included_com_tool_hz2py_hz2py#ifdef __cplusplusextern "C" {#endif/* * Class:     com_tool_hz2py_hz2py * Method:    hz2py * Signature: (ljava/lang/string;) ljava/lang/string; */jniexport Jstring jnicall java_com_tool_hz2py_hz2py_hz2py  (jnienv *, Jobject, jstring); #ifdef __cplusplus} #endif #endif

  

#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 = = ') {OK = 0;        Break        } _tmp + +;    i + +;    } if (ok) {i = 0;        while (I < count) {*to = *from;        to + +;        from + +;    i + +;            }}else{if (overage_buff! = NULL) {while (*from! =) {*overage_buff = *from;        from + +; }} break; UTF8 the characters in the encoded string into pinyin//in input//OUT output//first_letter_only whether only the Pinyin initials//Polyphone_support output Polyphone//Add_blank Whether to append spaces between pinyin//Convert_double_char convert full-width characters to half-width characters//Overage_buff End If there are extra bytes that cannot make up the full UTF8 character, it will be written to Overage_buff, Null will be output to outvoid 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 (*ut F! = ' + ') {if ((*utf >> 7) = = 0) {hz2py_str_copy (out, UTF, 1);//If a byte is delimited with a # number *out = ' # ';//Use the # symbol as the delimiter out++;//remove the other English only Left Chinese characters//can only search for Chinese pinyin inside the letter//out--;//*out = ";} Two bytes else if ((*utf & 0xE0) = = 0xC0) {hz2py_str_copy (out, UTF, 2);} Three bytes else if ((*utf & 0xF0) = = 0xE0) {if (* (UTF + 1)! = ' &amp ' && * (UTF + 2)! = ' + ') {uni = (((int) (*UTF) ; 0x0F)) << 12) | (((int) (* (UTF + 1) & 0x3F)) << 6) | (* (UTF + 2) & 0x3F); if (Uni > 19967 && Uni < 40870) {memset (py_tmp, ' n '), memset (PY_TMP2, ' + ', 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! = ')} {if (*_tmp = = ' | ' | | * (_tmp-1) = = ' | ') {*_TMP2 = *_tmp;_tmp2++;} _tmp++;}} else {strcpy (PY_TMP2, py_tmp);} _TMP2 = py_tmp2;if (Polyphone_support = = 0) {while (*_tmp2! = ') ') {if (*_tmp2 = = ' | ') {*_tmp2 = ' + '; break;} _tmp2++;} _TMP2 = PY_TMP2;} strcpy (out, _TMP2), out + = strlen (_TMP2), if (Add_blank) {*out = ' # ';//# as delimiter out++;} UTF + = 3;} else if (Convert_double_char && uni > 65280 && Uni < 65375) {*out = Uni-65248;out++;utf + = 3;} els E 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);}} Four bytes Else if ((*utf & 0xF8) = = 0xF0) {hz2py_str_copy (out, UTF, 4);} Five bytes else if ((*utf & 0xFC) = = 0xF8) {hz2py_str_copy (out, UTF, 5);} Six bytes 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;}}} Determines whether a string is UTF8 encoded 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) {CHEC K_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;//F Irst_letter_only whether to output only pinyin initials//Polyphone_support output Polyphone//Add_blank whether to append spaces between pinyin//Convert_double_char whether to convert full-width characters to half-width characters// Overage_buff End If there are extra bytes that cannot make up the full UTF8 character, it will be written to Overage_buff, and null will be output to Out_tmp = Inbuf;if (strlen (Overage_buff)) {strcpy(_tmp, Overage_buff); _tmp + = strlen (Overage_buff); memset (Overage_buff, ' 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 Chinese memset (otext,0,512); hztpy (ptext,otext); jstring Returntext = Env->newstringutf (Otext); Env->releasestringutfchars (text,ptext);d elete Otext;return ReturnText;}

  

The header file is a bit large, I upload it directly to everyone to download.
Hz2py
Directly compiled. So shared libraries and Java files, note that the package name can only be used with this and cannot be changed.
Libhz2py

    • This article fixed link: http://www.ithtw.com/37.html
    • Reprint Please specify: Leehom August 05, 2014 in it 100,000 why published

The jni text of the Android kanji Pinyin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.