Skip to content

Instantly share code, notes, and snippets.

@selvypen

selvypen/Hwr.cs Secret

Last active May 25, 2020 04:46
Show Gist options
  • Save selvypen/5c4d0df1a700d1ec845dac8ddcdc83f9 to your computer and use it in GitHub Desktop.
Save selvypen/5c4d0df1a700d1ec845dac8ddcdc83f9 to your computer and use it in GitHub Desktop.
Simple example source of math recognition for Windows using C#
/*!
* @brief Math Handwriting APIs
* @date 2020/01/17
* @file Hwr.cs
* @author samu.s.ko
*
* Copyright 2020. SELVAS AI Inc. All Rights Reserved.
*/
using System;
using System.Runtime.InteropServices;
namespace Selvasai
{
class Hwr
{
// Error Code ----------------------------------------------------------
/// <summary>
/// 성공
/// </summary>
public const int ERR_SUCCESS = 0;
/// <summary>
/// 인식결과가 없음
/// </summary>
public const int ERR_NORESULT = 1;
/// <summary>
/// Null Pointer를 참조함
/// </summary>
public const int ERR_NULL_POINTER = 2;
/// <summary>
/// memory access 범위를 벗어남 fatal error
/// </summary>
public const int ERR_OUTOFMEMORY = 3;
/// <summary>
/// value or size 범위를 벗어남 exception
/// </summary>
public const int ERR_OUTOFRANGE = 4;
/// <summary>
/// 입력된 데이터가 없음
/// </summary>
public const int ERR_EMPTY_INK = 5;
/// <summary>
/// 입력된 인자가 잘못됨
/// </summary>
public const int ERR_INVALID_ARGUMENTS = 6;
/// <summary>
/// 설정된 인식모델이 비정상적임
/// </summary>
public const int ERR_INVALID_MODEL = 7;
/// <summary>
/// 잘못된 객체에 접근
/// </summary>
public const int ERR_INVALID_INSTANCE = 8;
/// <summary>
/// 데모 기간/횟수 만료
/// </summary>
public const int ERR_EXPIRE_DEMO = 9;
/// <summary>
/// 엔진이 실행 중
/// </summary>
public const int ERR_ENGINE_BUSY = 10;
/// <summary>
/// 인증 실패
/// </summary>
public const int ERR_AUTHORIZATION_FAIL = 11;
/// <summary>
/// 엔진이 이미 존재함
/// </summary>
public const int ERR_ALREADY_EXIST = 12;
// Recognition Mode ----------------------------------------------------
/// <summary>
/// 낱자 인식
/// </summary>
public const int SINGLECHAR = 0;
/// <summary>
/// 여러 글자 인식
/// </summary>
public const int MULTICHAR = 1;
/// <summary>
/// 겹쳐 쓴 글자 인식
/// </summary>
public const int OVERLAPCHAR = 2;
/// <summary>
/// 여러 줄로 된 글자 인식
/// </summary>
public const int MULTILINE = 3;
// Language Mode -------------------------------------------------------
/// <summary>
/// 초등수학
/// </summary>
public const int DLANG_MATH_ELEMENTARY = 201;
/// <summary>
/// 중등수학
/// </summary>
public const int DLANG_MATH_MIDDLE = 202;
/// <summary>
/// 중등수학 확장
/// </summary>
public const int DLANG_MATH_MIDDLE_EXPANSION = 203;
/// <summary>
/// 화학식
/// </summary>
public const int DLANG_MATH_CHEMICAL = 204;
// Language Type -------------------------------------------------------
/// <summary>
/// 타입 없음
/// </summary>
public const int DTYPE_NONE = (1 << 0);
/// <summary>
/// 초등수학
/// </summary>
public const int DTYPE_MATH_ET = (1 << 22);
/// <summary>
/// 중등수학
/// </summary>
public const int DTYPE_MATH_MD = (1 << 23);
/// <summary>
/// 중등수학 확장
/// </summary>
public const int DTYPE_MATH_EX = (1 << 24);
/// <summary>
/// 화학식
/// </summary>
public const int DTYPE_MATH_CF = (1 << 25);
// Param type ----------------------------------------------------------
public const int LOG_LEVEL = 0;
public const int LOG_CALLBACK = 1;
public const int WRITE_INK = 2;
public const int LANG_MODEL = 3;
// Log level -----------------------------------------------------------
public const int LEVEL_NONE = 0;
public const int LEVEL_ERROR = 1;
public const int LEVEL_WARN = 2;
public const int LEVEL_INFO = 3;
public const int LEVEL_DEBUG = 4;
// Main APIs -----------------------------------------------------------
/// <summary>
/// 인식엔진의 인스턴스를 생성함
/// </summary>
/// <param name="keyPath">[in] 파일명을 포함한 License key 파일의 경로</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRCreate")]
public static extern int Create(String keyPath);
/// <summary>
/// 인식모드 속성 값을 설정함
/// </summary>
/// <param name="setting">[in] setting 설정이 완료된 Setting Object</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRSetAttribute")]
public static extern int SetAttribute(IntPtr setting);
/// <summary>
/// 인식모드 속성 값에 따라 입력 데이터를 인식함
/// </summary>
/// <param name="ink">[in] ink 좌표가 입력된 Ink Object</param>
/// <param name="result">[out] 인식된 결과가 저장된 Result Object</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRecognize")]
public static extern int Recognize(IntPtr ink, IntPtr result);
/// <summary>
/// 인식엔진의 인스턴스를 소멸시킴
/// </summary>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRClose")]
public static extern int Close();
// Optional APIs -------------------------------------------------------
/// <summary>
/// 엔진내부에서 사용하는 외부리소스(언어모델 파일 등)의 경로 설정
/// </summary>
/// <param name="path">[in] path 리소스 경로</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRSetExternalResourcePath")]
public static extern int SetExternalResourcePath(String path);
/// <summary>
/// 엔진내부에서 사용하는 외부라이브러리의 경로 설정
/// </summary>
/// <param name="path">[in] path 외부라이브러리의 경로</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRSetExternalLibraryPath")]
public static extern int SetExternalLibraryPath(String path);
/// <summary>
/// 특정 타입의 파라미터 값을 가져옴
/// </summary>
/// <param name="type">[in] 파라미터 타입</param>
/// <param name="param">[out] 특정 파리미터 타입의 포인터</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetParam")]
public static extern int GetParam(int type, [In, Out] ref int param);
/// <summary>
/// 특정 타입의 파라미터 값을 설정함
/// </summary>
/// <param name="type">[in] 파라미터 타입</param>
/// <param name="param">[in] 특정 파리미터 타입의 포인터</param>
/// <returns></returns>
[DllImport("libspmath", EntryPoint = "DHWRSetParam")]
public static extern int SetParam(int type, [In, Out] ref int param);
/// <summary>
/// 엔진의 빌드넘버 스트링의 포인터를 가져옴
/// </summary>
/// <param name="revision">[out] 엔진 빌드넘버</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetRevision")]
public static extern int GetRevisionPtr(IntPtr[] revision);
/// <summary>
/// 엔진의 빌드넘버 스트링을 반환
/// </summary>
/// <returns>엔진의 빌드넘버 스트링</returns>
public static String GetRevision()
{
IntPtr[] version = new IntPtr[1];
Hwr.GetRevisionPtr(version);
return Marshal.PtrToStringAnsi(version[0]);
}
/// <summary>
/// License key에 설정된 엔진의 due date를 가져온다
/// </summary>
/// <param name="due_date">[out] 년월일을 나타내는 8자리의 integer 값 (ex 20161206)</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetDueDate")]
public static extern int GetDueDate([In, Out] ref int due_date);
// SettingObject APIs --------------------------------------------------
/// <summary>
/// 설정 오브젝트를 생성한다
/// </summary>
/// <returns>설정 오브젝트</returns>
[DllImport("libspmath", EntryPoint = "DHWRCreateSettingObject")]
public static extern IntPtr CreateSettingObject();
/// <summary>
/// 설정 오브젝트를 제거한다
/// </summary>
/// <param name="setting">[in] 설정 오브젝트</param>
[DllImport("libspmath", EntryPoint = "DHWRDestroySettingObject")]
public static extern void DestroySettingObject(IntPtr setting);
/// <summary>
/// 필기 인식 모드를 설정한다(낱자, 연속)
/// </summary>
/// <param name="setting"> [in] 설정 오브젝트</param>
/// <param name="mode">[in] 인식 모드</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRSetRecognitionMode")]
public static extern int SetRecognitionMode(IntPtr setting, int mode);
/// <summary>
/// 인식 후보 출력 크기를 설정한다
/// </summary>
/// <param name="setting">[in] 설정 오브젝트</param>
/// <param name="size">[in] 후보수</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRSetCandidateSize")]
public static extern int SetCandidateSize(IntPtr setting, int size);
/// <summary>
/// 인식할 언어를 추가 한다
/// </summary>
/// <param name="setting">[in] 설정 오브젝트</param>
/// <param name="language">[in] 언어 값</param>
/// <param name="option">[in] 따른 옵션</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRAddLanguage")]
public static extern int AddLanguage(IntPtr setting, int language, int option);
/// <summary>
/// 설정된 언어 크기를 가져온다
/// </summary>
/// <param name="setting">[in] 설정 오브젝트</param>
/// <param name="size">[out] 크기</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetLanguageSize")]
public static extern int GetLanguageSize(IntPtr setting, [In, Out] ref int size);
/// <summary>
/// 설정된 언어를 초기화 한다
/// </summary>
/// <param name="setting">[in] 설정 오브젝트</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRClearLanguage")]
public static extern int ClearLanguage(IntPtr setting);
/// <summary>
/// 인식할 심볼을 정의
/// </summary>
/// <param name="setting">[in] 설정 오브젝트</param>
/// <param name="charset">[in] 인식할 심볼의 리스트</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRSetUserCharSet")]
public static extern int SetUserCharSet(IntPtr setting, [In] ushort[] charset);
// InkObject APIs ------------------------------------------------------
/// <summary>
/// 잉크 오브젝트를 생성한다
/// </summary>
/// <returns>잉크 오브젝트</returns>
[DllImport("libspmath", EntryPoint = "DHWRCreateInkObject")]
public static extern IntPtr CreateInkObject();
/// <summary>
/// 잉크 오브젝트를 제거한다
/// </summary>
/// <param name="ink">[in] 잉크 오브젝트</param>
[DllImport("libspmath", EntryPoint = "DHWRDestroyInkObject")]
public static extern void DestroyInkObject(IntPtr ink);
/// <summary>
/// 인식엔진에서 인식할 터치 좌표(잉크 좌표)를 추가함
/// </summary>
/// <param name="ink">[in] ink 잉크 오브젝트</param>
/// <param name="x">[in] x좌표</param>
/// <param name="y">[in] y좌표</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRAddPoint")]
public static extern int AddPoint(IntPtr ink, int x, int y);
/// <summary>
/// TouchUp 이벤트에 의해 한 획의 입력을 마쳤을 때 호출함
/// </summary>
/// <param name="ink">[in] 잉크 오브젝트</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWREndStroke")]
public static extern int EndStroke(IntPtr ink);
/// <summary>
/// 입력한 좌표 데이터를 초기화함
/// </summary>
/// <param name="ink">[in] 잉크 오브젝트</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRInkClear")]
public static extern int InkClear(IntPtr ink);
/// <summary>
/// 입력된 잉크 인덱스 값에 해당하는 잉크 좌표값을 받아옴
/// </summary>
/// <param name="ink">[in] 잉크 오브젝트</param>
/// <param name="index">[in] 받아오고자 하는 잉크의 인덱스</param>
/// <param name="x">[out] 해당 잉크의 x좌표값</param>
/// <param name="y">[out] 해당 잉크의 y좌표값</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetInkPoint")]
public static extern int GetInkPoint(IntPtr ink, int index, [In, Out] ref int x, [In, Out] ref int y);
/// <summary>
/// 현재까지 입력된 잉크의 카운트
/// </summary>
/// <param name="ink">[in] 잉크 오브젝트</param>
/// <param name="count">[out] 잉크 카운트</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetInkCount")]
public static extern int GetInkCount(IntPtr ink, [In, Out] ref int count);
// ResultObject APIs ---------------------------------------------------
/// <summary>
/// 결과 오브젝트를 생성한다
/// </summary>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRCreateResultObject")]
public static extern IntPtr CreateResultObject();
/// <summary>
/// 결과 오브젝트를 제거한다
/// </summary>
/// <param name="result">[in] 결과 오브젝트</param>
[DllImport("libspmath", EntryPoint = "DHWRDestroyResultObject")]
public static extern void DestroyResultObject(IntPtr result);
/// <summary>
/// 결과 값의 라인 크기를 가져온다
/// </summary>
/// <param name="ink">[in] 결과 오브젝트</param>
/// <returns>Line Size</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetLineSize")]
public static extern int GetLineSize(IntPtr ink);
/// <summary>
/// 지정된 인덱스의 라인 결과를 가져온다
/// </summary>
/// <param name="result">[in] 결과 오브젝트</param>
/// <param name="index">[in] 라인 인덱스</param>
/// <returns>라인 결과</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetLine")]
public static extern IntPtr GetLine(IntPtr result, int index);
/// <summary>
/// 라인 결과 값의 블럭 크기를 가져온다
/// </summary>
/// <param name="line">[in] 라인 결과 값</param>
/// <returns>Block Size</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetBlockSize")]
public static extern int GetBlockSize(IntPtr line);
/// <summary>
/// 라인 결과 값의 지정된 인덱스의 블럭 결과를 가져온다
/// </summary>
/// <param name="line">[in] 라인</param>
/// <param name="index">[in] 블럭 인덱스</param>
/// <returns>블럭 결과</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetBlock")]
public static extern IntPtr GetBlock(IntPtr line, int index);
/// <summary>
/// 블럭 결과 값의 처리된 획의 개수를 얻어온다
/// </summary>
/// <param name="block">[in] 블럭 결과 값</param>
/// <returns>Stroke Size</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetStrokeSize")]
public static extern int GetStrokeSize(IntPtr block);
/// <summary>
/// 블럭 결과값의 처리된 획의 인덱스들을 얻어온다
/// </summary>
/// <param name="block">[in] 블럭 결과 값</param>
/// <param name="indices">[out] 인덱스 포인터</param>
/// <param name="size">[in] 인덱스 포인터 사이즈</param>
/// <returns>에러 코드 값</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetStrokeIndices")]
public static extern int GetStrokeIndices(IntPtr block, [In, Out] ref int indices, int size);
/// <summary>
/// 블럭 결과 값에서 후보 크기를 가져온다
/// </summary>
/// <param name="block">[in] 블럭 결과 값</param>
/// <returns>Candidate Size</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetCandidateSize")]
public static extern int GetCandidateSize(IntPtr block);
/// <summary>
/// 블럭 결과 값에서 인식 후보문자의 포인터를 반환한다
/// </summary>
/// <param name="block">[in] 블럭 결과 값</param>
/// <param name="index">[in] 후보 인덱스</param>
/// <param name="length">[out] 인식후보 문자열 크기</param>
/// <returns>후보문자의 포인터</returns>
[DllImport("libspmath", EntryPoint = "DHWRGetCandidate")]
public static extern IntPtr GetCandidatePtr(IntPtr block, int index, [In, Out] ref int length);
/// <summary>
/// 블럭 결과 값에서 인식된 후보문자를 반환한다
/// </summary>
/// <param name="block">[in] 블럭 결과 값</param>
/// <param name="index">[in] 후보 인덱스</param>
/// <param name="length">[out] 인식후보 문자열 크기</param>
/// <returns>후보문자</returns>
public static String GetCandidate(IntPtr block, int index, [In, Out] ref int length)
{
return Marshal.PtrToStringUni(GetCandidatePtr(block, index, ref length));
}
}
}
/*!
* @brief simple example source of math recognition
* @date 2020/01/17
* @file MathSimpleExample.cs
* @author SELVAS AI
*
* Copyright 2020. SELVAS AI Inc. All Rights Reserved.
*/
using System;
using Selvasai;
namespace MathExample
{
class Program
{
// points of '\frac{2x+4}{5y-2}'
// -1, 0 : end of stroke
// -1, -1 : end of ink
// (x0, y0), (x1, y1), ... (-1, 0) ... (xn, yn) .... (-1, 0), (-1, -1)
static int[] ink_fraction = {383, 614, 325, 755, 338, 759, 373, 759, 392, 772, 353, 808, 318,
825, 320, 805, -1, 0, 348, 691, 351, 688, 358, 676, 397, 668, 427, 667, 433, 663, -1, 0,
528, 642, 533, 647, 524, 654, 506, 696, 503, 729, 515, 726, 528, 711, 561, 671, 586,
648, 585, 657, 572, 701, 563, 736, 545, 810, 518, 860, 498, 865, 478, 849, 480, 822,
515, 783, 535, 769, 587, 744, 620, 735, 631, 732, 642, 732, 644, 733, -1, 0, 714, 691,
712, 685, 750, 684, 771, 679, 778, 678, -1, 0, 880, 665, 891, 660, 926, 655, 938, 659,
958, 680, 940, 706, 923, 719, 883, 743, 876, 750, 892, 752, 933, 749, 979, 744, 999,
743, 1023, 743, 1003, 744, -1, 0, 193, 531, 206, 526, 258, 529, 316, 528, 384, 524, 418,
522, 500, 520, 575, 519, 604, 518, 661, 517, 715, 516, 752, 515, 798, 512, 852, 508,
873, 508, 915, 505, 961, 502, 979, 501, 1023, 500, 1055, 500, 1070, 499, 1100, 497,
1135, 495, 1146, 493, 1163, 492, 1152, 490, 1131, 487, -1, 0, 302, 341, 340, 342, 365,
355, 369, 379, 327, 416, 313, 427, 304, 436, 337, 430, 362, 424, 414, 410, 430, 406,
423, 406, -1, 0, 444, 337, 450, 337, 479, 319, 509, 325, 511, 347, 492, 386, 481, 405,
465, 428, 472, 414, -1, 0, 537, 292, 534, 292, 512, 303, 498, 337, 500, 378, 520, 419,
529, 429, 548, 445, 555, 452, 558, 453, -1, 0, 607, 374, 613, 369, 660, 371, 728, 364,
761, 356, 760, 349, -1, 0, 688, 316, 686, 318, 681, 331, 690, 388, 693, 407, 696, 426,
697, 423, -1, 0, 842, 292, 851, 296, 849, 311, 830, 354, 821, 376, 827, 391, 861, 398,
926, 402, 985, 401, 995, 400, 989, 398, -1, 0, 959, 281, 941, 293, 916, 337, 890, 401,
869, 458, 864, 479, 866, 488, -1, 0, -1, -1
};
// points of '\sqrt{144}+\sqrt{1728}'
static int[] ink_square_root = {376, 665, 375, 659, 383, 680, 394, 704, 405, 717, 410, 691, 411,
666, 408, 566, 404, 495, 410, 450, 413, 437, 419, 429, 449, 427, 498, 436, 602, 440,
759, 428, 824, 421, 859, 418, 857, 428, -1, 0, 516, 571, 509, 580, 513, 625, 519, 648,
526, 644, -1, 0, 624, 545, 627, 545, 627, 553, 594, 582, 562, 604, 567, 609, 606, 605,
657, 596, 670, 588, -1, 0, 613, 555, 612, 553, 620, 569, 624, 616, 630, 634, 635, 632,
-1, 0, 728, 547, 729, 548, 695, 580, 671, 597, 672, 601, 716, 601, 775, 594, 786, 590,
-1, 0, 739, 541, 728, 562, 729, 606, 733, 642, 743, 654, -1, 0, 1063, 519, 1054, 536,
1058, 577, 1060, 597, -1, 0, 1023, 563, 1032, 558, 1087, 553, 1117, 552, -1, 0, 1315,
620, 1319, 620, 1343, 652, 1360, 672, 1361, 650, 1359, 570, 1363, 484, 1365, 424, 1370,
413, 1391, 412, 1454, 415, 1519, 413, 1685, 397, 1816, 382, 1910, 377, 1980, 386, 1963,
394, -1, 0, 1506, 512, 1493, 512, 1499, 556, 1504, 592, 1509, 608, 1513, 602, -1, 0,
1586, 505, 1586, 511, 1573, 560, 1560, 549, 1564, 534, 1586, 506, 1614, 497, 1635, 525,
1637, 554, 1624, 617, 1618, 639, 1617, 641, 1623, 632, -1, 0, 1714, 542, 1731, 516,
1778, 519, 1792, 561, 1778, 578, 1735, 595, 1713, 593, 1733, 587, 1753, 584, 1811, 579,
1838, 578, -1, 0, 1925, 527, 1925, 520, 1906, 470, 1886, 476, 1877, 525, 1885, 576,
1873, 615, 1852, 617, 1862, 561, 1907, 535, 1942, 531, 1959, 542, -1, 0, -1, -1
};
static int MAX_CANDIDATES = 10;
static IntPtr inkObj = IntPtr.Zero;
static IntPtr settingObj = IntPtr.Zero;
static IntPtr resultObj = IntPtr.Zero;
static void MakeInputEvent(IntPtr ink, int[] inputs)
{
Hwr.InkClear(ink);
int i = 0;
while (true)
{
if (inputs[i + 0] == -1 && inputs[i + 1] == -1) // if end of ink
{
break;
}
else if (inputs[i + 0] == -1 && inputs[i + 1] == 0) // if end of stroke
{
Hwr.EndStroke(ink);
}
else
{
Hwr.AddPoint(ink, inputs[i + 0], inputs[i + 1]);
}
i += 2;
}
}
static String getCandidates(IntPtr result)
{
String output = "";
bool exit = false;
int length = 0;
int lineSize = Hwr.GetLineSize(result);
if (lineSize == 0)
{
output = "result empty";
return output;
}
for (int i = 0; i < MAX_CANDIDATES; i++)
{
for (int j = 0; j < lineSize; j++)
{
IntPtr line = Hwr.GetLine(result, j);
int blockSize = Hwr.GetBlockSize(line);
for (int k = 0; k < blockSize; k++)
{
IntPtr block = Hwr.GetBlock(line, k);
if (Hwr.GetCandidateSize(block) <= i)
{
exit = true;
break;
}
output += "[" + (i + 1) + "] ";
output += Hwr.GetCandidate(block, i, ref length);
if (k + 1 < blockSize)
{
output += " ";
}
}
if (exit)
{
break;
}
if (j + 1 < lineSize)
{
output += "\n";
}
}
if (exit)
{
break;
}
output += "\n";
}
return output;
}
static void PrintResult(String title, int status)
{
if (status == Hwr.ERR_SUCCESS)
{
Console.WriteLine("{0} ...Success", title);
Console.WriteLine("Recognized Text: \n{0}", getCandidates(resultObj));
}
else
{
Console.WriteLine("{0} ...Failed ({1})", title, status);
}
}
static void TestFraction()
{
Hwr.ClearLanguage(settingObj);
Hwr.AddLanguage(settingObj, Hwr.DLANG_MATH_MIDDLE_EXPANSION, Hwr.DTYPE_MATH_EX);
Hwr.SetAttribute(settingObj);
MakeInputEvent(inkObj, ink_fraction);
int status = Hwr.Recognize(inkObj, resultObj);
PrintResult("Fraction Test", status);
}
static void TestSquareRoot()
{
Hwr.ClearLanguage(settingObj);
Hwr.AddLanguage(settingObj, Hwr.DLANG_MATH_MIDDLE_EXPANSION, Hwr.DTYPE_MATH_EX);
Hwr.SetAttribute(settingObj);
MakeInputEvent(inkObj, ink_square_root);
int status = Hwr.Recognize(inkObj, resultObj);
PrintResult("Square root Test", status);
}
static int InitializeEngine()
{
int status = Hwr.Create("./license_key/license.key");
if (inkObj == IntPtr.Zero)
{
inkObj = Hwr.CreateInkObject();
}
if (settingObj == IntPtr.Zero)
{
settingObj = Hwr.CreateSettingObject();
}
if (resultObj == IntPtr.Zero)
{
resultObj = Hwr.CreateResultObject();
}
Hwr.SetExternalResourcePath("./hdb/");
Hwr.SetExternalLibraryPath("./lib/");
Hwr.SetRecognitionMode(settingObj, Hwr.MULTICHAR);
Hwr.SetCandidateSize(settingObj, MAX_CANDIDATES);
Console.WriteLine("SDK Version: {0}", Hwr.GetRevision());
Console.WriteLine("Initialize Engine ...{0}", status == Hwr.ERR_SUCCESS ? "Success" : "Failed");
return status;
}
static int DestroyEngine()
{
int status = Hwr.Close();
if (inkObj != IntPtr.Zero)
{
Hwr.DestroyInkObject(inkObj);
}
if (settingObj != IntPtr.Zero)
{
Hwr.DestroySettingObject(settingObj);
}
if (resultObj != IntPtr.Zero)
{
Hwr.DestroyResultObject(resultObj);
}
Console.WriteLine("Destroy Engine ...{0}", status == Hwr.ERR_SUCCESS ? "Success" : "Failed");
return status;
}
static void Main()
{
InitializeEngine();
TestFraction();
TestSquareRoot();
DestroyEngine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment