OPENCC_EXPORT opencc_t opencc_open(const char* configFileName);
OPENCC_EXPORT opencc_t opencc_open_w(const wchar_t* configFileName);
OPENCC_EXPORT int opencc_close(opencc_t opencc);
OPENCC_EXPORT size_t opencc_convert_utf8_to_buffer(opencc_t opencc, const char* input, size_t length, char* output);
OPENCC_EXPORT char* opencc_convert_utf8(opencc_t opencc, const char* input, size_t length);
OPENCC_EXPORT void opencc_convert_utf8_free(char* str);
OPENCC_EXPORT const char* opencc_error(void);
dumpbin /EXPORTS opencc.dll
package com.mei.opencc;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.WString;public interface Opencc extends Library
{
Opencc INSTANCE = (Opencc) Native.loadLibrary(“opencc”, Opencc.class);public Pointer opencc_open(String configFileName);
public Pointer opencc_open_w(WString configFileName);
public int opencc_close(Pointer opencc);
public int opencc_convert_utf8_to_buffer(Pointer opencc, String input, int length, String output);
public String opencc_convert_utf8(Pointer opencc, String input, int length);
public void opencc_convert_utf8_free(String input);
public String opencc_error();
}
package com.mei.opencc;
import com.mei.opencc.Opencc;
import com.sun.jna.Pointer;public class OpenccTest
{
public static void main(String[] args)
{
try
{
String configFileName = “E:\\workspace-sts-3.3.0.RELEASE\\OpenccJavaTest\\lib\\t2s.json”;
Pointer opencc = Opencc.INSTANCE.opencc_open(configFileName);
if (Pointer.nativeValue(opencc) == -1)
{
System.err.println(Opencc.INSTANCE.opencc_error());
return;
}String input = “中文簡繁轉換開源項目,支持詞彙級別的轉換、異體字轉換和地區習慣用詞轉換(中國大陸、臺灣、香港)”;
String output = Opencc.INSTANCE.opencc_convert_utf8(opencc, input, Integer.MAX_VALUE – 1);
if (output == null)
{
System.err.println(Opencc.INSTANCE.opencc_error());
return;
}
System.out.println(output);Opencc.INSTANCE.opencc_convert_utf8_free(output);
System.out.println(Opencc.INSTANCE.opencc_close(opencc));
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
}
转载时请保留出处,违法转载追究到底:进城务工人员小梅 » 在Java中进行中文繁体简体转换,基于OpenCC(Open Chinese Convert)方案