反编译并修复过的手写识别类--(android 搜狗 libhanwonhw_v15)

android的搜狗拼音输入法中,手写识别库貌似接口非常简单,所以我动了要用它的念头,于是反编译之。并修正了代码如下:

// HWIMEInterface.java

package com.sohu.handwriting.engine;


import android.content.Context;

import android.content.res.AssetFileDescriptor;

import android.content.res.Resources;

import android.util.Log;

import java.io.File;

import java.io.FileDescriptor;

import java.util.ArrayList;

import java.util.List;


import com.sohu.imputmethod.sogou.R;


public class HWIMEInterface

{

  public static final int ALC_CS_CURSIVE = 65536;

  public static final int ALC_GB18030 = 15;

  public static final int ALC_GBK = 7;

  public static final int ALC_GESTURE = 32768;

  public static final int ALC_HONGKONG = 16;

  public static final int ALC_LCALPHA = 1024;

  public static final int ALC_NUMERIC = 256;

  public static final int ALC_PUNC_COMMON = 2048;

  public static final int ALC_PUNC_RARE = 4096;

  public static final int ALC_SC_COMMON = 1;

  public static final int ALC_SC_GB18030EX = 8;

  public static final int ALC_SC_GBK34 = 4;

  public static final int ALC_SC_RADICAL = 128;

  public static final int ALC_SC_RARE = 2;

  public static final int ALC_SYM_COMMON = 8192;

  public static final int ALC_SYM_RARE = 16384;

  public static final int ALC_TC_TO_SC = 32;

  public static final int ALC_UCALPHA = 512;

  private static final boolean DEBUG = false;

  private static final int RESULT_NUM = 512;

  private static final String TAG = "HWIMEInterface";

  private static HWIMEInterface sInterface;

  private ArrayList<CharSequence> mCandidateWords;

  private Context mContext;

  private int mNativeContext;

  private char[] mRecognizedResult;


  

  static 

  {

  System.loadLibrary("hanvonhw_v15");

  }

  

  private HWIMEInterface(Context context)

  {

    this.mContext = context;

    nativeSetup();

    this.mCandidateWords = new ArrayList<CharSequence>();

    this.mRecognizedResult = new char[RESULT_NUM];


    String fileName = mContext.getFilesDir().getAbsolutePath() + "/dic_gb18030.bin";

    File file = new File(fileName);

    if (!file.exists())

      return;

    file.delete();

  }


  private static void LOGD(String string)

  {

  Log.d(TAG,string);

  }


  public static HWIMEInterface getInterface(Context context)

  {

    if (sInterface == null)

      sInterface = new HWIMEInterface(context);

    return sInterface;

  }


  private void splitCandidates_hw(char[] candidatesString, int candidatesNum, List<CharSequence>  candidatesList, int paramInt2)

  {

    int i = 0;

    while (true)

    {

        if (i >= candidatesNum)

            return;

      

        String str = String.valueOf(candidatesString[i]);

        candidatesList.add(i, str);

        i += 1;

    }

  }


  public native void freeDict();


  public void freeHWDict()

  {

    freeDict();

  }


  public native void loadDict();


  public native void loadDictRes(FileDescriptor mFileDescriptor, long startOffset, long length);


  public void loadHWDict()

  {

    AssetFileDescriptor mAssetFileDescriptor = this.mContext.getResources().openRawResourceFd(R.raw.dic_hw);

    FileDescriptor mFileDescriptor = mAssetFileDescriptor.getFileDescriptor();

    loadDictRes(mFileDescriptor, mAssetFileDescriptor.getStartOffset(), mAssetFileDescriptor.getLength());

  }


  public native void nativeFinalize();


  public native void nativeSetup();


  public native int recognize(short[] strokeData, char[] result, int limit);


  public ArrayList<CharSequence> recognizeStroke(short[] strokeData)

  {

    int i = recognize(strokeData, mRecognizedResult, 16);

    mCandidateWords.clear();

    splitCandidates_hw(mRecognizedResult, i, mCandidateWords, 0);

    return mCandidateWords;

  }


  public void release()

  {

    if (sInterface == null)

      return;

    

    nativeFinalize();

    mCandidateWords.clear();

    mCandidateWords = null;

    mRecognizedResult = null;

    sInterface = null;

  }


  public native void setDictPath(String dictpath);


  public native void setRecognizeRange(int range);

}


*******   这个类使用起来也比较简单

//onCreate

HWIMEInterface mHwIme = HWIMEInterface.getInterface(this);

        mHwIme.loadHWDict();

....

//onDestroy()

mHwIme.freeHWDict();


// 在需要识别的地方调用

mHwIme.recognizeStroke(strokes);   // strokes 是short[] 类型,也就是你给的笔画数据

注意:

本文用到的数据请自己去下载搜狗输入法。


你可能感兴趣的:(android,String,File,null,输入法,搜狗)