public override OcrResult Load(OcrImage image, string language, string apiLanguage) { string inputPath = null; string outputPath = null; try { inputPath = GetTempPath(); outputPath = GetTempPath(false); string spacing = AllowsSpacing(language) ? string.Empty : "--nospacing"; string newLines = AllowsNewLines(language) ? string.Empty : "--nolines"; File.WriteAllBytes(inputPath, image.Data); RunCommand("\"{0}\" \"{1}\" \"{2}\" {3} {4}", apiLanguage, inputPath, outputPath, spacing, newLines); if (File.Exists(outputPath)) { string[] lines = File.ReadAllLines(outputPath); OcrResult result = new OcrResult(); result.ResultType = OcrResultType.Success; foreach (string line in lines) { result.AddLine(line); } return(result); } else { return(OcrResult.Create(OcrResultType.Failed)); } } catch (Exception e) { return(OcrResult.Create(OcrResultType.Exception, e.ToString())); } finally { try { if (!string.IsNullOrEmpty(inputPath)) { File.Delete(inputPath); } } catch { } try { if (!string.IsNullOrEmpty(outputPath)) { File.Delete(outputPath); } } catch { } } }
public static OcrResult FromBinary(byte[] buffer) { try { using (BinaryReader reader = new BinaryReader(new MemoryStream(buffer))) { OcrResult result = new OcrResult(); result.Rect = ReadRect(reader); result.ResultType = (OcrResultType)reader.ReadByte(); result.Error = reader.ReadString(); if (string.IsNullOrEmpty(result.Error)) { result.Error = null; } int areaCount = reader.ReadInt32(); for (int i = 0; i < areaCount; i++) { result.AddArea(ReadArea(reader)); } int lineCount = reader.ReadInt32(); for (int i = 0; i < lineCount; i++) { result.AddLine(ReadLine(reader)); } return(result); } } catch (Exception e) { return(OcrResult.Create(OcrResultType.Exception, e.ToString())); } }
public override OcrResult Load(OcrImage image, string language, string apiLanguage) { string inputPath = null; string outputPath = null; string outputPath2 = null; string outputPath3 = null; try { inputPath = GetTempPath(); outputPath = GetTempPath(false); outputPath2 = outputPath + (IncludeTextRegions ? ".hocr" : ".txt"); outputPath3 = outputPath + (IncludeTextRegions ? ".txt" : ".hocr"); File.WriteAllBytes(inputPath, image.Data); RunCommand("\"{0}\" \"{1}\" -l {2} {3}", inputPath, outputPath, apiLanguage, IncludeTextRegions ? "-c tessedit_create_hocr=1" : string.Empty); OcrResult result = null; if (IncludeTextRegions) { result = OcrXml.FromFile(outputPath2); } else { string[] lines = File.ReadAllLines(outputPath2); result = new OcrResult(); result.ResultType = OcrResultType.Success; foreach (string line in lines) { result.AddLine(line); } } return(result); } catch (Exception e) { return(OcrResult.Create(OcrResultType.Exception, e.ToString())); } finally { try { if (!string.IsNullOrEmpty(inputPath)) { File.Delete(inputPath); } } catch { } try { if (!string.IsNullOrEmpty(outputPath)) { File.Delete(outputPath); } } catch { } try { if (!string.IsNullOrEmpty(outputPath2)) { File.Delete(outputPath2); } } catch { } try { if (!string.IsNullOrEmpty(outputPath3)) { File.Delete(outputPath3); } } catch { } } }