private static string RunOcr() { int res; TNSOCR.Img_DeleteAllBlocks(ImgObj); if (Frame.Width > 0) { Rectangle r = Frame; int w, h; TNSOCR.Img_GetSize(ImgObj, out w, out h); if (r.X < 0) { r.X = 0; } if (r.Y < 0) { r.Y = 0; } if (r.Width > w) { r.Width = w; } if (r.Height > h) { r.Height = h; } int BlkObj; res = TNSOCR.Img_AddBlock(ImgObj, r.Left, r.Top, r.Width, r.Height, out BlkObj); if (res > TNSOCR.ERROR_FIRST) { ShowError("Img_AddBlock", res); return(null); } //detect text block inversion TNSOCR.Blk_Inversion(BlkObj, TNSOCR.BLK_INVERSE_DETECT); Frame.Width = 0; } //zoning will not be executed since we have created text block res = TNSOCR.Img_OCR(ImgObj, TNSOCR.OCRSTEP_ZONING, TNSOCR.OCRSTEP_LAST, 0); if (res > TNSOCR.ERROR_FIRST) { ShowError("Img_OCR", res); return(null); } int flags = TNSOCR.FMT_EDITCOPY; //or TNSOCR.FMT_EXACTCOPY StringBuilder txt = new StringBuilder(0); int n = TNSOCR.Img_GetImgText(ImgObj, txt, 0, flags); //get buffer text length plus null-terminated zero txt = new StringBuilder(n + 1); TNSOCR.Img_GetImgText(ImgObj, txt, n + 1, flags); return(txt.ToString()); }
private static bool LoadImage(string fileName) { int res = TNSOCR.Img_LoadFile(ImgObj, fileName); if (res > TNSOCR.ERROR_FIRST) { ShowError("Img_LoadFile", res); return(false); } int w0, h0, w, h; //native size TNSOCR.Img_GetSize(ImgObj, out w0, out h0); //now apply image scaling, binarize image, deskew etc, //everything except zoning and OCR res = TNSOCR.Img_OCR(ImgObj, TNSOCR.OCRSTEP_FIRST, TNSOCR.OCRSTEP_ZONING - 1, 0); if (res > TNSOCR.ERROR_FIRST) { ShowError("Img_OCR", res); return(false); } //final size after pre-processing TNSOCR.Img_GetSize(ImgObj, out w, out h); /*res = TNSOCR.Img_GetSkewAngle(ImgObj); * if (res > TNSOCR.ERROR_FIRST) * { * ShowError("Img_GetSkewAngle", res); * //lbSkew.Text = ""; * } * else * { * //lbSkew.Text = "Skew angle (degrees): " + (res / 1000.0); * } * * //pixel lines info * res = TNSOCR.Img_GetPixLineCnt(ImgObj); * if (res > TNSOCR.ERROR_FIRST) * { * ShowError("Img_GetPixLineCnt", res); * return false; * } * //lbLines.Text = "Lines: " + res.ToString(); * * //edPage.Text = "1"; * //lbPages.Text = "of " + TNSOCR.Img_GetPageCount(ImgObj).ToString(); */ Frame = new Rectangle(0, 0, w, h); return(true); }